I am using ui-grid in my web application.
Everything is working fine, the issue is when I refresh the grid data the selection gets removed.
In the Fiddle when I select a row and then hit the refresh the button the ui-grid selection gets removed.
JSFiddle: http://jsfiddle.net/mdawood1991/xyuotpe8/6/
HTML:
<div ng-controller="MyCtrl as controller">
<button ng-click="controller.refreshData()" type="button">
Refresh
</button>
<div ui-grid="controller.assetListGrid" ui-grid-selection></div>
</div>
Controller:
var myApp = angular.module('myApp', ["ui.grid", "ui.grid.selection"]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
myApp.controller("MyCtrl", function($scope) {
var self = this;
$scope.name = 'Superhero';
self.assetListGrid = {};
self.gridOptions = {
enableFiltering: true,
enableGridMenu: true,ang
enableColumnMenus: false,
enableRowSelection: true,
enableSelectAll: false,
multiSelect: false,
enableHorizontalScrollbar: 1,
columnDefs: [{
name: 'assetId'
}, {
name: 'reference',
enableHiding: false,
width: 250,
resizeable: true
}],
onRegisterApi: function(gridApi) {
self.assetGridObject = gridApi;
// register the onRowSelect Function here
//this.assetGridObject.selection.on.rowSelectionChanged(null, function(row) {
// if (row.isSelected)
// });
},
appScopeProvider: self
}
self.initGrid = function() {
self.assetListGrid = self.gridOptions;
self.assetListGrid.data = "controller.gridAssets"
}
self.loadInitData = function() {
self.gridAssets = [{
assetId: 1,
reference: "Dawood"
}, {
assetId: 2,
reference: "Dawood 2"
}, {
assetId: 3,
reference: "Dawood 3"
}, {
assetId: 4,
reference: "Dawood 4"
}, ]
}
self.refreshData = function() {
console.log("Data refresh")
self.gridAssets = [{
assetId: 1,
reference: "Refresh"
}, {
assetId: 2,
reference: "Refresh 2"
}, {
assetId: 3,
reference: "Refresh 3"
}, {
assetId: 4,
reference: "Refresh 4"
}, ]
}
self.initGrid();
self.loadInitData();
});
How do I keep the selection?
Seems like I have found a solution:
What I did was first I put the selected row inside a temporary object, when the row is selected
onRegisterApi: function(gridApi) {
self.assetGridObject = gridApi;
// register the onRowSelect Function here
self.assetGridObject.selection.on.rowSelectionChanged(null, function(row) {
if (row.isSelected) {
self.assetGridObject.grid.appScope.selectedRow = row.entity;
}
});
},
Then when the data is refreshed I am checking if a row is selected or not, if it a row is selected I am getting the latest value of that row from the Array of new data, this what the refresh data method looks like now:
self.refreshData = function() {
console.log("Data refresh")
self.gridAssets =
[
{assetId: 1,reference: "Refresh 1"},
{assetId: 2,reference: "Refresh 2"},
{assetId: 3,reference: "Refresh 3"},
{assetId: 4,reference: "Refresh 4"}];
if (self.selectedRow)
{
console.log("Row is selected");
//THIS LINE HERE I THINK IS THE KEY -
self.assetGridObject.grid.modifyRows(self.gridAssets);
// GET THE ROW FROM NEWLY LOADED DATA
var selectedRoww = null;
for (var i = 0; i < self.gridAssets.length; i++)
{
//COMPARING BASED ON MY asseId AS THIS IS THE VALUE THAT WILL NOT CHANGE IN MY GRID - OTHER COLUMS CAN CHANGE
if (self.gridAssets[i].assetId == self.selectedRow.assetId)
{
selectedRoww = self.gridAssets[i];
}
}
// THIS LINE HERE IS SELECTING THE ROW FROM THE GRID
self.assetGridObject.selection.selectRow(selectedRoww);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With