Anton Anton - 5 months ago 137
AngularJS Question

how to set always selected first row ui-grid

I was looking for this function thru api and tutorial but found only select the first row in the first grid init,

gridApi.selection.selectRow($scope.gridOptions.data[0]);


I was trying to use this capability inside
onRegisterApi
or inside filter function

$scope.filter = function() {
$scope.gridApi.grid.refresh();
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
}

$scope.singleFilter = function( renderableRows ){
var matcher = new RegExp($scope.filterValue);
renderableRows.forEach( function( row ) {
var match = false;
[
// 'id',
// 'study.name',
'title',
'occuredDate',
// 'eventType.name',
'description',
'createdDate' ,
// 'priority.name',
'severity.name',
'status.name',
'createdDate'
].forEach(function( field ){
if (field.indexOf('.') !== '-1' ) {
field = field.split('.');
}
if ( row.entity.hasOwnProperty(field) && row.entity[field].match(matcher) || field.length === 2 && row.entity[field[0]][field[1]].match(matcher)){
match = true;
}

});
if ( !match ){
row.visible = false;
}
});
return renderableRows;
};


but nothing helps. I need that first row always been selected even if I'm filtering data thru columns filter or with using single Filter. Is it possible in ui-grid?

my plunker

Answer

Try adding the following line to your gridApi.selection.on.rowSelectionChanged-Function:

gridApi.selection.selectRow($scope.gridOptions.data[0]);

This way row number 1 will always be selected.

A forked plunkr: plunkr