简体   繁体   中英

Get value of multiple checkbox in angular primeng

  • Want to get the value from multiple checkbox.
  • Each row consist of user name and permission given to user ie read, write.
  • In the following following code I am trying to give the permission to user ie read, write.
  • for UI am using primeng checkbox.
  • on check the value read need to be added to user array.

在此处输入图片说明

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>

Instead of maintaining a permission array you can maintain a permission object and bind it your checkboxes.

There will be no need to maintain an index in that case.

Your code will be like this:

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}}
    ]
  }

} 

And your 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>

And with PrimeNG

You need to add

binary="true"

attribute to your checkbox

You can see the demo here

You have to have those read/write flags in your model and connect those to proper checkboxes. eg

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}}
]

and

<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>

values in model will switch accordingly to true/false depending on checkbox state. That is at least how I would do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM