gpt4 book ai didi

sorting - 简单的ember表头排序

转载 作者:行者123 更新时间:2023-12-02 06:08:48 24 4
gpt4 key购买 nike

我有一个 ember 应用程序,其中有一张 table 。我正在尝试做一个简单的列标题排序,但似乎无法让它工作或找到任何关于如何做到这一点的体面示例。任何援助将不胜感激。

链接.hbs:

<thead>
<tr>
<th {{action "sortBy" "linkID"}}>ID</th>
<th {{action "sortBy" "name"}}>NAME</th>
<th {{action "sortBy" "description"}}>DESCRIPTION</th>
<th {{action "sortBy" "url"}}>URL</th>
<th {{action "sortBy" "effDate"}}>EFFECTIVE DATE</th>
<th {{action "sortBy" "endDate"}}>END DATE</th>
<th {{action "sortBy" "secr"}}>SECURITY RESOURCE</th>
<tr>
</thead>

links.js( Controller ):
import Ember from 'ember';

export default Ember.ArrayController.extend({
actions:{
sortBy: function(property) {
alert('Hello');
this.set('sortProperties', [property]);
this.set('sortAscending', !this.get('sortAscending'));
}
}

});

最佳答案

假设您正在迭代 model要构建表格,最简单的方法(对代码的更改最少)就是设置 model arrangedContent 每次排序时(sortedProperties 更改时):

Controller :

export default Ember.ArrayController.extend({
sortProperties: ['name'],
sortAscending: false,

actions:{

sortBy: function(property) {
this.set('sortProperties', [property]);
this.toggleProperty('sortAscending');
this.set('model', this.get('arrangedContent')); // set the model to the sorted array
}
}
});

模板:
<table>
<thead>
<tr>
<th {{action "sortBy" "linkID"}}>ID</th>
<th {{action "sortBy" "name"}}>NAME</th>
<th {{action "sortBy" "description"}}>DESCRIPTION</th>
<th {{action "sortBy" "url"}}>URL</th>
<th {{action "sortBy" "effDate"}}>EFFECTIVE DATE</th>
<th {{action "sortBy" "endDate"}}>END DATE</th>
<th {{action "sortBy" "secr"}}>SECURITY RESOURCE</th>
</tr>
</thead>
<tbody>
{{#each item in model}}
<tr>
<td>{{item.linkID}}</td>
<td>{{item.name}}</td>
<td>{{item.description}}</td>
<td>{{item.url}}</td>
<td>{{item.effDate}}</td>
<td>{{item.endDate}}</td>
<td>{{item.secr}}</td>
</tr>
{{/each}}
</tbody>
</table>

Here is a sample

Here is a full demo

如果您需要自定义比较器功能(例如比较日期时),您可以覆盖 sortFunction hook 和 Ember 在比较元素进行排序时会自动调用它。传入 sortFunction的参数是 sortProperties的内容对应的属性值.

例如,对于数组 [ {name: "bob"}, {name: "john"} ] :
// ...
sortProperties: ['name'],
sortFunction: function(propA, propB) {
// propA === "bob"
// propB === "john"
if (propA < propB) {
return -1;
}
if (propA > propB) {
return 1;
}
return 0; // a must be equal to b
}
// ...

关于sorting - 简单的ember表头排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com