Angular:在agGrid中的动态列上设置默认排序的最佳方法 [英] Angular: Best way to set a default sort on a dynamic column in agGrid

查看:221
本文介绍了Angular:在agGrid中的动态列上设置默认排序的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在agGrid上工作,我们没有明确定义列.我一辈子都想不通如何在我的其中一列上设置默认排序.在初始化时,我们执行以下操作:

I'm working on an agGrid, where we don't explicitly define the columns. I can't for the life of me figure out how to set a default sort on one of my columns. On init we do this:

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

这是我最初要排序的 getDefaultColumnsDefinition()中的列之一.我尝试过

It's one of the columns in getDefaultColumnsDefinition() that I want to be sorted initially. I tried

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

但是它不起作用.网格看起来相同.有人可以帮忙吗?

but it doesn't work. The grid looks the same. Can anyone help?

推荐答案

根据您所说的,您是在初始化所有内容之前调用api的.我不确定100%如何进行此设置,但是您应该在代码的 onGridReady 函数中进行这些调整.在onGridReady中,您可以执行以下操作:

From what you have stated, you are calling the api before everything is initialized. I'm not 100% sure how you have this set up, but you should make these adjustments in the onGridReady function of your code. Within onGridReady you can do something similar to this:

HTML

<ag-grid-angular
    class="ag-theme-balham"
    [rowData]="data"
    [columnDefs]="columnDefs"
    (gridReady)="onGridReady($event)"></ag-grid-angular>

TypeScript

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

这将公开当前的gridApi,并允许您进行任何初始化后的修改.

This exposes the current gridApi and allows for you to make any post-initialization modifications.

这篇关于Angular:在agGrid中的动态列上设置默认排序的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆