How to set row selection to newly added row?

风流意气都作罢 提交于 2019-12-12 16:29:03

问题


I need to set selection to a newly added row. When $scope.gridApi.selection.selectRow(newObject) is entered, the grid's row model does not have the new row and thus can not select it. After method addRow is finished, the grid shows the new row (and the data) but no selection was made.

My test code:

$scope.addRow = function() {
    $scope.gridOptions.data.unshift({
        // my new data object
    });

    var newObject = $scope.gridOptions.data[0];
    $scope.gridApi.selection.selectRow(newObject);
};

How can I achieve this behavior?

Is there an event like 'rowAdded' in angularjs ui-grid I have to listen to? Is there anywhere a comprehensive list of events fired by ui-grid?

Thanks for help!


回答1:


You can event listeners on row, column and so on. The documentation is here http://ui-grid.info/docs/#/api/ui.grid.class:Grid

You can add the data listener in onRegisterApi like below,

onRegisterApi : function(gridApi)
            {
                if(!$scope.gridApi) 
                {
                    $scope.gridApi = gridApi; 
                    $scope.gridApi.grid.registerDataChangeCallback(function(data)
                    {
                      $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
                    }, [uiGridConstants.dataChange.ROW]);
                }
            }

and select any row you want to select.

Here is a working plnkr. http://plnkr.co/edit/NjnMb65w86L0bKmkkJp0?p=preview



来源:https://stackoverflow.com/questions/30329748/how-to-set-row-selection-to-newly-added-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!