Angular Material Table: custom sort foler and file items

China☆狼群 提交于 2021-02-08 10:12:21

问题


I have different types of items in my mat-table: Files and Folders.
They have to get sorted like in Microsofts file-explorer. Folders can`t be separated from folders and likewise with files.
All other sorting rules staying the same.

Has anyone an idea how to solve this? Thank you in advance!


回答1:


You will have to overwrite the sortData on your MatTableDataSource attached to the table. This is the function that is responsible for sorting records, e.g.

this.dataSource.sortData = (data: YourObjectType[], sort: MatSort) => {
 return data.sort((a: YourObjectType, b: YourObjectType => {
   //Sorting logic here
 });
}

You can look at the default implementation in the material code on github: https://github.com/angular/components/blob/master/src/material/table/table-data-source.ts#L142



来源:https://stackoverflow.com/questions/61480695/angular-material-table-custom-sort-foler-and-file-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!