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

How to insert a new row in grid?

Open balakrishnan051 opened this issue 12 years ago • 17 comments

I just inserted a new row in grid but it always displayed as last row. here is the http://plnkr.co/edit/geHyRzsAzSGruVzuCi29?p=preview

balakrishnan051 avatar Oct 07 '14 06:10 balakrishnan051

Hi, is there any hack code for this problem? I tried refresh but nothing happen.

namminh2005 avatar Oct 08 '14 07:10 namminh2005

Most likely "hack" would be to shallow copy the data, then reassign it.

$scope.newData = angular.copy($scope.data);
$scope.data.slice ....... whereever you're adding the data
$scope.gridOptions.data = $scope.newData;

PaulL1 avatar Oct 08 '14 09:10 PaulL1

Thanks for your reply, Paul.

I tried it, but it only works if $scope.gridOptions.data is empty before assign $scope.newData.

Summary: [] -> Assigned['A', 'D', 'B', 'C'] -> Table ['A', 'D', 'B', 'C'] ['A', 'B', 'C'] -> Assigned ['A', 'D', 'B', 'C'] -> Table ['D', 'A', 'B', 'C']

I also tried:

$scope.gridOptions.data = [];
$timeout(function(){$scope.gridOptions.data = $scope.newData;});

It works but ... @@. Any advices?

Thanks a lot.

namminh2005 avatar Oct 08 '14 09:10 namminh2005

FYI, looking through the source, I figured that making this small change fixes this problem. Hopefully this will be included in the official release asap.

Grid.prototype.addRows = function addRows(newRawData,data) { var self = this;

var existingRowCount = self.rows.length;
for (var i=0; i < newRawData.length; i++) {
  var newRow = self.processRowBuilders(new GridRow(newRawData[i], i + existingRowCount, self));

  if (self.options.enableRowHashing) {
    var found = self.rowHashMap.get(newRow.entity);
    if (found) {
      found.row = newRow;
    }
  }

/// BEGIN OLD CODE //self.rows.push(newRow); /// END OLD CODE

/// BEGIN NEW CODE

  if (!data)
      self.rows.push(newRow);
  else {
      var index = data.indexOf(newRow.entity);
      if (index != -1) {
          self.rows.splice(index, 0, newRow);
      }
  }

/// END NEW CODE } };

balakrishnan051 avatar Oct 08 '14 16:10 balakrishnan051

Thanks balakishman, you saved my day :)

namminh2005 avatar Oct 09 '14 02:10 namminh2005

@balakrishnan051: Can i know what variable you passed as data parameter? Is that self.options.data?

I'm passing self.options.data but it works incorrectly in case data existed but data don't contains newRow. Ex: Load page1's data -> data assigned -> load page2's data -> blank grid.

Thanks a lot.

namminh2005 avatar Oct 09 '14 07:10 namminh2005

This is how I called addRows:

Grid.prototype.modifyRows = function modifyRows(newRawData) {

......

  var unfoundNew = (unfoundNewRowsToFind || newRawData);

  // Search for real new rows in `unfoundNew` and concat them onto `newRows`
  newRows = newRows.concat(self.newInN(self.rows, unfoundNew, 'entity'));

/// OLD CODE self.addRows(newRows); /// END

/// NEW CODE self.addRows(newRows, newRawData); /// END

balakrishnan051 avatar Oct 09 '14 13:10 balakrishnan051

I'm not completely sure, but it looks like this change would completely ignore all the row hashing setup that addRows() uses.

Have you compared before and after performance when adding large chunks of data? That's what the row hashing is intended to fix. Check this comment: https://github.com/angular-ui/ng-grid/issues/1029#issuecomment-41562620

c0bra avatar Oct 09 '14 13:10 c0bra

@c0bra : it's look like, it is working fine. Where do you using the hashing to improve the performance, so that i can debug there and come up with some performance numbers.

balakrishnan051 avatar Oct 09 '14 14:10 balakrishnan051

@PaulL1 your solution worked thanks!, but in a hope to understand the angular/grid runtime better, if you have some time, can you provide any insight why your 'hack' solution: angular.copy, unshift on the copy, then replace the whole array works vs gridOptions.data.unshift(row)?

Aren't there watchers on the data property that the grid would lose when I replace it with a new array in that manner?

And what watchers on the unshift operation would cause the grid to render below the rows already rendered?

JohnMorales avatar Oct 31 '14 16:10 JohnMorales

The watchers are set up so that they notice a change in the overall array, I think it's doing a watch collection. So it notices that there's a new row in the data, but the way the code works it first looks to see what rows have changed (removing any that no longer exist), then adds new rows. I think replacing the whole array causes it to remove all rows and then readd them, so you end up with things in the order of your original array.

There's a PR out there to align the data to the source data, it needs a couple of updates before being applied.

PaulL1 avatar Oct 31 '14 22:10 PaulL1

Thanks Paul, so it's not the watch per say but really it's the logic in the listener that would need to be adapted to support honoring the position the item was inserted into relative to the way the grid is showing the data.. that makes sense..

JohnMorales avatar Nov 01 '14 20:11 JohnMorales

@PaulL1 @JohnMorales can you clarify the hack? I don't get it. .slice....

I need to get around this issue too, and I don't wanna modify the ui-grid.js code.

pmariduena avatar Nov 19 '14 21:11 pmariduena

Refer this plunker - I think this "hack" is no longer necessary, we merged a change that puts the rows in the order of the data. http://plnkr.co/edit/fsDVpgtpufVNhsGklXeP?p=preview

You may need to build from source, this might not be in rc16 yet.

PaulL1 avatar Nov 19 '14 22:11 PaulL1

Closing as no response for some months, and believe this is now working.

PaulL1 avatar Jan 14 '15 22:01 PaulL1

I concur, it works for me now on 3.0.0-rc15

pmariduena avatar Jan 14 '15 22:01 pmariduena

PaulL1, I was goin through your plunker sample. If I am adding a new row and saving. Later, I want to apply an external save button, By which this newly added record is persisted to the DB. Then how about its refreshing the grid again. And how about the id Number generated.

nareshnraj avatar Apr 06 '16 07:04 nareshnraj