简体   繁体   中英

ag-grid column row grouping on dropdown selection

I am using ag-grid library in my angular 7 project for creating grid and it has one property in its <ag-grid-angular [rowGroupPanelShow]="rowGroupPanelShow"></ag-grid-angular> selector which is rowGroupPanelShow as mentioned above.

By using this property ag-grid enable drag and drop option for the user and by just dragging and dropping any column ag-grid groups all the rows by that particular column.

But I don't want to drag and drop the column each time I use to group my rows with that particular column. I want to perform all this stuff by using a simple dropdown where dropdown will contain values similar to that of columns in the grid and when user will select any value from dropdown, the ag-grid should group all rows matching with that selected value in ag-grid columns.

I am struggling with issue from last 10 hours. I have searched a lot in ag-grid official site as well. But I didn't got any solution of my issue.

The approach that I am following right now can be seen in the image attached with this post.

app.component.ts第1部分 app.component.ts第二部分 app.component.html 所需的输出图像

I suppose you missed the grid-column-API documentation

Methods for management of column row groups: getRowGroupColumns() , addRowGroupColumn(colKey) , addRowGroupColumns(colKeys) , removeRowGroupColumn(colKey) , removeRowGroupColumns(colKeys) , setRowGroupColumns(colKeys) , moveRowGroupColumn(fromIndex, toIndex)

So to achieve drop-down scenario you have to take care to add and remove grouping on drop-down changes. (*might be not, but let's do it like that)

<select (change)="onChange($event.target.value)">
    <option value="null">Select group</option>
    <option value="year">Year</option>
    <option value="country">Country</option>
</select>

onChange(value){
  this.gridColumnApi.getRowGroupColumns().forEach(i=>{
    this.gridColumnApi.removeRowGroupColumn(i.colId);
  })
  this.gridColumnApi.addRowGroupColumn(value);
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM