【发布时间】:2017-10-10 03:20:48
【问题描述】:
我使用 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>
我唯一想做的就是将所有带有“PLN”货币的记录放在开头。为什么我不只是按照我想要的方式在 HTML 中按我想要的方式对它们进行排序,然后在没有初始排序的情况下启用排序?因为实际上这些记录是用PHP生成的(我把上面的代码简化了,只是展示了一个生成HTML的例子),我无法预测我会得到什么数据。
我需要在这个地方写一个排序函数:
accountsList.sort("currency-td", {
order: "desc",
sortFunction: function () {}
});
你有什么想法吗? :)
【问题讨论】:
标签: javascript sorting list.js