How to disable checkbox in primeng datatable

I need to disable multiple checkboxes in primeng datatable based on condition:

For example:

<p-column *ngFor="let col of cols; let i = index" [field]="col.field" [header]="col.header" [styleClass]="col.class" selectionMode="{{col.header==fields.BULKACTIONS.header ? 'multiple': ''}}" [disabled]="isDisabled()">

      

But that doesn't seem to work. There is a feature request for this on the primeng forum: https://forum.primefaces.org/viewtopic.php?f=35&t=47101&p=155122&hilit=disable#p155122

Has anyone done a hack for this?

+3


source to share


1 answer


You can use the templates parameter

<p-column>
       <ng-template let-col let-car="rowData" pTemplate="body">
       <input type="checkbox" [disabled]="true"/>
       </ng-template>
</p-column>

      

Update 1:

<p-dataTable (onRowSelect)="rowSelected($event)"

   [value]="tableData" [(selection)]="selectedData" dataKey="model" [responsive]="true">
     <p-column>
       <ng-template let-col let-car="rowData" pTemplate="body">
           <input type="checkbox" [checked]="car.status"  [(ngModel)]="car.status" (change)="checked(car)"/>
       </ng-template>
     </p-column>
    <p-column field="orderNumber" header="Order Number"></p-column>
    <p-column field="country" header="Country"></p-column>
</p-dataTable>

      

Selected items

 checked(carValue){
    console.log(carValue)
    if(carValue.status){
      this.selectedData.push(carValue);
    }else {
   _.remove(this.selectedData, function(val) {return val === carValue;})      

    }

      



The demo is updated accordingly LIVE DEMO

Update 1: Check and CheckAll

<p-dataTable (onRowSelect)="rowSelected($event)"

   [value]="tableData" [responsive]="true">
     <p-column>
     <ng-template pTemplate="header">
           <input type="checkbox" [ngModel]="checkedAll" (ngModelChange)="checkAll($event)"/>
    </ng-template>
       <ng-template let-col let-car="rowData" pTemplate="body">
           <input type="checkbox" *ngIf="!car.disabled" [(ngModel)]="car.status" (change)="checked(car)"/>
           <input type="checkbox" *ngIf="car.disabled" [checked]="false" disabled (change)="checked(car)"/>
       </ng-template>
     </p-column>
    <p-column field="orderNumber" [header]="'Order Number'"></p-column>
    <p-column field="country" [header]="'Country'"></p-column>
</p-dataTable>

      

Typescript code

  checked(carValue){
    if(carValue.status){
      this.selectedData.push(carValue);
    }else {
     _.remove(this.selectedData, function(val) {return val === carValue;})      
    }
    console.log(this.selectedData)

  }
  checkAll(event){

    _.forEach(this.tableData =>(item){
      if(event){
      item.status=true;
      }else {
        item.status=false;
      }

    });

    this.selectedData= this.tableData;
    if(!event){
      this.selectedData = [];
    }
    console.log(this.selectedData);
  }

      

LIVE DEMO

+4


source







All Articles