View Full Version : How to get the actual count of the selected records in a grid?.

18 Aug 2014, 8:14 AM

I am using 4.2.1. I am having a search results grid with multi-select option on my page and there is a display field below the grid which should show the number of records selected in the grid. So after a search, if a user clicks on a record in the grid, I should display that the count is one. If he selects more, the count should increase accordingly and if he clicks on a record which is already selected which means de-selection, the count should reduce. For this purpose I wrote a on-click event on the search results grid as follows.

var countSelectedRecords = 0;
countSelectedRecords = this.searchResultsGrid.getSelectionModel().getCount();

The problem I am facing with this is that when a user comes and selects a record for the first time it shows the total count as 0. When the user selects another record, then it shows the count as 1. Also when a user multiselects, say 3 records, the selected items count comes as 2. Not sure why it is not able to pickup the first selection. Also when a user deselects a record(Clicking on an already selected record), it is not able to pickup the first de-selection. It lags the count by 1 record everytime. Not sure where I am going wrong.

Any advice please....

18 Aug 2014, 8:42 AM
I think the problem is here is that this function gets called by the onclick event. At that point of time the record is not selected yet. So the count shows as zero. Only after it gets past this function, this record gets selected and that is the reason why it always shows the previous count. Any suggestion on when I should call this function or if I can make any changes,


18 Aug 2014, 11:16 AM
Ok, I changed the itemclick event to 'select' event and now it works fine in giving me the correct count of the records selected even the first time a record is selected. Only problem is when I multi-select records, say I multi-select 4 records and then click on an already selected record, the record gets unselected which it should be but the count won't change. It still shows the total selected records as 4 while the actual selected records are 3.Can someone please let me know how I can set this right.

18 Aug 2014, 4:14 PM
You could use the selectionchange event:

The second parameter passed to this event is the selected records array. you could just do an array.length to show the amount selected.