垫复选框,选中后,取消选择所有其余的

时间:2020-04-14 04:46:42

标签: html angular typescript angular-material

我对切换列表有独特的要求。

默认情况下,所有(4)个切换均为true。选择一个切换开关时,我希望所有其他切换开关都设置为false,而选定的切换开关为true。

然后,我希望能够选择其他真实的“添加”对象。

我的代码如下:

HTML

<mat-list class="list lt-checkbox" [ngClass]="{'selected-all': allTogglesSelected()}">
<mat-list-item>
  <app-icon-switch class="toggle-all" text="All Sydney" [subText]="" icon=""
    (checkedChange)="onAllChange($event)" [checked]="allTogglesSelected()">
  </app-icon-switch>
</mat-list-item>
<mat-list-item>
  <mat-checkbox [checked]="getFilterValue('Toggle_one')" (change)="onToggleChange('Toggle_one', $event)" color="primary"
    class="toggle">Toggle 1
  </mat-checkbox>
</mat-list-item>
<mat-list-item>
  <mat-checkbox [checked]="getFilterValue('Toggle_two')" (change)="onToggleChange('Toggle_two', $event)"
    color="primary" class="toggle">Toggle 2
  </mat-checkbox>
</mat-list-item>
<mat-list-item>
  <mat-checkbox [checked]="getFilterValue('Toggle_three')" (change)="onToggleChange('Toggle_three', $event)"
    color="primary" class="toggle">Toggle 3
  </mat-checkbox>
</mat-list-item>
<mat-list-item>
  <mat-checkbox [checked]="getFilterValue('Toggle_four')" (change)="onToggleChange('Toggle_four', $event)" color="primary"
    class="toggle">Toggle 4
  </mat-checkbox>
</mat-list-item>

切换TS

private toggleRegions = ['Toggle_one', 'Toggle_two', 'Toggle_three', 'Toggle_four'];

  onToggleChange(toggleState: string, value: any) {
    this.toggleFilters[toggleState] = value.checked;
    this.saveInStore();
  }

  onAllChange(value: boolean) {
    const hasChanged = this.toggleRegions.some(r => this.toggleFilters[r] !== value);
    if (hasChanged) {
      this.toggleRegions.forEach(r => (this.toggleFilters[r] = value));
      this.saveInStore();
    }
  }

  saveInStore() {
    this.store.dispatch(UpdateToggleFilter({ toggleFilters: this.toggleFilters }));
  }

  getFilterValue(filter: string): boolean {
    return this.toggleFilters[filter];
  }

  allSydneySelected(): boolean {
    return this.toggleRegions.findIndex(r => !this.toggleFilters[r]) === -1;
  }

出于隐私原因,我不得不更改某些名称。基本上,它目前可以正常工作,因为单击切换将取消选择它,并保持其他选择,我希望相反。

1 个答案:

答案 0 :(得分:1)

因此,似乎您没有使用Angular强大的更改检测功能来捕获所有这些值更改。这就是我要做的:

在组件中(请添加您自己的类型):

firstToggle: boolean = true;
listItems = [
    {
        id: 'Toggle_one',
        checked: true,
        text: 'Toggle 1'
    },
    {
        id: 'Toggle_two',
        checked: true,
        text: 'Toggle 2'
    },
    {
        id: 'Toggle_three',
        checked: true,
        text: 'Toggle 3'
    },
    {
        id: 'Toggle_four',
        checked: true,
        text: 'Toggle 4'
    },
]

onToggleChange(id: string, value: any) {
    this.listItems = this.listItems.map(item => {
        if (item.id === id) {
            item.checked = value.checked;
        } else {
            if (this.firstToggle) {
                item.checked = false;
            }
        }
        return item;
    })

    if (this.firstToggle) {
        this.firstToggle = false;
    }

    this.saveInStore();
}

onAllChange(value: boolean) {
    this.listItems = this.listItems.map(item => {
        item.checked = value;
        return item;
    })
    this.saveInStore();
}

saveInStore() {
    this.store.dispatch(UpdateToggleFilter({ toggleFilters: this.toggleFilters }));
}

在HTML中:

<mat-list class="list lt-checkbox" [ngClass]="{'selected-all': allTogglesSelected()}">
    <mat-list-item>
        <app-icon-switch class="toggle-all" text="All Sydney" [subText]="" icon="" (checkedChange)="onAllChange($event)"
            [checked]="allTogglesSelected()">
        </app-icon-switch>
    </mat-list-item>
    <mat-list-item *ngFor="let item of listItems">
        <mat-checkbox [checked]="item.checked" (change)="onToggleChange(item.id, $event)" color="primary"
            class="toggle">{{item.text}}
        </mat-checkbox>
    </mat-list-item>
</mat-list>

希望这会有所帮助! :)