PrimgNg:<p-table>列排序

时间:2018-06-28 12:13:16

标签: angular primeng primeng-datatable

我正在使用<p-table>,并且必须对标题进行排序。我正在做如下:

HTML

<p-table [value]="documents">
        <ng-template pTemplate="header">
            <tr>
                <th [pSortableColumn]="">
                    File Name
                    <p-sortIcon [field]=""></p-sortIcon>
                </th>
               <th [pSortableColumn]="">
                    File Type
                    <p-sortIcon [field]=""></p-sortIcon>
                </th>
               <th [pSortableColumn]="">
                    File Date
                    <p-sortIcon [field]=""></p-sortIcon>
                </th>
            </tr>
        </ng-template>
    <ng-template pTemplate="body" let-doc>
        <tr>
            <td>
                {{doc.sName}}
            </td>

        <td>
                {{doc.sType}}
            </td>
        <td>
                {{doc.sDate}}
            </td>                
        </tr>
    </ng-template>
</p-table>

TS

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

this.documents=[{
   "sName":"Book",
   "sType":"PDF",
   "sDate":"20"
   },
   {
   "sName":"Book",
   "sType":"PDF",
   "sDate":"20"
   },
   {
   "sName":"Cook Book",
   "sType":"Text",
   "sDate":"20"
   },
   {
   "sName":"Book",
   "sType":"PDF",
   "sDate":"25-04"
   },
   {
   "sName":"File",
   "sType":"PDF",
   "sDate":"02-01"
   }]
}

我在代码中确实使用了[pSortableColumn][field],但是我没有得到传递该特定字段排序值的值。数据正确弹出,只是我所缺少的排序。请指导我如何实现列的排序。谢谢

我无法使用<p-dataTable>

2 个答案:

答案 0 :(得分:5)

替换

<th [pSortableColumn]="">
    File Name
    <p-sortIcon [field]=""></p-sortIcon>
</th>

使用

<th [pSortableColumn]="'sName'">
    File Name
    <p-sortIcon [field]=""></p-sortIcon>
</th>

例如要按 sName 排序。

答案 1 :(得分:1)

只要有人需要,只需在@Antikhippe接受的答案中添加其他信息即可。

对于已接受答案的代码示例,p-sort图标没有更改。

因此,使用以下代码即可。

(defun find-entry (entry file &key (select #'car))
  (with-open-file 
    (file-stream file) 
    (loop for sexp = (read file-stream nil)
      while sexp
      do (if (equal (funcall select sexp) entry)
             (return sexp)))))