获取有角质数中的多个复选框的值

时间:2019-05-25 12:42:19

标签: angular primeng

  • 想要从多个复选框中获取值。
  • 每行包括用户名和授予用户的权限,即读,写。
  • 在下面的以下代码中,我试图将权限授予用户,即读取,写入。
  • 对于UI,我正在使用primeng复选框。
  • 检查读取的值是否需要添加到用户数组中。

enter image description here

app.component.ts

import { Component, OnInit } from '@angular/core';
import { supportsWebAnimations } from '@angular/animations/browser/src/render/web_animations/web_animations_driver';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  users: Array<any>;
  permissions: Array<any> = [];


  ngOnInit(){

    this.users = [
      {id: 1, name: 'Sam', permission: []},
      {id: 2, name: 'Adam', permission: []},
      {id: 3, name: 'Chris', permission: []}
    ]


  }
}

app.component.html

<h3 class="first">Permission</h3>
<div class="ui-g" style="margin-bottom:10px; ">
    <div  class="ui-g-2"><b>Name</b></div>
    <div  class="ui-g-2"><b>Read</b></div>
    <div  class="ui-g-2"><b>Write</b></div>
</div>
<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
    <div  class="ui-g-2">{{user.name}}</div>
    <div  class="ui-g-2"><p-checkbox name="group[i]" value="Read"  [(ngModel)]="permissions[i]" inputId="R"></p-checkbox></div> 
    <div  class="ui-g-2"><p-checkbox name="group[i]" value="Write"  [(ngModel)]="permissions[i]" inputId="W"></p-checkbox></div>
</div>

2 个答案:

答案 0 :(得分:1)

代替维护permission数组,您可以维护permission对象并将其绑定到您的复选框。

在这种情况下,无需维护索引。

您的代码将如下所示:

import { Component, OnInit } from '@angular/core';
import { supportsWebAnimations } from '@angular/animations/browser/src/render/web_animations/web_animations_driver';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  users: Array<any>;
  permissions: Array<any> = [];


  ngOnInit(){

    this.users = [
      {id: 1, name: 'Sam', permission: { read: true, write: true}},
      {id: 2, name: 'Adam', permission: { read: false, write: true}},
      {id: 3, name: 'Chris', permission: { write: true, read: false}}
    ]
  }

} 

还有您的HTML:

<h3 class="first">Permission</h3>
<div class="ui-g" style="margin-bottom:10px; ">
    <div  class="ui-g-2"><b>Name</b></div>
    <div  class="ui-g-2"><b>Read</b></div>
    <div  class="ui-g-2"><b>Write</b></div>
</div>
<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
    <div  class="ui-g-2">{{user.name}}</div>
    <div  class="ui-g-2"><p-checkbox type="checkbox" name="group[i]" value="Read"  [(ngModel)]="user.permission.read" inputId="R"></p-checkbox></div> 
    <div  class="ui-g-2"><p-checkbox type="checkbox" name="group[i]" value="Write"  [(ngModel)]="user.permission.write" inputId="W"></p-checkbox></div>
</div>

并使用 PrimeNG

您需要添加

  

binary =“ true”

归因于您的复选框

您可以在此处看到demo

答案 1 :(得分:0)

您必须在模型中具有那些读/写标志,并将它们连接到正确的复选框。例如

this.users = [
  {id: 1, name: 'Sam', permission: {read:false,write:false}},
  {id: 2, name: 'Adam', permission: {read:false,write:false}},
  {id: 3, name: 'Chris', permission: {read:false,write:false}}
]

<div class="ui-g" style="margin-bottom:10px;" *ngFor ="let user of users; let i = index">
    <div  class="ui-g-2">{{user.name}}</div>
    <div  class="ui-g-2"><p-checkbox  [(ngModel)]="user.permissions.read" inputId="R"></p-checkbox></div> 
    <div  class="ui-g-2"><p-checkbox  [(ngModel)]="user.permissions.write" inputId="W"></p-checkbox></div>
</div>
模型中的

值将根据复选框状态相应地切换为true / false。至少这就是我要做的。