Keep selection after ui-grid data refresh

ε祈祈猫儿з 提交于 2019-12-10 14:32:39

问题


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?


回答1:


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;
      }
    });

  },

FIDDLE: http://jsfiddle.net/mdawood1991/02dpggyo/2/

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);

  }

}


来源:https://stackoverflow.com/questions/41033131/keep-selection-after-ui-grid-data-refresh

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