2017-11-08 3 views

答えて

2

sort()メソッドの4番目の引数としてカスタムソート関数を指定できます。

例:

let COMPARE_INSENSITIVE = (direction: any, a: any, b: any) => { 
    // Converting strings to lowercase 
    let first = typeof a === 'string' ? a.toLowerCase() : a; 
    let second = typeof b === 'string' ? b.toLowerCase() : b; 

    if (first < second) { 
    return -1 * direction; 
    } 
    if (first > second) { 
    return direction; 
    } 
    return 0; 
} 

NG2-スマートテーブルは、次のデフォルトコンペア機能を使用しています。

export class LocalSorter { 

    protected static COMPARE = (direction: any, a: any, b: any) => { 
    if (a < b) { 
    return -1 * direction; 
    } 
    if (a > b) { 
     return direction; 
    } 
    return 0; 
    } 

    static sort(data: Array<any>, field: string, direction: string, customCompare?: Function): Array<any> { 

    const dir: number = (direction === 'asc') ? 1 : -1; 
    const compare: Function = customCompare ? customCompare : this.COMPARE; 

    return data.sort((a, b) => { 
     return compare.call(null, dir, a[field], b[field]); 
    }); 
    } 
} 
+0

このコードを挿入する場所はどこですか?このカスタム機能はありますか? –

+1

Lumixが書いたように、テーブル構成でcompareFunction属性を指定することができます:[すべてのテーブル構成プロパティを持つドキュメント] –

+1

is not working –

1

はちょうどあなたが実装するかどうかをスローするようにしたかったですこれはあなたがa:compaの後ろにreFunction。下に示すように...

columns: { 
    group_name: { 
     title: 'Groupname', 
     compareFunction:(direction: any, a: any, b: any) => { 
      // Converting strings to lowercase 
      let first = typeof a === 'string' ? a.toLowerCase() : a; 
      let second = typeof b === 'string' ? b.toLowerCase() : b; 

      if (first < second) { 
       return -1 * direction; 
      } 
      if (first > second) { 
       return direction; 
      } 
      return 0; 
     } 
    } 
} 
関連する問題

 関連する問題