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 Crendal,
Thank you for posting in our community.
From your post I assume that you have already implemented the logic for selecting/unselect rows on check/uncheck of the UnboundCheckBox column in the grid and what you are looking for is how to get the number or currently selected rows on the client side. If this is the case, what I can suggest is using get_selectedRows method of WebDataGrid. This method gets a reference to the currently selected rows collection. Afterwards, in order to get the count of the items in this collection the get_length method could be used. For example:
var grid = $find("WebDataGrid1"); var selectedRowsCount = grid.get_behaviors().get_selection().get_selectedRows().get_length();
var grid = $find("WebDataGrid1");
var selectedRowsCount = grid.get_behaviors().get_selection().get_selectedRows().get_length();
If I this is not an accurate demonstration of what you are trying to achieve please feel free to send me a small isolated sample where I could get a better idea of your scenario along with any clarifications if you consider them needed.
I hope you find this information helpful.
Please feel free to contact me if you have any additional questions regarding this matter.
That sounds exactly like what I'm looking for! Thanks!
Can you point me in the direction for the documentation on how to use JavaScript with an ASP.NET Webforms application? I'm not sure where I need to put this code to make it work.
Thanks,
Chris
Hello crendall,
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.
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!
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 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.