Don't know why this is firing 4 Times, but it does not happen when you use selectedItems:
$scope.gridOptions = {
data: 'myData',
showSelectionCheckbox: true,
selectedItems:$scope.output
};
Not really an answer, but maybe it helps you.
Update Found out some more:
The event ngGridEventData
is fired 2 times:
On Initialization AND after
selectItem
by a watcher.Also
afterSelectionChange
is fired 2 time. The rowItem from first call is a clone (from cache?) the second one is noClone.
This sums up to 4!
So by taking init out of ngGridEventdata
and replacing it with a timeout as well as only pushing rowitems when they are a clone (why?) resolves this issue.
$scope.gridOptions = {
data: 'myData',
showSelectionCheckbox: true,
afterSelectionChange: function(rowItem, event) {
if (rowItem.isClone) {
$scope.output.push({
name: rowItem.entity.name,
selected: rowItem.selected
});
$scope.num++;
}
}
};
setTimeout(function() {
angular.forEach($scope.myData, function(data, index) {
if (data.name == 'Enos') {
$scope.gridOptions.selectItem(index, true);
}
});
})
I know this is still not an answer and smells like a bug to me, but here is another forked Plunker anyhow.
Of course you now have to find a way how to splice items out of the array when they are unselected. Good Luck!