Column width doesn't change
var columnDefs1 = [ { name: 'firstName', width: 100 } ]; var columnDefs2 = [ { name: 'firstName', width: 200 } ]; Switching beetween the 2 definitions for the grid the width for the "name" column doesn't change. here is the plnkr: http://plnkr.co/edit/Q3BYbb3fIHVgDVkKF4Tl?p=preview Please hit swap data and you will notice that the gender company is changed to employed, but the column width is the same.
This is probably another example of a problem we have where we don't detect that the column has changed, so we don't re-copy the columnDef down (we assume the user might have changed the width manually, maybe using resizeColumns, so we don't overwrite).
One workaround is to empty the columnDefs, wait for a digest, then put the new ones in. http://plnkr.co/edit/hYGggNUfMKZtYH98R3Rc?p=preview
I have implemented the workaround that you suggested but there's a pretty ugly glitch. http://screencast.com/t/iUjXNtysAb Here is an example with a simple one, if i have more columns the glitch is even uglier. I wonder if there isn't a "flag" that we could change when we modify the column definition so that the grid could target the column definition change and update to the latest version ? Thanks
Alternatively, you can directly set the column.width (rather than columnDef.width), that will apply immediately.
Yes, this is a workaround that solves the width problem. But there are a few more problems as it would be the column order, the column sort direction. The main issue i think is that the grid doesn't detect the columnDef change.
columnDef changes can be notified through notifyDataChange.
I just tried using the notifyDataChange with both uiGridConstants.dataChange.COLUMN and uiGridConstants.dataChange.ALL and I can confirm that it doesn't work for the setup that we have. It doesn't change the following aspects (at least): column widths and pinning of the column.
By design neither column widths nor pinning gets overwritten from the columnDefs onto the columns once the user has had a chance to alter them. We run a grid refresh in lots of places, including sometimes during scrolling, column moves, and a bunch of other actions. If each of those actions reset the column width to the default (perhaps after the user has already changed it to suit themselves) it would be very annoying for them.
For pinning I think there is an API you can use to pin columns programatically, for column width you need to set the column.width directly.
I understand that you cannot just refresh the grid at any point, that's very good. What we are trying to do is to replace the columns definitions completely, including grid data. The problem is that usually, the columns definitions are similar with a few small exceptions: some columns might not be present, some might have their order switched, some might have another sorting direction set or some may be pinned. We can do a complete clean-up of the columns definitions but that results in an ugly redrawing time when switching between definitions. Without that redraw, everything looks fine, some aspects of the grid are changed like hidden or visible columns, order inside the column. But some of the aspects like column width and pinning don't get changed.
It should notice the change so long as the objects change - at present the columnDef watch is implemented as follows in ui-grid.js:
deregFunctions.push( $scope.$parent.$watchCollection(function() { return $scope.uiGrid.columnDefs; }, columnDefsWatchFunction) );
So it should notice any columnDef whose reference changes.
The columnDefsWatchFunction in turn is:
function columnDefsWatchFunction(n, o) {
if (n && n !== o) {
self.grid.options.columnDefs = n;
self.grid.buildColumns({ orderByColumnDefs: true })
.then(function(){
self.grid.preCompileCellTemplates();
self.grid.callDataChangeCallbacks(uiGridConstants.dataChange.COLUMN);
});
}
}
So that's going to call buildColumns.
That in turn is in Grid.js, and it looks to match columnDefs by name.
So, if I wanted to have a decent go at the grid noticing my columnDefs changes, and giving me new columns with new configuration and dropping my old ones, I'd:
- Make all my columnDefs be new objects - so if you're using the old ones but tweaking them, I'd angular.copy them first - something like
var newColumnDefs = angular.copy($scope.gridOptions.columnDefs);
$scope.gridOptions.columnDefs = newColumnDefs;
- Give them all new names. You can keep the old fields, and keep the old displayNames, but give the columns themselves new names and the grid will think they're new columns.
Thanks for the suggestions Paul. We do reset the columnDefs array completely each time we do this:
scope.gridOptions.columnDefs = [];
And then we start pushing into this new array the new columns based on our definition for the columns. Somehow this does not trigger the proper watchers. We do not change the names for the columns though, it would not be possible to correlate with the data if we do that.
Sorry about the confusion in the previous comment by me. I just noticed that we do have a field property which connects the column with the data, and if we set that it won't rely on "name" anymore. I did the changes and it appears to work, at least for the preliminary tests. Thank you very much for the suggestion with changing the names of the columns. That was the only thing that did the trick. I am wondering though, if you do have a watch on the collection, and then another on each column, why wouldn't one of them get triggered? Is 'name' a reserved field somehow in angular or in ui-grid and it it is the same the changes are ignored ?
The watch gets triggered, but then our buildColumns function will iterate through each columnDef and see if we already have a column with that name. If we do, then we tie the columnDef to that column. It's so that people can go replacing their columnDefs array with a new copy of it (which people seem to do all the time) and the grid will work out that they're the same columns and link them, rather than replacing them all. Which is exactly what it was doing to you.
This kind of behavior would make sense for us too, with the exception that the columns definitions can contain changes for us, for the columns with the same name. The name changing seems to do the trick. The initial suggestion for resetting the columns and waiting for a digest was producing very ugly rendering results. We now reset the columns but we build them back immediately, so no digest in between, and the redraw process seems to be very friendly looking.
Thank you again for your suggestions and continuous help and support!
Sounds like this is resolved, closing.