【问题标题】:material angular select all checkbox材料角度全选复选框
【发布时间】:2019-06-02 01:39:14
【问题描述】:

我正在尝试在角度材料上实现全选复选框。当用户点击特定的复选框(项目)时,主复选框应显示 Indeterminate,如果所有复选框都被选中,则转为 checked。目前我有一些奇怪的行为。谁能让我知道我在哪里犯了错误?这里是stackblitz。 这是我的示例代码:

app.html

<fieldset class="demo-fieldset">
  <div>
    <mat-checkbox aria-label="Select All" [checked]="isChecked(selected3, itemsObject)" [indeterminate]="isIndeterminate(selected3, itemsObject)" (click)="toggleAll(selected3, itemsObject)">
      Select All list of user (Array of objects) {{isChecked(selected3, itemsObject)}}
    </mat-checkbox>
  </div>
  <div class="demo-select-all-checkboxes" *ngFor="let item of itemsObject">
    <mat-checkbox [checked]="exists(item, selected3)" (click)="toggle(item, selected3)">
      {{ item.val }}
    </mat-checkbox>
    {{exists(item, selected3)}}

  </div>
</fieldset>

app.ts

import {
  Component
} from '@angular/core';

@Component({
  selector: 'app',
  templateUrl: 'app.html',
  styleUrls: ['app.css'],
})
export class CheckboxConfigurableExample {
  itemsObject = [{
    id: 1,
    val: 'john'
  }, {
    id: 2,
    val: 'jane'
  }];
  selected3 = [];

  toggle(item, list) {
    var idx = list.indexOf(item);
    if (idx > -1) {
      list.splice(idx, 1);
    } else {
      list.push(item);
    }
  }

  exists(item, list) {
    return list.indexOf(item) > -1;
  };

  isIndeterminate(x, t) {
    return (x.length !== 0 && x.length !== t.length);
  };

  isChecked(x, t) {
    return x.length === t.length;
  };

  toggleAll(x, t) {
    var l1 = x.length,
      l2 = t.length;
    if (l1 === l2) {
      x.splice(0, l1);
    } else if (l1 === 0 || l1 > 0) {
      //First we need to empty array, because we are using push to fill in array
      x.splice(0, l2);
      t.forEach(y => x.push(y));
    }
  };
}

这是我的stackblitz

【问题讨论】:

    标签: angular checkbox angular-material


    【解决方案1】:

    试试这个代码:

    组件:

    import {Component} from '@angular/core';
    import {
    
      MatCheckboxChange
    } from '@angular/material';
    
    /**
     * @title Configurable checkbox
     */
    @Component({
      selector: 'checkbox-configurable-example',
      templateUrl: 'checkbox-configurable-example.html',
      styleUrls: ['checkbox-configurable-example.css'],
    })
    export class CheckboxConfigurableExample {
      itemsObject = [{
        id: 1,
        val: 'john'
      }, {
        id: 2,
        val: 'jane'
      }];
      selected3 = [];
    
      toggle(item,event: MatCheckboxChange) {
         if (event.checked) {
          this.selected3.push(item);
        } else {
          const index = this.selected3.indexOf(item);
          if (index >= 0) {
            this.selected3.splice(index, 1);
          }
        }
       console.log(item + "<>", event.checked);
      }
    
      exists(item) {
        return this.selected3.indexOf(item) > -1;
      };
    
      isIndeterminate() {
        return (this.selected3.length > 0 && !this.isChecked());
      };
    
      isChecked() {
        return this.selected3.length === this.itemsObject.length;
      };
    
    
    
      toggleAll(event: MatCheckboxChange) { 
    
        if ( event.checked ) {
    
           this.itemsObject.forEach(row => {
              // console.log('checked row', row);
              this.selected3.push(row)
              });
    
            // console.log('checked here');
        } else {
          // console.log('checked false');
           this.selected3.length = 0 ;
        }
    }
    }
    

    模板:

    <fieldset class="demo-fieldset">
      <div>
         <mat-checkbox aria-label="Select All" [checked]="isChecked()" [indeterminate]="isIndeterminate()" (change)="$event ? toggleAll($event) : null">
          Select All list of user (Array of objects) {{isChecked(selected3)}}
        </mat-checkbox>
      </div>
      <div class="demo-select-all-checkboxes" *ngFor="let item of itemsObject">
        <mat-checkbox (click)="$event.stopPropagation()"
                        (change)="$event ? toggle(item, $event) : null"
                        [checked]="exists(item)">
          {{ item.val }}
        </mat-checkbox>
        {{exists(item)}}
    
      </div>
    </fieldset>
    

    【讨论】:

    【解决方案2】:

    在您的 Html 中,添加如下内容:

    <table class="table">
                  <thead>
                    <tr>
                      <th>
                        <mat-checkbox (change)="onChangeSelectAll($event)">Select/Deselect All</mat-checkbox>
                      </th>
    
                    </tr>
                  </thead>
                  <tbody>
                    <tr *ngFor="let item of offers">
                      <td>
                        <mat-checkbox [checked]="item.selected" (change)="selection($event, i, item)" name="chkInvoice"></mat-checkbox>
                      </td>                      
                    </tr>
                  </tbody>
                </table>
    

    在您的打字稿文件上,

    onChangeSelectAll(event) {
        if (event.checked) {
            this.offers.forEach(obj => {
                obj.selected = true;
            });
        }
        else {
            this.offers.forEach(obj => {
                obj.selected = false;
            });
        }
    }
    

    【讨论】:

      【解决方案3】:

      另一种策略可能是绑定到对象和组件上的值,而不是调用方法。这样,您可以更有效地管理组件中的状态。

      例如,您可以在对象模型中引入以下内容:

      public itemsObject = [{
        id: 1,
        val: 'john',
        isChecked: false
      }, {
        id: 2,
        val: 'jane',
        isChecked: false
      }];
      

      然后您可以使用以下方法将其绑定到复选框:

      [checked]="item.isChecked"
      

      绑定“change”事件也会让你知道什么时候发生了变化,然后你可以采取相应的行动:

      <mat-checkbox [checked]="item.isChecked" (change)="itemChanged(item,$event)">
      

      我创建了一个 Stackblitz,它展示了一个工作示例:-

      https://stackblitz.com/edit/angular-uuw7qh-ninwen

      【讨论】:

        【解决方案4】:

        在您使用的 checkbox-configurable-example.html 中:

        [checked] = "isChecked(selected3, itemsObject)"
        

        我只是将其更改为:

        value="isChecked(selected3, itemsObject)"
        

        它看起来和你期望的一样? 我怀疑整个项目需要进行更多调整,但这可能会让您找到方向?

        【讨论】:

        • 我看到这会产生其他问题。
        猜你喜欢
        • 2016-01-23
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-25
        • 2016-08-30
        • 2018-06-22
        • 2020-02-06
        相关资源
        最近更新 更多