Angular2 PrimeNG条件行格式

时间:2016-10-20 07:39:45

标签: angular datatable formatting conditional primeng

我有一个使用PrimeNG组件的Angular2应用程序。我试图使它成为当一个字段具有特定值时,DataTable中的行被着色为特定颜色。我有另一个字段,其中包含用于行突出显示的颜色值,但无法确定如何使其工作。

我的模型如下(非常简化):

export class RepackRequest{
AdhereToLeadTime:   boolean;
LeadTimeRemaining: string;

constructor() {
    var today = new Date();
    if(this.AdhereToLeadTime){
        var difference = today.getTime() - this.StartDate.getTime(); 
        if(difference >= 3 && difference <= 4){
            this.LeadTimeRemaining = "orange";
        }
        else if(difference >= 5){
            this.LeadTimeRemaining = "red";
        }
    }
    else {
        this.LeadTimeRemaining = 'white';
    }
 }
}

所以基本上如果它坚持5天的提前期,它会根据与提前期的接近时间而改变颜色。

在我的模板中,我有以下内容:

<p-dataTable    [value]="approvalRepacks" 
                            [rows]="10" 
                            [paginator]="true" 
                            [pageLinks]="5" 
                            [rowsPerPageOptions]="[5,10,20]"
                            selectionMode="single"
                            [(selection)]="selectedRepack"
                            (onRowSelect)="onSelect()"
                            [globalFilter]="na">
                <header>
                    <div style="text-align:center;">
                        <button pButton type="button" icon="fa-plus" iconPos="left" label="Create New Request" (click)="addNew()"></button>
                    </div>
                </header>
                <p-column field="DateRequested" header="Date Requested" sortable="true">
                    <template let-col let-rep="rowData" pTemplate type="body">
                        <span [style.background]="{{rep.LeadTimeRemaining}}">{{rep[col.field] | date:'dd/MM/yyyy'}}</span>
                    </template>
                </p-column>
                <p-column field="OrderNum"          header="Order No"           sortable="true" styleClass="wordBreak"></p-column>
                <p-column field="Customer"          header="Customer"           sortable="true"></p-column>
                <p-column field="FromItem"          header="Repack From"        sortable="true" styleClass="wordBreak"></p-column>
                <p-column field="ToItem"            header="Repack To"          sortable="true" styleClass="wordBreak"></p-column>
                <p-column field="FromWarehouse"     header="From Whse"          sortable="true"></p-column>
                <p-column field="FromLocation"      header="From Bin"           sortable="true"></p-column>
                <p-column field="ToWarehouse"       header="To Whse"          sortable="true"></p-column>
                <p-column field="ToLocation"        header="To Bin"           sortable="true"></p-column>
                <p-column field="Quantity"          header="Qty"                sortable="true"></p-column>
                <p-column field="RequestedBy"       header="Requested By"       sortable="true" styleClass="wordBreak"></p-column>
                <p-column header="Actions">
                    <template let-col let-rep="rowData" pTemplate type="body">
                        <button pButton type="button" icon="fa-check" label="Approve" (click)="approve(rep.ID)"></button>
                    </template>
                </p-column>
            </p-dataTable>

正如您所见,我有[style.background]="{{rep.LeadTimeRemaining}}"尝试设置列的颜色。

我想这是错误的方法,因为它只为该列设置它并且id在每列上都需要相同。

任何人都可以帮忙解决这个问题。我找不到任何关于它的信息。

非常感谢

修改

在每一列上使用以下内容将其突出显示为一个点然而它非常难看,因为它只是为div而不是td或tr着色。

<template let-col let-rep="rowData" pTemplate type="body">
    <div [style.background]="rep.LeadTimeRemaining" style="margin:-50px;">
         <span>{{rep[col.field]}}</span>
    </div>
</template>

enter image description here

3 个答案:

答案 0 :(得分:4)

您可以使用rowStyleClass属性突出显示数据表中的行。 您应该将函数传递给此属性。该函数应返回将在表行上应用的类名。

模板:

<p-dataTable [rowStyleClass]="highlightRow" ...>

组件代码:

highlightRow(rowData: any, rowIndex: number) {
    return rowData.LeadTimeRemaining + '-highlighting';
}

CSS:

.red-highlighting { background-color: red; }
.white-highlighting { background-color: white; }
.orange-highlighting { background-color: orange; }

答案 1 :(得分:1)

请尝试使用这种方式:

[style]="{'background':rep.LeadTimeRemaining}"

而不是:

[style.background]="{{rep.LeadTimeRemaining}}"

答案 2 :(得分:1)

div周围空白区域的原因是由于padding的primeng数据表应用于父td。要填充周围的空格,您可以将父p-column的填充设置为0(使用styleClass),然后将padding添加到div内的td数据表父p-column(参见下面的示例)。

采用这种方法的主要原因是,如果您想根据当前行的数据更改某些单元格的背景颜色。如果您想使用此方法根据数据更改整行的背景颜色,则必须在每个.padding-none { padding: 0 !important; } /** (optional) still pad table header and footer on datatables with columns set to .padding-none */ .ui-datatable thead th.padding-none, .ui-datatable tfoot td.padding-none, .ui-datatable tfoot th.padding-none { /** set to whatever your default datatable td padding is */ padding: .25em .5em !important; } .ui-datatable tbody td.padding-none div { /** set to whatever your default datatable td padding is */ padding: .25em .5em; } 上应用这些样式。如果您想要着色整行,请参阅here以获得更简单的方法。

<强> CSS

<p-column field="..." header="..." styleClass="padding-none" sortable="true">
  <template let-col let-rep="rowData" pTemplate>
    <div [style.background]="rep.LeadTimeRemaining">
      {{rep[col.field]}}
    </div>
  </template>
</p-column>

<强>模板

{{1}}
相关问题