如何在 Angular Grid 中禁用分组,如果没有要分组的内容?

huangapple go评论194阅读模式
英文:

How to disable grouping in the Angular Grid if there is nothing to group?

问题

以下是代码的翻译部分:

@Component({
  selector: 'my-app',
  template: 
  `<ag-grid-angular
    style="height: 100%;"
    class="ag-theme-alpine"
    [columnDefs]="columnDefs"
    [animateRows]="true"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)">
  </ag-grid-angular>`,
})
export class AppComponent {

  public columnDefs: ColDef[] = [
    { field: 'school', rowGroup: true, hide: true },
    { field: 'subject', rowGroup: true, hide: true },
    { field: 'branch' },
    { field: 'perfomance', aggFunc: 'avg' }
  ];

  public rowData: any[];

  onGridReady(params) {
    this.rowData = [
          {
            school: 'SomeSchool',
            subject: 'Music',
            branch: undefined,
            perfomance: 4
          },
          {
            school: 'SomeSchool',
            subject: 'Math',
            branch: 'Algebra',
            perfomance: 5
          },
          {
            school: 'SomeSchool',
            subject: 'Math',
            branch: 'Geometry',
            perfomance: 4.5
          }
        ];
  }
}

对于你的问题,如果你想要在"Music"这一行没有分组时禁用展开并仅显示它为"Music - Perfomance"一行,你可以考虑在数据中添加一个逻辑,检查是否有分支(branch)信息,如果没有,则在"subject"列中显示"Music - Perfomance",而不进行分组。这需要在数据准备阶段进行逻辑处理,具体实现可能需要根据你的应用程序逻辑来调整。

英文:

There is the following code:

@Component({
  selector: &#39;my-app&#39;,
  template: 
  `&lt;ag-grid-angular
    style=&quot;height: 100%;&quot;
    class=&quot;ag-theme-alpine&quot;
    [columnDefs]=&quot;columnDefs&quot;
    [animateRows]=&quot;true&quot;
    [rowData]=&quot;rowData&quot;
    (gridReady)=&quot;onGridReady($event)&quot;&gt;
  &lt;/ag-grid-angular&gt;`,
})
export class AppComponent {

  public columnDefs: ColDef[] = [
    { field: &#39;school&#39;, rowGroup: true, hide: true },
    { field: &#39;subject&#39;, rowGroup: true, hide: true },
    { field: &#39;branch&#39; },
    { field: &#39;perfomance&#39;, aggFunc: &#39;avg&#39; }
  ];

  public rowData: any[];

  onGridReady(params) {
    this.rowData = [
          {
            school: &#39;SomeSchool&#39;,
            subject: &#39;Music&#39;,
            branch: undefined,
            perfomance: 4
          },
          {
            school: &#39;SomeSchool&#39;,
            subject: &#39;Math&#39;,
            branch: &#39;Algebra&#39;,
            perfomance: 5
          },
          {
            school: &#39;SomeSchool&#39;,
            subject: &#39;Math&#39;,
            branch: &#39;Geometry&#39;,
            perfomance: 4.5
          }
        ];
  }
}

And such code is rendered to the grid so that if you open the Music item, it's just an empty string without branches, only with perfomance. And my question is, is there any way to make it so that if, as for Music, there is nothing to group, disable expanding for this row and show it only as Music - Perfomance in one row?

答案1

得分: 0

你查看过TreeData了吗?

https://www.ag-grid.com/angular-data-grid/tree-data/#file-browser-example

在示例中,temp.text文件与其他数据处于相同级别,但没有子项。然而,它仍然显示在其他列中的数据。

如何在 Angular Grid 中禁用分组,如果没有要分组的内容?

英文:

Have you looked into TreeData?

https://www.ag-grid.com/angular-data-grid/tree-data/#file-browser-example

See in the example the temp.text file is at the same level as the other data but has no children. However it still shows the data in other columns

如何在 Angular Grid 中禁用分组,如果没有要分组的内容?

答案2

得分: 0

根据文档,如果为autoGroupColumnDef定义了cellRendererSelector,则可以有条件地禁用单元格的分组。我已经这样设置,如果params.node.childrenMapped == null,我们将使用默认的单元格渲染器。

英文:

According to the documentation, you can conditionally disable grouping for a cell if you define cellRendererSelector for autoGroupColumnDef. I made it so that we use the default cell renderer if params.node.childrenMapped == null

huangapple
  • 本文由 发表于 2023年7月4日 22:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定