我使用list.js对表格的数据进行简单的排序。排序功能正常,但我想修改它的初始行为。当用户第一次看到该表时,他/她应该在一列中具有某些特定值的记录(在此示例中使用我的本地货币)放在顶部。最初,后来的排序应该以标准方式工作。
让我们看看代码:
<table id="accountsList">
<thead>
<tr>
<th scope="col" class="sort" data-sort="currency-td" aria-role="button"><span>Currency</span></th>
<th scope="col" class="sort" data-sort="accountNo-td" aria-role="button"><span>Account number</span></th>
<th scope="col" class="sort td-centered" data-sort="used-td" aria-role="button"><span>Used</span></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td class="currency-td">EUR</td>
<td class="accountNo-td">53106010151036926643566665</td>
<td class="used-td td-centered">
<input type="checkbox" checked>
</td>
</tr>
<tr>
<td class="currency-td">PLN</td>
<td class="accountNo-td">83106010151036926643522665</td>
<td class="used-td td-centered">
<input type="checkbox">
</td>
</tr>
<tr>
<td class="currency-td">PLN</td>
<td class="accountNo-td">59996010151036926643566665</td>
<td class="used-td td-centered">
<input type="checkbox" checked>
</td>
</tr>
<tr>
<td class="currency-td">USD</td>
<td class="accountNo-td">33106010151036999643566675</td>
<td class="used-td td-centered">
<input type="checkbox">
</td>
</tr>
</tbody>
<script type="application/javascript">
$(document).ready(function(){
var options = {
valueNames: ['currency-td', 'accountNo-td', 'used-td']
};
var accountsList = new List('accountsList', options);
accountsList.sort("currency-td", {
order: "desc"
});
});
</script>
我唯一想做的就是将所有记录都放在&#39; PLN&#39;货币在开头的顶部。为什么我不按照我想要的方式按照我想要的方式对它们进行排序,然后启用排序,而不进行初始排序?因为事实上,这些记录是由PHP生成的(我简化了上面的代码,只显示了生成的HTML的一个例子),我无法预测我将获得的数据。
我需要在这个地方写一个排序函数:
accountsList.sort("currency-td", {
order: "desc",
sortFunction: function () {}
});
你有什么想法吗? :)
答案 0 :(得分:1)
尝试使用List.js的字母功能,smthg如:
var options = {
valueNames: ['currency-td', 'accountNo-td', 'used-td']
};
var accountsList = new List('accountsList', options);
accountsList.sort("currency-td", { alphabet: "PLNABCDEFGHIJKMOQRSTUVXYZplnabcdefghijkmoqrstuvxyz" }
);
答案 1 :(得分:0)
我这样想:
accountsList.sort('currencyTd', {
order: 'asc',
sortFunction: function (a, b) {
if ((a.currencyTd === 'PLN') != (b.currencyTd === 'PLN')) {
return a.currencyTd === 'PLN' ? 1 : -1;
}
return a.currencyTd > b.currencyTd ? 1 :
a.currencyTd < b.currencyTd ? -1 : 0;
}
});