Angular:在 agGrid 中的动态列上设置默认排序的最佳方法

2024-04-29

我正在开发 agGrid,我们没有明确定义列。我一生都无法弄清楚如何在我的一列上设置默认排序。在初始化时我们这样做:

public ngOnInit(): void {
    this.gridOptions.defaultColDef = this.selectable ? this.getDefaultColumnsDefinition() : null;
    this.showSpinner = true;
    this.getAllRefreshJobs();
}

这是其中的一列getDefaultColumnsDefinition()我想首先进行排序。我试过

public onGridReady(params): void {
    this.gridApi = params.api;
    const sortModel = [
        {colId: 'recordStartTime', sort: 'desc'}
    ];
    this.gridApi.setSortModel(sortModel);
    this.gridApi.sizeColumnsToFit();
}

但它不起作用。网格看起来是一样的。有人可以帮忙吗?


感谢@Jon Black 让我开始!

当我得知后setSortModel已弃用,这对我有用:

function sortGrid(event, field, sortDir) {
  const columnState = { // https://www.ag-grid.com/javascript-grid-column-state/#column-state-interface
    state: [
      {
        colId: field,
        sort: sortDir
      }
    ]
  }
  event.columnApi.applyColumnState(columnState);
}

with

this.gridOptions = {
      defaultColDef: {
        sortable: true // enable sorting on all columns by default https://www.ag-grid.com/javascript-grid-sorting/
      },
      columnDefs,
      onGridReady: function (event) {
        console.log('The grid is now ready', event);
        sortGrid(event, 'timestamp', 'asc')
      },
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular:在 agGrid 中的动态列上设置默认排序的最佳方法 的相关文章

随机推荐