AngularJS ng-grid -> afterSelectionChange triggers twice
Asked Answered
C

2

9

I have within ng-grid options:

afterSelectionChange: function (row, event) {
                console.log("Event: " + event);
                console.log("Row: " + row.rowIndex);
            },

The first time a row is selected, it works as expected. The second time, it triggers twice, once for the row leaving, and again for the new row. This is fine. The issue is, I want to take action once, for the new row. The only way I can do that is by examining the event. However the event is undefined.

Output:

Event: undefined activityGridController.js:13
Row: 0 activityGridController.js:14
Event: undefined activityGridController.js:13
Row: 1 

Question is, how to determine the new row?

Cauvery answered 17/11, 2013 at 5:39 Comment(0)
M
9

You could just set an extra property with the beforeSelectionChange event. Something like this

beforeSelectionChange: function(row) {
  row.changed = true;
  return true;
},
afterSelectionChange: function (row, event) {
  if (row.changed){
    console.log("deal with row " + row.rowIndex);
    row.changed=false;
  }
},
Maelstrom answered 18/11, 2013 at 19:17 Comment(1)
Excellent solution! I'm impressed it only has 7 upvotes. Worked for me.Attalie
Z
5

check the row.selected the first row should be false and the send row should be true.

  if(row && row.entity){
    if(row.selected){
       var selectedEntity = row.entity;
    }
  }
Zworykin answered 14/5, 2014 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.