簡體   English   中英

Ember.js中的排序表

[英]Sorting Table within Ember.js

我有一個用服務器數據生成的表。 表格上的每一列在頂部都有一個按鈕,可根據該列中的值從高到低或從低到高對整個表格進行排序。 還有一個名稱列,按字母順序排序。

目前,我已經將所有這些都與手柄掛鈎了,當您單擊按鈕時,它將對表數據數組進行排序,然后重新插入模板並將其插入到頁面中。

我正在嘗試將其移至使用ember.js。 我的第一個問題是我的表數據數組不再是一個簡單的數組,它是一個內部具有各種get和set方法以及其他內容的ember對象。 如何使用類似的功能對它進行排序:

function sortAtoZ(arr, one, two) {
    arr.sort(function(a, b) {return a[one].localeCompare(b[two])});
}

通過Sortable Mixin在ember陣列控制器中內置了對基本排序的支持。 開箱即用,它支持使用Ember.compare按一個或多個屬性排序,但可以使用自定義sortFunction,例如:

App.TableController = Ember.ArrayController.extend({
  sortProperties: ['trackNumber'],
  sortAscending: true,
  sortFunction: function(a,b) {
    // your custom sort logic here
    // return 0 if the two parameters are equal, return a negative value if the first parameter is smaller than the second or return a positive value otherwise
  }
});

另外,如果您想對表格進行更高級的操作,請查看ember-table

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM