简体   繁体   English

PrimeNg<p-table> 排序</p-table>

[英]PrimeNg <p-table> sorting

I am using primeNg <p-table> .我正在使用 primeNg <p-table> I want to implement sorting of data.我想实现数据排序。 What I did is below我所做的如下

sort.HTML排序.HTML

<p-table [columns]="cols" [value]="documents">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns" [pSortableColumn]="col.field">
                {{col.header}}
                <p-sortIcon [field]="col.field"></p-sortIcon>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-doc>
        <tr>
            <td>
                {{doc.sName}}
            </td>

        <td>
                {{doc.sYear}}
            </td>
        <td>
                {{doc.sAge}}
            </td>
        <td>
                {{doc.sColor}}
            </td>        
        </tr>
    </ng-template>
</p-table>

sort.ts排序.ts

this.cols = [
            { field: 'name', header: 'Name' },
            { field: 'year', header: 'Year' },
            { field: 'age', header: 'Age' },
            { field: 'color', header: 'Color' }
        ];

ngOnInit(){
    //made a service call and got data for

this.documents=[{
"sName":"Jhon",
"sYear":"1994",
"sAge":"20",
"sColor":"Red"
},
{
"sName":"Sam",
"sYear":"1996",
"sAge":"25",
"sColor":"Red"
},
{
"sName":"Anna",
"sYear":"1991",
"sAge":"21",
"sColor":"Green"
},
{
"sName":"Jhon",
"sYear":"1999",
"sAge":"25",
"sColor":"Blue"
},
{
"sName":"Betty",
"sYear":"1993",
"sAge":"35",
"sColor":"Red"
}]
}

As of now only Name field is getting sorted, how can I implement sorting in other columns as well?到目前为止,只有Name字段得到排序,我如何在其他列中也实现排序? I used [pSortableColumn] but columns are not getting sorted, I am missing out of somepoint.我使用[pSortableColumn]但列没有得到排序,我错过了某个点。 Can you please guide me where I am wrong?你能指导我哪里错了吗?

PS: I cannot used <p-dataTable> . PS:我不能使用<p-dataTable>

For Sorting with Turbo table / p-table with fixed column try below code 对于带有固定列的Turbo table / p-table排序,请尝试下面的代码

                <p-table #dt [value]="data">
                <ng-template pTemplate="header">
                    <tr>
                        <th [pSortableColumn]="'Name'">Name
                            <p-sortIcon [field]="'Name'"></p-sortIcon>
                        </th>
                        <th [pSortableColumn]="'Age'">Age
                            <p-sortIcon [field]="'Age'"></p-sortIcon>
                        </th>
                        <th [pSortableColumn]="'Height'">Height
                            <p-sortIcon [field]="'Height'"></p-sortIcon>
                        </th>
                    </tr>
                </ng-template>
                <ng-template pTemplate="body" let-col>
                    <tr>
                        <td>{{col.Name}}</td>
                        <td>{{col.Age}}</td>
                        <td>{{col.Height}}</td>
                    </tr>
                </ng-template>
            </p-table>

if I got your question right, you are not asking to be able to sort multiple columns at the same time, but simply sorting is not working. 如果我的问题是正确的,那么你并不是要求能够同时对多个列进行排序,而只是排序不起作用。 In your code the problem is that you are specifying in the header of the table the following columns field to sort to: 在您的代码中,问题是您在表的标题中指定以下列字段以进行排序:

[pSortableColumn]="col.field"

and these fields are defined as: 这些字段定义为:

this.cols = [
            { field: 'name', header: 'Name' },    
            { field: 'year', header: 'Year' },
            { field: 'age', header: 'Age' },
            { field: 'color', header: 'Color' }
        ];

BUT your data is arriving with different names: 但是您的数据以不同的名称到达:

this.documents=[{
          "sName":"Jhon",
          "sYear":"1994",
          "sAge":"20",
          "sColor":"Red"
},
[...]

"name" != "sName" so your table is not capable to sort the data. “name”!=“sName”因此您的表无法对数据进行排序。 In fact I'm surprised you say that the column "name" is sortable. 事实上,我很惊讶你说“名字”栏是可以排序的。

Just change the definition and the code will be working. 只需更改定义,代码就可以正常工作。

In any case, to allow also multi column sorting, I suggest to change the code as: 在任何情况下,为了允许多列排序,我建议将代码更改为:

<p-table [columns]="cols" [value]="documents" sortMode="multiple">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns" [pSortableColumn]="col.field">
                {{col.header}}
                <p-sortIcon [field]="col.field"></p-sortIcon>
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-doc let-columns="columns">
        <tr>
            <td *ngFor="let col of columns">
                {{doc[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

It is also more light and no change in the ts file is required, even if you take data from a web service since from the html file point of view you are always passing a "documents" object. 它也更轻,并且不需要更改ts文件,即使您从Web服务获取数据,因为从html文件的角度来看,您总是传递“文档”对象。

The TurboTable it's OK TurboTable 没关系

        <div class="table-responsive ">
        <p-table #turboTable [value]="claims" [rowHover]="true" [paginator]="true" [rows]="20" [showCurrentPageReport]="true" [first]="first"
                 currentPageReportTemplate="Reclamos de {first} a {last} de {totalRecords} registros" [rowsPerPageOptions]="[10,25,50]">
            <ng-template pTemplate="header">
                <tr>

                    <th pSortableColumn="id" width="8%">ID <p-sortIcon field="id"></p-sortIcon></th>

                    <th pSortableColumn="code" width="15%">CODE <p-sortIcon field="code"></p-sortIcon></th>

                    <th pSortableColumn="customerClaimDate" width="18%">FECHA RECLAMO CLIENTE <p-sortIcon field="customerClaimDate"></p-sortIcon></th>

                    <th pSortableColumn="claimDate" width="18%">FECHA CREACIÓN RECLAMO <p-sortIcon field="claimDate"></p-sortIcon></th>

                    <th pSortableColumn="maximumResponseDate" width="15%">FECHA MAX RESPUESTA <p-sortIcon field="maximumResponseDate"></p-sortIcon></th>

                    <th pSortableColumn="assignedUserName" width="17%">DERIVADO A <p-sortIcon field="assignedUserName" ></p-sortIcon></th>

                    <th pSortableColumn="state" width="10%">ESTADO <p-sortIcon field="state"></p-sortIcon></th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-tblreclamos>
                <tr>
                    <td>
                        <span>{{ tblreclamos.id }}</span>
                    </td>
                    <td>
                        <span><a [routerLink]="['edicion', tblreclamos.id]">{{ tblreclamos.code }}</a></span>
                    </td>
                    <td>
                        <span>{{ tblreclamos.customerClaimDate }}</span>
                    </td>
                    <td>
                        <span>{{ tblreclamos.claimDate }}</span>
                    </td>
                    <td>
                        <span>{{ tblreclamos.maximumResponseDate }}</span>
                    </td>
                    <td>
                        <span>{{ tblreclamos.assignedUserName }}</span>
                    </td>
                    <td>
                        <span>
                           <span [class]="'customer-badge'"
                                 *ngIf="tblreclamos.state==53">Borrador</span>
                          <span [class]="'customer-badge status-warning'"
                                *ngIf="tblreclamos.state==54">Pendiente</span>
                          <span [class]="'customer-badge status-success'"
                                *ngIf="tblreclamos.state== 55">Resuelto</span>
                          <span [class]="'customer-badge status-danger'"
                                *ngIf="tblreclamos.state==56">Anulado</span>

                        </span>
                    </td>
                </tr>
            </ng-template>


        </p-table>
    </div>

You need to enable multi mode for sorting using sortMode="multiple" like this - 您需要使用sortMode="multiple"启用多模式进行排序,如下所示 -

<p-table [columns]="cols" [value]="documents" sortMode="multiple">

Default sorting is executed on a single column, in order to enable multiple field sorting, set sortMode property to "multiple" and use metakey when clicking on another column. 默认排序在单个列上执行,以便启用多个字段排序,将sortMode属性设置为“multiple”,并在单击另一列时使用metakey。

For more information refer to documentation - 有关更多信息,请参阅文档 -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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