slickgridangular-slickgrid

AngularSlickGrid problems when change the columndefinition


I'm developing an app using the amazing angularslickgrid. Till now, i haven't got any problem, buy I found out an strange behaviour on it. In the ngOnInit I wrote the following code:

ngOnInit(){   
    this.defineGridHeaders();
    this.defineGridOptions();
    this.obtainData(); 
  }

Till this moment everything works well and the grid load correctly the data including the RowSelection column.

The problem is when I try to change the columndefinition, and perfrom a reset() like this:

    this.defineGridHeaders();
    this.angularGrid.gridService.resetGrid();

The new colums have been loaded correctly but i lose the rowSelection column.. :(

I've tried to include the defineGridOptions() function in the middle of the defineGridHeaders() and resetGrid() but the result is the same.

In the this.defineGridHeaders() I just perfom the following:

this.columnDefinitions = [];

[...FIELDS CREATION...]

      const col = {
        id,
        name,
        field,
        sortable,
        filterable,
        type,
        editor,
        formatter,
        filter,
        outputType,
        params,        
        minWidth: minwidth,
        width: width,
        header: header,
        excludeFromExport,
        excludeFromGridMenu,
        excludeFromQuery,
        excludeFromHeaderMenu
      };

this.columnDefinitions.push(col);

Could someone help me on this?

Many Thanks!


Solution

  • Please note that I'm the author of Angular-Slickgrid and you opened an issue with the same question on GitHub, I'll simply reply with the same answer here

    a reset is a reset, so it won't keep that. But I added the Row Selection in the Grid State not that long ago, so you could get it from there (just check the Grid State/Presets - Wiki. Dynamically adding a new column can be seen in this Example 3, you should look at the code on how to make it show up correctly (you can't just add it, you need to manually trigger a dirty change call, look at the example code for that)

    Also to add a bit more to this, SlickGrid Row Selection is by row index, it's not by row data. Meaning that if your data changes when you refresh or something and you keep the row selection it will not synched anymore... all that to say, just remember, it's a row index, so pay attention when you want to keep or want to reapply a row selection.

    If it's just to row selection that you lose, just save it before adding a column and put back the selection after adding the column. It's simple, get the row selection (with getSelectedRows) before you change the column definitions, add your new column and then put back the selection (with setSelectedRows).