当按钮单击某个特定行时,将使用angular slickgrid中的预选网格选项进行选择,但是如果这样做,则gridoption preseletedrow会更改,但不会显示。请帮助我
ts文件
arr1 = [1,2,8];
arr =[];
ngOnInit() {
this.onPageLoad();
this.dataset = [];
}
onPageLoad(){
this.gridOptions = {
enableAutoResize: true, // true by default
enableCellNavigation: true,
enableFiltering: true,
editable: true,
rowSelectionOptions: {
selectActiveRow: false,
},
preselectedRows:this.arr,
enableCheckboxSelector: true,
enableRowSelection: true,
};
}
check(){
this.gridOptions.preselectedRows =this.arr;
}
pre
前缀(presets
,preselectedRows
,...)开头的任何网格选项都应仅在构建网格时(第一页加载时)使用。要动态更改行选择,您将需要使用SlickGrid的其他功能,有两种方法可以实现,这两种方法非常相似。
同时提醒您,Angular-Slickgrid是SlickGrid的包装,它是核心库,要选择某些行,您需要从核心库中调用a方法。
[您首先需要获取AngularGridInstance
,然后从那里可以从setSelectedRows
调用GridService
方法(该方法公开了SlickGrid的某些方法)或直接从SlickGrid核心库中使用它。
<!-- Component View -->
<angular-slickgrid
gridId="grid4"
[columnDefinitions]="columnDefinitions"
[gridOptions]="gridOptions"
[dataset]="dataset"
(onAngularGridCreated)="angularGridReady($event)"> <!-- <<== you need this line !-->
</angular-slickgrid>
// Component
export class MyComponent {
angularGrid: AngularGridInstance;
columnDefinitions: Column[];
gridOptions: GridOption;
dataset: any[];
// as the name suggest, this instance will be available
// once the grid finished rendering and is ready
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
}
changeSelectionDynamically(rowIds: number[]) {
if (this.angularGrid) {
// you can call the method from the GridService
// I prefer to use this because it has the Types (TypeScript)
angularGrid.gridService.setSelectedRows(rowIds);
// OR call the method from the SlickGrid object (core lib instance)
angularGrid.slickGrid.setSelectedRows(rowIds);
}
}
所以从所示示例中,我应该选择哪一个?这完全取决于您,您可以使用其中一个,而结果将相同。我将某些方法复制到GridService
中的唯一原因仅仅是因为Angular-Slickgrid
是用TypeScript编写的,所以我们有类型检查符,而SlickGrid(核心库)是用纯JavaScript编写的,它不会如果您犯了一个错误,请不要抱怨。
最后,如果您希望在调用一组新的选择之前清除选择,则可以通过调用一个空数组来实现,如下所示。如果在进行新选择之前有任何选择,则必须执行此操作。
angularGrid.slickGrid.setSelectedRows([]); // first clear the selection
angularGrid.slickGrid.setSelectedRows(rowIds); // then set a new selection
也请注意,我是Angular-Slickgrid的作者,有关此答案的更多信息,您可以阅读SlickGrid & DataView objects Wiki。我写了很多Wiki,实际上您在Row Selection Wiki中以另一种方式回答了您的问题。