【问题标题】:How to write sort function for List.js?如何为 List.js 编写排序函数?
【发布时间】: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


    【解决方案1】:

    尝试使用 List.js 的 字母 功能,类似:

    var options = {
        valueNames: ['currency-td', 'accountNo-td', 'used-td']
    };
    
    var accountsList = new List('accountsList', options);
    
    accountsList.sort("currency-td", { alphabet: "PLNABCDEFGHIJKMOQRSTUVXYZplnabcdefghijkmoqrstuvxyz" }
    );
    

    这里记录了http://listjs.com/api/#sort

    【讨论】:

    • 我认为它确实会将“PLN”记录放在首位,但恐怕它也有副作用。例如,在这种情况下,“NGN”将位于“JPY”之前。我只需要“PLN”例外。
    【解决方案2】:

    我是这样想的:

    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;
            }
        });
    

    建议的解决方案: https://stackoverflow.com/a/17254561/5420497

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-15
      • 2020-01-12
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多