mat-selection-list with search filter not keeping selections after a search

纵然是瞬间 提交于 2020-02-06 04:36:47

问题


I'm attempting to implement a mat-selection-list to have a list of characters--but at the top, you can search/filter characters. This is working except when you select a character, search for another character, then stop searching--your original selection is visible in the UI, but the model is cleared until you make another selection.

Example of what I mean: https://stackblitz.com/edit/angular-yo2o9o

I can come up with a way to fix this--by manually pushing/removing from an array; but this doesn't keep the order selected in the list and that's important.

Any ideas how this can be solved without some gross workaround?


回答1:


Don't use ngModel and ngModelChange - use selectionChange and manage the model yourself. To manage the ordering problem, just filter the original array instead of pushing/removing:

<mat-selection-list #heroes
        (selectionChange)="onSelectedOptionsChange()"
        [disableRipple]="true">
    <mat-list-option *ngFor="let hero of (overwatchHeroes | heroSearch:heroSearch.value)"
            (click)="hero.selected = !hero.selected;"
            [selected]="hero.selected"
            [value]="hero">
        {{hero.name}} - {{hero.selected}}
    </mat-list-option>
</mat-selection-list>

public onSelectedOptionsChange() {
    setTimeout(() => {
        this.selectedLongListHeroes = this.overwatchHeroes.filter(hero => {
            return hero.selected; 
        });
    })
}


来源:https://stackoverflow.com/questions/50744023/mat-selection-list-with-search-filter-not-keeping-selections-after-a-search

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