Jquery Functions for operations on gridview with checkboxes - asp.net

The first column in my gridview (gvAvailable) is a currently checkbox column "chkSelect".
The way it works now is that a user can check multiple checkboxes on a gridview, but I would like a jquery function to deselect all the checkboxes on the gridview except for the checkbox that was clicked.
I was also looking for a way to access the columns of the checked row with jquery on a button click.
Thanks for any help
Here's how the generated html looks
<table class="Grid" cellspacing="0" border="0" id="gvAvailable" style="width:99%;border-collapse:collapse;">
<tr class="GridHeader">
<th scope="col"> </th><th scope="col">Guid</th><th scope="col">Id</th><th scope="col">Name</th>
<th scope="col">Facility</th><th scope="col">Room</th>
</tr>
<tr class="GridItem">
<td>
<input id="gvAvailable_ctl02_chkSelect" type="checkbox" name="gvAvailable$ctl02$chkSelect" />
</td>
<td>24</td>
<td>000101020201</td>
<td>Test</td>
<td>Test Facility</td>
<td> </td>
</tr><tr class="GridAltItem">
<td>
<input id="gvAvailable_ctl03_chkSelect" type="checkbox" name="gvAvailable$ctl03$chkSelect" />
</td>
<td>25</td>
<td>1001</td>
<td>Test 2</td>
<td>Test 3</td>
<td> </td>
</tr>
</table>

if you add the same class to each of the checkboxes in the markup, you can retrieve an array of them by saying
$('.classname')
This will give you back the array of objects. You can then call 'each' on the array and deselect them all.
function removeChecks()
{
$('.classname').each(function()
{
$(this).removeAttr('checked');
});
}
Then add the above function call in the click handler for the each checkbox:
$('.classname').each(function()
{
$(this).click(function()
{
removeChecks();
$(this).attr('checked', 'checked');
});
});
the code above should be set up to run on page load.

In this example I put one button to check, and one button to uncheck gridview checkboxes :
<asp:GridView runat="server" ID="grid"></asp:GridView>
<input type="button" onclick="uncheckCheckBoxes()" value="UnCheck" />
<input type="button" onclick="checkCheckBoxes()" value="Check" />
<script>
function uncheckCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = false;
});
}
function checkCheckBoxes()
{
var gridClientID = '<%= grid.ClientID %>';
jQuery.each($("#" + gridClientID + " input[type='checkbox']"), function ()
{
this.checked = true;
});
}
</script>

I am assuming you would like to make sure the user clicked checkboxes do not get toggled? If so, go on below.
First, just give a class name to all checkboxes in the gridview.
Whenever a checkbox was clicked, add another class to it to denote it was physically selected.
$('.checkbox').click(function(){
$(this).addClass('checked');
});
Now, when a user clicks on the select all checkbox (let's call it "selectAll") on top, iterate through all the checkboxes and toggle the status while checking the "checked" class
$('#selectAll').click(function(){
var status = $(this).attr('checked')
$('.checkbox').each(function(){
//only toggle if not set
if(!$(this).hasClass('checked')){
if(status){
$(this).attr('checked', 'checked');
}
else{
$(this).attr('checked', '');
}
}
});
});
This should get you along your merry way hopefully.
Now, accessing columns of the checked row?
You could add an onclick event to each table row.
$('#tablename tr').click(function(){
//do something
});

i found this articale very usefull Check/Uncheck all Items in an ASP.NET CheckBox List using jQuery

Related

get table column value of selected row in table asp.net in jQuery

I am working on asp.net mvc project.
I want to get the cell value from the selected row (row in which "Manage "button is clicked).
in this case value of userID.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th width="45%">User ID</th>
<th width="45%">User Name</th>
<th width="5%">View</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TypeList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
<input id="Manage2" class="btn btn-primary" type="button" value="Manage" />
</td>
</tr>
}
</tbody>
I am calling jQuery click function and Ajax call and want to send the UserId value in data from selected row to the controller.
below is the jQuery ajax call,
<script type="text/javascript">
$(document).ready(function () {
$('#Manage2').click(function () {
//alert(1);
var url = '#Url.Action("ManageUserRole", "UserRoleCompany")';
$.ajax({
url: url,
data: { Id: '1' },
cache: false,
type: "POST",
success: function (data) {
$("#Data").html(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
});
});
</script>
Below is the view screen shot,
You can actually store the UserId value with html 5 data attribute on the button itself.
Your current code has Id attribute value hard coded inside a loop, which will create more than one element with same Id value. That is invalid HTML!
Remove the Id and use a more generic selector to wire up the click event. Here I added another CSS class "manage" to the button.
<input data-userid="#item.UserId" class="btn btn-primary manage"
type="button" value="Manage" />
and now
$(document).ready(function () {
$('.manage').click(function () {
var id = $(this).data("userid");
alert(id);
// use id for your ajax call
});
});
You do not necessarily need to add the extra css class only to use that as the selector later. Since you are adding a data attribute, you may simply use that as well for your jQuery selector.
$(function () {
$('[data-userid]').click(function () {
var id = $(this).data("userid");
alert(id);
// use id
});
});
You have invalid html because of the duplicate id attributes in your button.
Remove the id="Manage2" and replace with a class name and data- attribute for the value
<input data-id="#item.UserId" class="btn btn-primary Manage2" type="button" value="Manage" />
Then in the script, get the value using
$('.Manage2').click(function () { // class selector
var id = $(this).data('id);
var url = '#Url.Action("ManageUserRole", "UserRoleCompany")';
$.ajax({
url: url,
data: { Id: id },
....
Alternatively you could use relative selectors
$('.Manage2').click(function () {
var id = $(this).closest('tr').children('td:first').text();
....

How to add validation on a dynamic textbox based from the model?

Say I have this Jquery code:
$('.additems').click(function () {
$table = $('.item');
$table.append('<tr> <td> <input type="textbox" name="test"> </td> </tr>');
});
And I want to enable the validation from the [Required] attribute of my model who contains the property test
I tried adding the autogenerated html from the #Html.ValidationMessageFor which is
<span class="field-validation-valid" data-valmsg-for="test" data-valmsg-replace="true"></span> but it's not working.
How do I do it?
You can try different solution
$('.additems').click(function () {
$table = $('.item');
$table.append('<tr> <td> <input type="textbox" class="myReq" name="test"> </td> </tr>');
});
function foo() {
$(".myReq").each(function () {
//Your Control
});
}

Get link from text input field and then open it with DalekJS

I have an issue where the link I need to traverse to using DalekJS that is not clickable, it is rather copy/pastable. How can I retrieve a value from an input field in the browser and then use it in a test.open() call?
<table>
<tbody>
<tr>
<td>Link 1</td>
<td><input type="text" value="http://example.com/link-1" class="start-link"></td>
</tr>
<tr>
<td>Link 2</td>
<td><input type="text" value="http://example.com/link-2" class="start-link"></td>
<tr>
</table>
In the example above I would like DalekJS to dynamically run test.open('http://example.com/link-1'); during the progression of my test.
module.exports = {
'A test case': function (test) {
test.open('http://example.com/example-from-above.html')
.open('http://example.com/link-1') //This is the link I'm trying to retrieve dynamically.
.screenshot('image.png')
.done();
}
}
How can I accomplish this?
DalekJS does not allow reading values of HTML element synchronously and using them in the test scripts. If you need to interact with the content of the testing page, the execute method may help.
You can try setting the window location by a function "executed in the browser":
module.exports = {
'A test case': function (test) {
test.open('http://example.com/example-from-above.html')
// Open the URL from the first input element value
.execute(function () {
var input = document.querySelectorAll('.start-link')[0];
location = input.value;
})
// Wait until the browser opens the page
.waitFor(function () {
return location.pathname.indexOf('link-1') > 0;
})
.screenshot('image.png')
.done();
}
}

Button in a cell of easyiu-datgrid

I have get a easyui-datagrid from a php file. I want insert a buttons on a colum, but I donĀ“t know how to do it...
This is my code
<table id="dg_lineas_albaran" class="easyui-datagrid" style="width:700px;height:auto"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
toolbar: '#tb_lineas_albaran',
onClickRow: onClickRow
">
<thead>
<tr>
<th id="combo_productos" data-options="field:'cod_producto',width:60,
formatter:function(value,row){
return row.cod_producto;
},
editor:{
type:'combogrid',
options:{
panelWidth: 500,
idField: 'cod_producto',
textField: 'cod_producto',
mode: 'remote',
url: 'get_productos_combo.php',
columns: [[
{field:'cod_producto',title:'Cod_producto',width:50,sortable:true},
{field:'descripcion_producto',title:'Descripcion',width:150,sortable:true},
]],
fitColumns: true,
}
}
">CODIGO</th>
<th data-options="field:'descripcion_producto',width:250,editor:'text'">OBSERVACIONES</th>
<th data-options="field:'cantidad_producto',width:80,align:'right',editor:'numberbox'">Cantidad</th>
<th data-options="field:'precio_producto',width:80,align:'right',editor:{type:'numberbox',options:{precision:2}}">PRECIO</th>
<th data-options="field:'importe_producto',width:80,align:'right',editor:'numberbox'">IMPORTE</th>
<th data-options="field:'action',width:80,align:'right'"
>NUM SERIE</th>
<th data-options="field:'status',width:60,align:'center',editor:{type:'checkbox',options:{on:'P',off:''}}">Status</th>
</tr>
</thead>
</table>
I will like insert a button in the same colum than the combo and another one in the cell of NUM_SERIE to open a easyui-window.
Thank for all.
Add this to your javascript file inside $(function(){ ... }
$("#dg_lineas_albaran").datagrid({
// Fires when data in datagrid is loaded successfully
onLoadSuccess:function(){
// Get this datagrid's panel object
$(this).datagrid('getPanel')
// for all easyui-linkbutton <a>'s make them a linkbutton
.find('a.easyui-linkbutton').linkbutton();
}
});
The problem is the data from the grid is loaded after the browser loads easyui. In order to get easyui to recognize the linkbuttons, you have to tell it to look for them after the data is loaded.
This is a sample add this to a data grid
<th field="PRO_RIF" align='right' width="50" formatter="acciones">Aciones</th>
a use this sample script
<script>
function acciones(val,row){
var url = 'print.php?id='+val;
return '<a target="_blank" href="' + url + '"><button>Print</button></a>';
}
</script>

How to Get / Set Div and Table Width / Height

I have a Table (or a region) and want to set it's Width and Height value to another Div (or region).
The second one is actually a Ajax Indicator modal which display a loading text when the page is asynchronously post back. here is the example
<table id="MainTable">
<tr>
<td>
Content ....
</td>
</tr>
</table>
<div id="Progress">
Ajax Indocator
</div>
the following javascript didn't work
document.getElementById("Progress").style.width = document.getElementById("MainTable").style.width;
document.getElementById("Progress").style.height = document.getElementById("MainTable").style.height;
It should work both on IE and FireFox. how to correct it.
I checked some other solution in StackOverFlow but I couldn't fix it.
I'm waiting to hear from you.
Update : I use this
<script>
function SetSize(regionToChange, mainRegion) {
$(document).ready(function() {
$('#regionToChange)
.width($('#mainRegion).outerWidth())
.height($('#mainRegion).outerHeight());
});
}
</script>
and I call it like
<asp:Button ID="btnReset" runat="server" SkinID="Button" Text="Reset" OnClick="btnReset_OnClick" OnClientClick="SetSize('Progress', 'MainTable');" />
But it shows me an error which can not find the object
Update 2 I see this error
and in debugger I face with this
spoken in jQuery:
$(document).ready(function(){
$('#Progress')
.width($('#MainTable').outerWidth())
.height($('#MainTable').outerHeight());
});
in jQuery you can do
$("#Progress").css('width', function(){
return $("#MainTable").width()+'px';
});
and so with the height...
edit
on javascript,
this
document.getElementById("Progress").style.width = document.getElementById("MainTable").style.width;
document.getElementById("Progress").style.height = document.getElementById("MainTable").style.height;
will work if your html is something like this for id="MainTable"
<table id="MainTable" style="width: 500px; height: 300px;">
<tr>
<td>
Content ....
</td>
</tr>
</table>
because you are accessing style attribute...
edit2
function SetSize(regionToChange, mainRegion) {
$(regionToChange)
.width($(mainRegion).outerWidth())
.height($(mainRegion).outerHeight());
}
//you can use it as
SetSize('#Progress','#MainTable'); // prefix '#'if it's an ID

Resources