I have a dropdown, on change of value in the dropdown I am loading the grid with new data & calling setGridOptions() which sets all the grid options again. I have enablecheckboxselector:true but then also checkboxes are not coming. Surprisingly, as soon as I click on any column to sort it, checkboxes are shown. Can anyone please suggest a solution. Thanks!
1 Answers
The answer is roughly the same as the other SO Question you asked here. So this seems like a duplicate and the answer was
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
this.gridObj = angularGrid.slickGrid;
}
changeGridOptions() {
const isReadyOnly = false; // whatever your code logic is here...
this.angularGrid.slickGrid.setOptions({ enableCheckboxSelector: isReadyOnly });
if (isReadOnly) {
// you also need to remove the column from your column definitions
// you can do that by using slice which will also trigger a dirty change to refresh the grid
this.columnDefinitions = this.columnDefinitions.slice(1); // return all columns without first one (without index 0)
}
}
If that doesn't work, you can try the other solution I answered in a similar question yesterday here, this piece from the answer (this is probably what will work for you if you add/remove the checkbox column dynamically, it's written in the code comment and for a full explanation then read the other SO here)
dynamicallyAddTitleHeader() {
//... add your new column
// NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
// you MUST use "getAllColumnDefinitions()" from the GridService, using this will be ALL columns including the 1st column that is created internally
// for example if you use the Checkbox Selector (row selection), you MUST use the code below
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
allColumns.push(newCol);
this.columnDefinitions = [...allColumns]; // (or use slice) reassign to column definitions for Angular to do dirty checking
}
Basically Angular-Slickgrid keeps an internal reference of the column definitions and their position, if you go directly with grid.setOptions
you're kind of bypassing Angular-Slickgrid and it is not aware of a column definitions change, you can force a sync with the 2 proposed piece of code written in this answer. What you have to know and remember is that Angular-Slickgrid is a wrapper on top of SlickGrid, if you talk directly to SlickGrid then in some cases (like this one) then Angular-Slickgrid isn't aware of the change.
There's also an event onSetOptions
in SlickGrid that could potentially be used to just re-sync the column definitions but I'm not sure I want to put that directly in the lib, it should be safe to use in your grid but I'm not sure of the consequence directly in the lib (I maybe don't want to reload the columns on every kind of grid option changes), something like this maybe (untested)
<angular-slickgrid
gridId="grid"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event)"
(sgOnSetOptions)="handleOnSetOptions($event.detail.eventData, $event.detail.args)"
</angular-slickgrid>
handleOnSetOptions(e, args) {
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
// (or use slice) reassign to column definitions for Angular to do dirty checking
this.columnDefinitions = [...allColumns];
}

- 12,308
- 6
- 69
- 112
-
1I tried to use above solution but sometimes this.angularGrid.gridService.getAllColumnDefinitions(); is returning all columns along with checkbox selector column & sometimes it is returning columns except checkbox selector. In that case how to push checkbox selector column explicitly? Also when I dont get checkbox selector column & I click on sort icon of any column then suddenly I get checkbox selector column also – aks1993 Mar 22 '21 at 16:21
-
if it doesn't show correctly it's because you're doing it too early and/or you're facing a racing condition. You can try adding a `setTimeout` to slow it down by at least a cycle. That is all the answers I can provide, it might not fix every use case, I never had (and hopefully never will) have such use case, personally I don't find this to be a good use case I prefer to go with `selectableOverride` as per this [wiki](https://github.com/ghiscoding/Angular-Slickgrid/wiki/Row-Selection#disable-custom-rows-selections-via-selectableoverride) – ghiscoding Mar 23 '21 at 12:52
-
yes, when I added settimeout() to this.angularGrid.slickGrid.setOptions({ enableCheckboxSelector: true }); then after few miliseconds checkboxes came but all other columns are disappearing (getting unchecked from gridmenu) – aks1993 Mar 24 '21 at 07:22
-
and then even if I select the columns from grid menu then checkboxes column hides again – aks1993 Mar 24 '21 at 10:15