early
early

Reputation: 543

angular ng-grid: rowItem.rowIndex not map to index of data array after sorting

I found that the row Id will no longer map to the index of the data array if sorting is applied.

simple demo here: http://plnkr.co/edit/irVXdjAoCvAKPxQ1uD4P?p=preview

Is this intended behavior? or, is this a bug?

I know there is row.entity that can get the whole row information, but it does not help me directly to point to the data array. (unless an extra looping to the whole array is applied which is the worst case I gonna do)

so, does anyone know if there is any pointer/reference/variable I can use to point to the corresponding row of the data array after sorting is applied? Thank you.

Upvotes: 1

Views: 5398

Answers (2)

thanks for your answer this was very usefull. To move the selection to the next row after deletion you could do it like this (single select)

// Step 01: remember current selected rowindex:

gridOptions.afterSelectionChange: function(row, event) {
$scope.gridOptions.rowIndex = row.rowIndex;}

// Step 02: delete row and move selection

$scope.deleteRecord = function() {
var arrayIndex = $scope.gridOptions.ngGrid.rowMap.indexOf(rowItem.rowIndex);
var rowIndex = $scope.gridOptions.rowIndex; `on sorted grids these to might differ`
var e = $scope.$on('ngGridEventData', function() {
    if (rowIndex >= $scope.gridItems.length) {
                    // last element is deleted, thus move selection up
        rowIndex--;
    }
    if (rowIndex >= 0) {
        $scope.gridOptions.selectRow(rowIndex, true); 
        $scope.focusRow(rowIndex);
        e(); // deregister the listener
        $scope.entity2View();
    }
});
$scope.gridItems.splice(arrayIndex, 1);});

Upvotes: 0

early
early

Reputation: 543

Finally, I found the way to get the corresponding index of array even when sorting is applied.

demo here: http://plnkr.co/edit/irVXdjAoCvAKPxQ1uD4P?p=preview

By using rowMap like this,

$scope.gridOptions.ngGrid.rowMap.indexOf(rowItem.rowIndex);

which is not documented, so beware that the future patches/updates may affect this implementation.

Updated: With further studies, I found that selectedItems with multiSelect equals to false may serves the same purpose. However, it won't work exactly the same if you need the grid with multiSelect:true.

Upvotes: 3

Related Questions