Page 1 of 1

onRowClick and getSelectedRows

Posted: Thu May 17, 2018 11:45 am
by ndeppe
We have an application in which users can select multiple rows, and the screen displays a sum of values from the selected rows. I figured the best way to do this is to put a code in the grid's onRowClick event that would use getSelectedRows() to get a list of all rows that are selected, then get the value from each row returned in the array to get the sum of the values. The problem is it seems like the the onRowClick event is firing before the row selection property is being set. So when the code executes in onRowClick, the result of getSelectedRow() is the selected rows before I make the selection, and not after.

Is there a better way to determine what rows are selected when a user makes a selection?

Re: onRowClick and getSelectedRows

Posted: Fri May 18, 2018 6:01 pm
by Scott Klement
I've never done this, so not sure what to tell you.

I have done something similar with the grid's filter feature. There, the user can select rows by changing the filter string, and the grid has an "onfilterchange" event that runs whenever the filter changes, this way you can update the totals whenever it changes, which is really convenient. Would that work for you?

I haven't done this with row selection, exactly. I've done similar-ish things by having the user click a button to get the results (in my case, it wasn't a total, but it could've been) or by using the right-click (context) menu to do things based on row selection. These also worked fine. So you could consider these alternatives as well.

But, never tried to do it when the selection is clicked.

Re: onRowClick and getSelectedRows

Posted: Mon May 21, 2018 8:51 pm
by Megan
Hello Nick,

I have looked into this further and have a solution that may work for you. I used the following code on the onrowclick event of the grid:

Code: Select all

setTimeout(function() {
  var myGrid = getObj("Grid1");
  var myArray = myGrid.grid.getSelectedRows(); 
  
  var totalInts = 0;
  var totalDecs = 0;
  var totalMnys = 0;
  
  for (var i=0; i<myArray.length; i++) {
    
    var rrn = myArray[i];
 
    totalInts += parseInt(get("Ints." + rrn),10);
    totalDecs += parseFloat(get("Decs." + rrn));
    totalMnys += parseFloat(get("Monies." + rrn));
    
  }
  
  applyProperty("TotalInts", "value", totalInts);
  applyProperty("TotalDecs", "value", totalDecs.toFixed(5));
  applyProperty("TotalMnys", "value", totalMnys.toFixed(2));
  
}, 10);
The timeout function gives the program a chance to finish updating the selected rows list, which is then available to be used to get the values from the cells and add them up. You may need to change the time value of the timeout function in order for the selected rows list to have enough time to update. I used '+=' to add up the values in the for loop. I then applied the value property with the new calculated value.

Result:
totalonrowclick.gif
totalonrowclick.gif (1007.53 KiB) Viewed 803 times
I hope this helps!

Re: onRowClick and getSelectedRows

Posted: Wed Jun 06, 2018 5:12 pm
by ndeppe
Hi Megan,

This solution worked for me, although I'm not too fond of using a timeout in a situation like this. I'm just not sure if there is a guarantee of the row selection property being updated within 10 milliseconds of the onRowClick event. This solution does seem to be working for me though and I haven't been able to break it.

Perhaps in the future a new event can be added when a user makes a selection on a grid, like onRowSelected for example. The event would fire after the selection property is set on the row. Does that make sense? I could submit an RFE for this.

Re: onRowClick and getSelectedRows

Posted: Wed Jun 06, 2018 5:32 pm
by Megan
Hello Nick,

I'm glad to hear my solution worked for you! I also completely understand your desire to have a onRowSelected event. If you would like this event submitted as a feature request, could you please send us an email at support@profoundlogic.com with a general description of how you would like the onRowSelected function (i.e. row number should be passed to the event) and how important this feature is for you. This information will help our developers in assessing the necessity of the feature and the urgency of the feature to be implemented if accepted.

Thanks,