ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

How can I have the grid scroll and select a new row when I add a row to my data model ?

Open RichardAlan opened this issue 13 years ago • 1 comments

I have a master detail grid where the selected row data is displayed below the grid. This is similar to your example. Now I would like to add functionality so that when I click on an add button then a new row is added to the model, the grid scrolls to that new row, the new row gets selected and my details appear below the grid. I tried the following:

        $scope.addTest = function () {
            var TestRow = {
                "name": "xx",
                "description": "xx",
                "subjectId": 15
            };
            $scope.gridOptions.selectAll()
            var testRowLength = $scope.gridData.push(TestRow);
            $scope.gridOptions.selectItem(testRowLength, true);
        }

When I click a button to call the add function then a new element is added to my data model but the new row added does not get selected. Can you help out by providing an example of how to do this. I'm sorry to be adding this as an "issue" but I am not sure where to get help with this and I can't find any example in the documentation.

RichardAlan avatar Apr 22 '13 05:04 RichardAlan

This is actually unrelated to #348. When you push on to the grid's data the new row is not available yet for you to select. You need to attach a listener for the ngGridEventData event and then select the new row after the grid has been rendered.

Here is a working plunk: http://plnkr.co/edit/6ogoYI?p=preview

One possibility to make this cleaner would be to add an addRow function to the grid's options, which would return a promise that resolves after the grid renders. You could use it like so:

var newRow = { name: 'Bob', age: 25 };
$scope.gridOptions.addRow(newRow)
    .then(function(){
        $scope.gridOptions.selectItem($scope.gridData.length+1, true);
    });

c0bra avatar Apr 23 '13 19:04 c0bra