繁体   English   中英

Mat-selection-list 选择首先出现?

[英]Mat-selection-list selected coming first?

如果我有一个填充了 mat-list-option 的 mat-selection-list,我可以以某种方式将选定的值放在列表的顶部吗?

                       <mat-selection-list formControlName="mySubEntityIds">
                          <mat-list-option [value]="entity.id" *ngFor="let entity of entities" checkboxPosition="before">
                             {{entity.name}}
                          </mat-list-option>
                       </mat-selection-list>

本质上,您将单击的项目从 position 中删除,并将其放在实体数组的开头。

不确定您的列表是否是多选的,但这是快速解决方案 [假设:id 是唯一的]:

<mat-selection-list>
  <mat-list-option
    [value]="entity.id"
    *ngFor="let entity of entities"
    checkboxPosition="before"
    (click)="onSelected(entity.id)">
    {{ entity.name }}
  </mat-list-option>
</mat-selection-list>

在 ts 中:

onSelected(id){
    let removedItem;
    let removedIndex;
    this.entities.every( (element,i) => {
        if(element.id == id){
            removedItem = element;
            removedIndex = i;
            return false;
        }
        return true;
    });
    this.entities.splice(removedIndex,1);
    this.entities.unshift(removedItem);
}

Stackblitz 示例: https://stackblitz.com/edit/mat-selection-list-8zbx1j

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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