【问题标题】:How to disable check box in primeng datatable如何禁用primeng数据表中的复选框
【发布时间】:2017-10-13 19:54:49
【问题描述】:

我需要根据条件禁用primeng数据表中的几个复选框:

例如:

<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()">

但这似乎不起作用。在primeng论坛上有相同的功能请求:https://forum.primefaces.org/viewtopic.php?f=35&t=47101&p=155122&hilit=disable#p155122

有没有人为此做过hack?

【问题讨论】:

  • @halfer 感谢您的反馈。我会在以后的帖子中记住这一点

标签: angular typescript primeng primeng-datatable


【解决方案1】:

您可以使用模板选项

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

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

所选项目

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

    }

Demo 相应更新 LIVE DEMO

更新 1:检查和检查全部

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

打字稿代码

  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

【讨论】:

  • 如何将这些添加到由多选选项完成的 selectedRows 数组中
  • 通过创建附加属性来处理复选框的更改事件。
  • 我正在这样做。selectedRows.push(row);在更改事件中,行被选中但复选框没有被选中,你能告诉我如何以编程方式检查它们
  • 您是否在模型中创建了属性?
  • 您能否在标题中提供一个复选框来选择/取消选择表格行
猜你喜欢
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 2017-09-15
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 2011-05-21
相关资源
最近更新 更多