I have an unbound checkbox column that the user checks to select rows. How can I count up the number of rows clien-side the user has checked?
Hello crendall,
You are welcome.
Please let me know if you need any further assistance with this matter, I would be glad to help.
You definitely pointed me in the right direction!
I modified the code you sent me to this:
function wdg_CellValueChanged(sender, e) { var columnKey = e.get_cell().get_column().get_key();
if (columnKey == "chkSelect") { var rowsLength = e.get_cell().get_column(0).get_checkedCount(); }
$("#selectedItems").text(rowsLength); }
Using the get_checkedCount() function I was able to get the number of rows checked without having to loop through all the rows.
Thanks for helping figure out my code!
Chris
What I can suggest on order to get the count of the checkboxes that have been checked is handling CellValueChanged client side event. This event is fired when the value of a cell changes. In this event you could be checked whether the column that fired the event is the column with the checkboxes. Afterwards, if this is the UnboundCheckbox column you could loop trough all the rows and check if the checkbox is checked, respectively whether the row is selected or not. The cound coulkd be stored in a global variable. For example:
function WebDataGrid1_Editing_CellValueChanged(sender, eventArgs) { var grid = $find("WebDataGrid1"); var selRowsCount = 0; var columnKey = eventArgs.get_cell().get_column().get_key(); if (columnKey == "SelectRow") { var rowsCount = grid.get_rows().get_length(); for (var i = 0; i < rowsCount; i++) { if (grid.get_rows().get_row(i).get_cellByColumnKey("SelectRow").get_value()) { selRowsCount++; } } alert(selRowsCount); } }
function WebDataGrid1_Editing_CellValueChanged(sender, eventArgs)
{
var grid = $find("WebDataGrid1");
var selRowsCount = 0;
var columnKey = eventArgs.get_cell().get_column().get_key();
if (columnKey == "SelectRow") {
var rowsCount = grid.get_rows().get_length();
for (var i = 0; i < rowsCount; i++) {
if (grid.get_rows().get_row(i).get_cellByColumnKey("SelectRow").get_value()) {
selRowsCount++;
}
alert(selRowsCount);
I hope this helps.
Please let me know if you need any further assistance with this matter.
I'm very close to getting this working the way I want.
I'm looking to count how many of the unbound checkboxes have been checked. Right now it's just telling me how many rows have been selected, which is usually one. When the user clicks on a checkbox I'd like an event to fire that would display how many checkboxes have been checked.
Thanks,
I am glad that you consider my information helpful.
I believe you will find the following article from our documentation useful:
http://help.infragistics.com/doc/ASPNET/2014.1/CLR4.0/?page=WebDataGrid_Selection.html
There you will find everything needed for the Selection behavior in both C# and Java Script.
Regarding the piece of code, suggested in my previous post, it could be used anywhere in code (where the WebDataGrid and its behaviors are initialized). For example you could retrieve the number of selected rows on a button click, or in an event.
If you have some doubts about the usage of the provided code for your particular scenario please free to let me know and I would be glad to help you.