如何使用复选框 angular 2 绑定数据?

how to bind data with checkbox angular 2?

JSON

[ {
  "code" : "TEST",
  "projectId" : "PROJECT",
  "name" : "TEST",
  "description" : "test",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : false
}, {
  "code" : "test",
  "projectId" : "PROJECT",
  "name" : "Test",
  "description" : "test 11",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : true
}, {
  "code" : "asdasd2122",
  "projectId" : "PROJECT",
  "name" : "TEST12",
  "description" : "ashdasdhl",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : false
}, {
  "code" : "test1a",
  "projectId" : "PROJECT",
  "name" : "TEST122",
  "description" : "a,sndkajlskdjlkajd",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : true
}, {
  "code" : "dasdasd",
  "projectId" : "PROJECT",
  "name" : "test123123",
  "description" : "asdasdasd",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : true
}, 
 {
  "code" : "asdasdTES",
  "projectId" : "PROJECT",
  "name" : "TEST123",
  "description" : "asdasdasd",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : true
} ]

HTML

            <mat-table [dataSource]="offersColumnRowData" matSort matSortActive="NAME">

          <ng-container matColumnDef="SELECT">
            <mat-header-cell *matHeaderCellDef> SELECT </mat-header-cell>
            <mat-cell *matCellDef="let element">
              <mat-checkbox>
              </mat-checkbox>
            </mat-cell>
          </ng-container>

          <ng-container matColumnDef="NAME">
            <mat-header-cell *matHeaderCellDef> NAME </mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.name }} </mat-cell>
          </ng-container>

          <ng-container matColumnDef="CODE">
            <mat-header-cell *matHeaderCellDef> CODE </mat-header-cell>
            <mat-cell *matCellDef="let element">{{ element.code }} </mat-cell>
          </ng-container>

          <ng-container matColumnDef="DESCRIPTION">
            <mat-header-cell *matHeaderCellDef> DESCRIPTION </mat-header-cell>
            <mat-cell *matCellDef="let element"> {{ element.description}} </mat-cell>
          </ng-container>

          <ng-container matColumnDef="CREATEDATE">
            <mat-header-cell *matHeaderCellDef> CREATEDATE </mat-header-cell>
            <mat-cell *matCellDef="let element"> </mat-cell>
          </ng-container>

          <ng-container matColumnDef="UNAME">
            <mat-header-cell *matHeaderCellDef> UNAME </mat-header-cell>
            <mat-cell *matCellDef="let element"> </mat-cell>
          </ng-container>

          <ng-container matColumnDef="ISACTIVE">
            <mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
            <mat-cell *matCellDef="let element">
              <mat-checkbox type="checkbox">
              </mat-checkbox>
            </mat-cell>
          </ng-container>

          <mat-header-row *matHeaderRowDef="offersColumns"></mat-header-row>
          <mat-row *matRowDef="let row; columns: offersColumns;" (mouseover)="row.hovered = true" (mouseout)="row.hovered = false"></mat-row>
        </mat-table>

我正在尝试将值绑定到复选框中!布尔值 (TRUE|FALSE)

我尝试绑定复选框,但它不起作用

说明:转到我的 stackblitz -> 如果我 select 来自 SELECT 列的任何复选框,代码列应该 selected(我只想打印 select 控制台日志中的列行代码)

2) 如何将布尔值绑定到列名称为 ISACTIVE 的复选框列,如果活动为 false,则复选框未 selected 如果 active 为 true -> 复选框为 selected!

这是我的 stackblits link 这对你来说很容易!

https://stackblitz.com/edit/angular-gbf9kz-a2s4zc?file=app%2Ftable-basic-example.ts

我更新了你的 stackblitz,检查:https://stackblitz.com/edit/angular-gbf9kz-qg9fvw?file=app%2Ftable-basic-example.ts

使用 [checked]="element.active"

在复选框 html 标记上绑定数据

使用 (change)="change(element)"

获取活跃的 属性 值

根据您要处理的复杂程度,您可以采用表单方法(推荐),也可以向您的复选框添加一个简单的更改处理程序。

组件

selectedCodes: string[] = [];

receiveCheckboxChange(code: string, value: MatCheckboxChange) {
   console.log(code, value.checked);
   this.updateCodes(code, value.checked);
}

updateCodes(code: string, selected: boolean) {
   const inArrayIndex: number = this.selectedCodes.indexOf(code);
   if (selected && inArrayIndex === -1) {
      this.selectedCodes.push(code);
   } else if (inArrayIndex !== -1) {
      this.selectedCodes.splice(inArrayIndex, 1);
   }
}

isSelected(code: string) {
    return this.selectedCodes.indexOf(code) !== -1;
}

模板

<ng-container matColumnDef="ISACTIVE">
    <mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
        <mat-cell *matCellDef="let element">
            <mat-checkbox type="checkbox" (change)="receiveCheckboxChange(element.code, $event)" [checked]="isSelected(element.code)">
            </mat-checkbox>
        </mat-cell>
</ng-container>

这里的关键部分是 (change)="receiveCheckboxChange(element.code, $event)" - 您可以从 element 中提供您想要的任何信息,或者实际上是将整个 element 对象本身提供给您的函数。然后就由你来做任何你想做的事了。

然后您将在变量 selectedCodes 中拥有一组选定代码。你在那里用它们做什么,完全取决于你。

闪电战:https://stackblitz.com/edit/angular-gbf9kz-sgtrtv

<ng-container matColumnDef="SELECT">
                <mat-header-cell *matHeaderCellDef> SELECT </mat-header-cell>
                <mat-cell *matCellDef="let element">
                  <mat-checkbox [(ngModel)]="element.Isselected" (change)="change()">
                  </mat-checkbox>
                </mat-cell>
              </ng-container>

在ts文件中

change() {
    const result = this.offersColumnRowData.filter(x => x.Isselected == true);

console.log('result', result);
  }

在 json

中选择了 属性
"Isselected": false,
  "code" : "TEST",
  "projectId" : "PROJECT",
  "name" : "TEST",
  "description" : "test",
  "level1" : "1 ",
  "level2" : "2 ",
  "level3" : "3",
  "level4" : "4",
  "level5" : "5",
  "active" : false

使用 [checked] 和(change)来显示是否 element.active=true 并更改 element.active 的值。并使用条件运算符显示(或不显示)element.code

的值
<ng-container matColumnDef="CODE">
  <mat-header-cell *matHeaderCellDef> CODE </mat-header-cell>
  <mat-cell *matCellDef="let element">
      {{ element.active?element.code:"" }} 
  </mat-cell>
</ng-container>
...
<ng-container matColumnDef="ISACTIVE">
   <mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
   <mat-cell *matCellDef="let element">
     <mat-checkbox type="checkbox" [checked]="element.active" 
                                   (change)="element.active=$event.checked">
     </mat-checkbox>
   </mat-cell>
</ng-container>

更新:更好地使用 ngModel -不[检查]和(更改)-

<ng-container matColumnDef="ISACTIVE">
    <mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
    <mat-cell *matCellDef="let element">
        <mat-checkbox type="checkbox" [(ngModel)]="element.active">
        </mat-checkbox>
    </mat-cell>
</ng-container>