我是淘汰赛的新手。
很好,你帮助我理解我需要如何改变这段代码:
<ol class="items" data-bind="foreach: getDisplayedItems()"> <li class="item"> <a class="simple" data-bind="html: label, attr: {href:url}, visible: count >= 1, css: { active: is_selected }"></a> <span class="count" data-bind="text: count"></span> </li> </ol>
按字母顺序排列?
谢谢你的建议关于
您可以对它们进行就地排序:
<ol class="items" data-bind="foreach: getDisplayedItems().sort((a, b) => ko.unwrap(a.label) < ko.unwrap(b.label) ? -1 : 1)">
<li class="item">
<a class="simple" data-bind="html: label, attr: {href:url}, visible: count >= 1, css: { active: is_selected }"></a>
<span class="count" data-bind="text: count"></span>
</li>
</ol>
但是我认为这很难看,而且不能重复使用。最好创建一个新的可观察数组,以正确的顺序包含项目:
function App {
// the observable you already have
this.getDisplayedItems = ko.observableArray([...]);
// a computed observable based on that
this.getDisplayedItemsAlphabetical = ko.pureComputed(() => this.getDisplayedItems.sorted((a, b) => ko.unwrap(a.label) < ko.unwrap(b.label) ? -1 : 1));
}
和
<ol class="items" data-bind="foreach: getDisplayedItemsAlphabetical">