row selection via selectItem fails when grid is sorted and then a new row is added
I have a grid of say 4 rows. I want to add a row to the data and have that row be automatically selected. to do this I added a listener to ngGridEventData event. when the data changes I perform a call to selectItem with the new item's index as the argument.
this works fine when the grid is not sorted.
but doesn't work as expected when grid was sorted before the new data item was added. if the grid is sorted, and then a new data item is added to the grid's data source. it correctly displays the newly added item in the correct sort order. however, it selects the wrong row when selecting the row via a call to selectItem (as described above).
I am using the latest build 2.0.4 . I looked at the code and noticed this was caused due to a bug in the sortActual method.
a snippet is shown below
self.sortActual = function() {
if (!self.config.useExternalSorting) {
var tempData = self.data.slice(0);
angular.forEach(tempData, function(item, i) {
var e = self.rowMap[i];
if (e != undefined) {
var v = self.rowCache[v];
if(v != undefined) {
item.preSortSelected = v.selected;
item.preSortIndex = i;
}
}
});
as you can see on the line
var v = self.rowCache[v];
the wrong variable is being used to index into rowCache.I believe it should in fact be
var v = self.rowCache[i];
as this was a minor issue, i felt lazy to make a pull request.
thank you for your great work. ng-grid is an awesome tool.
@sum4me - I opened a similar issue as I cannot find a way to add a new row and have it selected. Can you share some of the code that you have to add the new row.
Reference: #354.
I have a plunker demonstrating this issue with the code @RichardAlan supplied in #354: http://plnkr.co/edit/b58tAV (though I believe it should be selectItem(rowLen-1, true) ...
Once I get the the end-to-end tests set up, I will try to use the fix @sum4me suggested. Thanks!