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

If the options of ng-grid changes, ng-grid will not apply it

Open freewind opened this issue 13 years ago • 27 comments

I want to configure the ng-grid on runtime, but it won't apply to ng-grid.

See this demo: http://plnkr.co/edit/Dk2eFA?p=preview

In the modal, when I click the change grid options button, I replaced original data source, but nothing happens.

And also some other options e.g. columnDefs, I hope I can modify it and reflect it to ng-grid immediatly, because it's useful for a "grid designer" with ng-grid.

freewind avatar Feb 04 '13 15:02 freewind

yeah, the grid options is not meant to be overridden. You can do it this way: http://plnkr.co/edit/YvFdHR?p=preview

    $scope.changeGridOptions = function() {
      $scope.myData = $scope.myData2;
    }

orneryd avatar Feb 05 '13 00:02 orneryd

Is there any way to apply a changed grid options? I really need it.

I provide a simple ui designer to let user configure the columns, the gridOptions.columnDefs will change, but it can't be reflected to the grid

freewind avatar Feb 05 '13 05:02 freewind

You can by putting the column definitions on the scope and pass the string name of the variable like this:

columnDefs: 'myDefs'

If you reassign the definitions to a new coldef array then the grid will update.

orneryd avatar Feb 05 '13 05:02 orneryd

Just don't reassign the gridOptions and you'll be fine, reassign the references that you assign to the options instead.

orneryd avatar Feb 05 '13 05:02 orneryd

Thank you, let me try

freewind avatar Feb 05 '13 05:02 freewind

I defined a columnDefs for $scope:

   $scope.columnDefs = [{
        field: 'title',
        displayName: 'YYY'
    }];
    $scope.gridOptions = {
        data: dataName,
        columnDefs:  'columnDefs'
    }

Later I changed the $scope.columnDefs:

$scope.columnDefs.push({
field: 'name',
displayName: 'NNN'
});

But the grid doesn't add the new column.

freewind avatar Feb 05 '13 06:02 freewind

Don't push, reassign the entire array. Push/pop/splice is currently not supported on the columndefs. We may add that here soon.

On Monday, February 4, 2013, freewind wrote:

I defined a columnDefs for $scope:

$scope.columnDefs = [{ field: 'title', displayName: 'YYY' }]; $scope.gridOptions = { data: dataName, columnDefs: 'columnDefs' }

Later I changed the $scope.columnDefs:

$scope.columnDefs.push({ field: 'name', displayName: 'NNN' });

But the grid doesn't add the new column.

— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ng-grid/issues/128#issuecomment-13116222.

-Tim Sweet

orneryd avatar Feb 05 '13 06:02 orneryd

It works now, thank you so much!

freewind avatar Feb 05 '13 06:02 freewind

There is a very easy solution to support push/pop/splice.

Find:

if (typeof options.columnDefs == "string") {
    $scope.$parent.$watch(options.columnDefs, function(a) {
        $scope.columns = [];
        grid.config.columnDefs = a;
        grid.buildColumns();
        grid.eventProvider.assignEvents();
        domUtilityService.RebuildGrid($scope,grid);
    });
} else {
    grid.buildColumns();
}

Add a true to the $watch as the 3rd parameter:

    $scope.$parent.$watch(options.columnDefs, function(a) {
        $scope.columns = [];
        grid.config.columnDefs = a;
        grid.buildColumns();
        grid.eventProvider.assignEvents();
        domUtilityService.RebuildGrid($scope,grid);
    }, true);

Angular will deep watch the array and the item's properties. Is it available?

freewind avatar Feb 05 '13 16:02 freewind

I'm afraid you missed my last comment :)

freewind avatar Feb 06 '13 03:02 freewind

@timothyswt I think push/pop/splice support would be a really nice addition to have... Perhaps it should also be made clear in the documentation that it isn't supported currently, as it's only after an hour of me scratching my head that I've found this!

Siyfion avatar Feb 21 '13 15:02 Siyfion

+1 for dynamic grid configuration. I know the columnDefs and data properties can be reassigned (in a bit of a strange way). But there are many other properties that I need to rconfigure (eg. rowTemplate) and I can find no way of doing so.

Essentially, I would like to be able to pass a different configuration variable name to the ng-grid directive and have it update its layout accordingly. Or even to assign a different config object in the controller. At present it seems that neither option works.

dotcom9 avatar Aug 23 '13 14:08 dotcom9

Hi Tim and thanks for your excellent work on this plugin!

It might be worth pointing out that when programatically creating columnDefs for a grid with enableColumnResize set to true you also need to add the index and width attributes to the column definition. Otherwise if you change the width of a column and then reload the columnDefs no data will be displayed.

In the @freewind's example above this would mean adding the following:

columnDefs.push({
    field: 'name',
    displayName: 'NNN',
    index: columnIndex,   // where columnIndex is 0, 1, 2, 3, etc.
    width: "*"
});

$scope.myColumnDefs = columnDefs;

Hope this helps other folks who stumble upon this issue :)

jan-molak avatar Nov 17 '13 15:11 jan-molak

@jan-molak Could you provide a plunker of your fix in action? I've tried the following:

$scope.columns = [
    {field: "name", displayName: "name"},
    {field: "age", displayName: "age"}
];

$scope.collectionGridOptions = {
    data: 'myData',
    columnDefs: 'columns',
    enableColumnResize: true
};

$scope.addColumn = function(newColumn) {
    newColumns = $scope.columns;

    var columnObj = {
        field: newColumn, 
        displayName: newColumn,
        index: $scope.columns.length,
        width: '*'
    };

    newColumns.push(columnObj);

    $scope.columns = newColumns;
}

Where the newColumn variable is a string submitted by a form. But that still clears out the grid after resizing. I also noticed that when I try to define the index for a column on start-up, like so:

$scope.columns = [
    {field: "name", displayName: "name", index: 1},
    {field: "age", displayName: "age", index: 0}
];

The order of the columns is unaffected. Any idea what I'm doing wrong?

clarkbynum avatar Feb 28 '14 22:02 clarkbynum

@sh3nan1gans, I have an example project that uses the approach I mentioned: https://github.com/jan-molak/data-grid/blob/master/src/scripts/controllers/preview.js - controller https://github.com/jan-molak/data-grid/blob/master/src/scripts/services/dataGridExpert.js - column defs

Hope this helps! J

jan-molak avatar Mar 01 '14 13:03 jan-molak

Hi , I'm just trying to follow the suggestions in this thread and starting with an empty array as columnDefs is giving me several console errors and finally the data doesn't load up. Here's my Plunkr:

http://plnkr.co/edit/CoFBNvIsMF40MfCemqBP?p=preview

Can any of you check and tell me if I'm missing something?

@sh3nan1gans :Were you able to resolve your issue? I'm facing something similar. The grid just clears out.

AA33 avatar Oct 24 '14 05:10 AA33

@AA33 I was able to resolve my issue. Can you give me a more in-depth description of your problem? Is the grid getting cleared out after a new column is added?

clarkbynum avatar Oct 26 '14 17:10 clarkbynum

@sh3nan1gans : I realise I may not be on the right forum as I'm using ui-grid, which as said is a rewrite of ng-grid, not sure if all ng-grid features copy over to that too. Anyways, here's what I'm trying to do. If you look at my Plunkr, the footer cells show popovers on hover. I'm trying to get the filter functionality on the grid on the popovers. That means modifying the colDef for that particular column and applying that to the whole grid. That part doesn't work. The relevant code is in the handleMinMaxChange function in the advancedGadget directive in app.js.

Here's the plunkr: http://plnkr.co/edit/CoFBNvIsMF40MfCemqBP?p=info

AA33 avatar Oct 27 '14 19:10 AA33

@AA33 I'm running into the same issue, also using ui-grid.

@sh3nan1gans When I update the $scope.myData variable ( to an entirely different set of data, with completely different columns ) , the table becomes empty. I'm unsure how to solve the issue, I've considered putting the grid-controller div into a template and re-rendering that template.

juliusakula avatar Dec 05 '14 22:12 juliusakula

Hi @freewind @timothyswt , I have ran into a similar issue with ng-grid where I want to update my column headers dynamically based on the user click. It works when I use a normal table with ng-repeat="heading in gridHeaders">{{ heading }}, but when I include ng-grid the grid cells becomes empty and the columns headers remain static.

Here is the JS Fiddle http://jsfiddle.net/masoom/svcxh6hu/7/

My problem on stackoverflow - http://stackoverflow.com/questions/29165191/ng-grid-angularjs-how-to-update-the-column-headers-in-ng-grid

masoom avatar Mar 20 '15 13:03 masoom