前端Angular排序

前端Angular排序

此种方法适用于数据量小时,将数据全部获取后,在前端table中点击排序按钮进行排序。排序逻辑放在前端服务中,不需要向后端发请求。

创建服务SortTableListService提供排序方法

使用

1, Html中使用Ant Design of Angular的<nz-table>标签。

nzShowSort显示列的排序按钮,nzSortKey配置排序的依照属性名,sortlist()方法在点击排序按钮后调用。示例代码如下。

<nz-table #nzTable [nzData]="list" >

     <thead (nzSortChange)="sortlist($event)" nzSingleSort>

          <tr>

               <th id="Label" nzWidth="10%" nzShowSort  nzSortKey="title">名称</th>

          </tr>

     </thead>

     <tbody>

          <tr [class.selected-tr]="i === selectedOrderIndex" *ngFor="let item of nzTable.data; let i = index">

               <td class="overflow-td">{{item.title}}</td>

          </tr>

     </tbody>

</nz-table>

2,在component.ts中实现sortlist()方法

1)引入SortTableListService服务

constructor(

        private sortTableListService: SortTableListService

    ) {}

2)在sortlist()方法中调用服务方法实现排序。

sortlist(sort: { key: string; value: string }): void {

    if (this.isUndefinedOrNull(this.list)) {

         return;

    }

    this.list = this.sortTableListService.sortTableListBykey(sort, list);

  }

当table中的某一项是一个list拼接显示的值时

eg:<nz-table>中[nzData]的值 list : dto[]。
dto的属性包含subPropertyList: subDto[]。即dto中不仅包含object类型的属性,还包括list数组类型的属性。
subDto的属性包括siteName等。在table中展示siteName时拼接subPropertyList中的每一项。
若对siteName这一列进行排序,则需要调用SortTableListService的另一个方法sortTableListByKeyAndListkey().
示例代码如下:

html:

<nz-table #nzTable [nzData]="list" >

     <thead (nzSortChange)="sortlist($event)" nzSingleSort>

          <tr>

               <th id="Label" nzWidth="10%" nzShowSort  nzSortKey="title">名称</th>

               <th id="TreatmentSite" nzWidth="10%" nzShowSort  nzSortKey="siteName">治疗部位</th>

          </tr>

     </thead>

     <tbody>

          <tr [class.selected-tr]="i === selectedOrderIndex" *ngFor="let item of nzTable.data; let i = index"

               (click)="switchSelectedOrder(i)">

               <td class="overflow-td">{{item.title}}</td>

               <td class="overflow-td">

                    <div

                         *ngFor="let d of item?.subPropertyList; let i = index; let c = count;">

                         <div *ngIf="d.siteName != null">

                              {{d.siteName}}

                              <em *ngIf="i != c - 1"> / </em>

                         </div>

                    </div>

               </td>

          </tr>

     </tbody>

</nz-table>

component.ts

sortlist(sort: { key: string; value: string }): void {

        this.list =

         this.sortTableListService.sortTableListByKeyAndListkey(sort,

             this.list, ['siteName'], 'subPropertyList');

    }

SortTableListService

根据自己项目的情况注入该服务,示例中使用 providedIn: 'root'

import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class SortTableListService {

     Init() {}

     // 列表中对象的属性不包含list属性,每个属性均为一个object
     // 通过列表中对象的某个属性进行排序, key为对象的属性名称,设置在在<th>中nzSortKey的值
     // sort为此次排序传入的排序参照属性名和排序顺序
     sortTableListBykey(sort: { key: string; value: string }, initiallist: any[]): any[] {
          if (sort.key && sort.value) {
              let list: any[] = [];
                  list = initiallist.sort((a, b) =>
                  sort.value === 'ascend' ?
                      (a[sort.key] > b[sort.key] ?
                          1 :
                          -1)
                      :
                      (b[sort.key] > a[sort.key] ?
                          1 :
                          -1)
              );
              initiallist = [...list];
          }
          return initiallist;
     }

     

     // 若列表中对象的某个属性也为list,页面展示为遍历属性list的对象属性值,进行拼接
     // lisProperty为列表对象中属性为list的属性名,key为list属性对象的属性名称,设置在在<th>中nzSortKey的值。
     // lisPropertyItem 为列表对象中属性为list的对象的属性名称。
     // eg:initiallist: dto[]; dto的属性有lisPropertyName: subDto[]; subDto的属性名为lisPropertyItem数组中的值
     sortTableListByKeyAndListkey(sort: { key: string; value: string },
         initiallist: any[], lisPropertyItem: string[], lisPropertyName: string): any[] {
          if (sort.key && sort.value) {
               let list: any[] = [];
               const isAscend = (sort.value === 'ascend');
               if (lisPropertyItem.includes(sort.key)) {
                    list = initiallist.sort((a, b) =>
                        this.compairObjectByListProperty(a, b, sort.key, isAscend, lisPropertyName) ? 1 : -1
                   );
                } else {
                    list = initiallist.sort((a, b) =>
                     this.compairObjectByProperty(a, b, sort.key, isAscend) ? 1 : -1
                    );
                }
                initiallist = [...list];
          }
          return initiallist;
      }

     private compairObjectByProperty(a: any, b: any, sortKey: string, isAscend: boolean): boolean {
          return isAscend ?
               (a[sortKey] > b[sortKey] ?
               true :
               false)
               :
               (b[sortKey] > a[sortKey] ?
               true :
               false)
      }

     private compairObjectByListProperty(a: any, b: any, sortKey: string,
         isAscend: boolean, lisPropertyName: string): boolean {
          if (this.isUndefinedOrNull(a[lisPropertyName])) {
               return isAscend ? false : true;
             }
             if (this.isUndefinedOrNull(b[lisPropertyName])) {
               return isAscend ? true : false;
             }
             let aValueListString = '';
             let bValueListString = '';
             a[lisPropertyName].forEach(item => {
                 if (!this.isUndefinedOrNull(item)) {
                     aValueListString += item[sortKey];
                 }
             })
             b[lisPropertyName].forEach(item => {
                 if (!this.isUndefinedOrNull(item)) {
                     bValueListString += item[sortKey];
                 }
             })
             return isAscend ?
              (aValueListString > bValueListString ? true : false)
              :
             (bValueListString > aValueListString ? true : false) ;
      }

      isUndefinedOrNull(object: any): boolean {
          if (object === undefined
            || object === null) {
              return true;
          }
          return false;
       }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值