首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为List.js编写排序函数?

如何为List.js编写排序函数?
EN

Stack Overflow用户
提问于 2017-05-11 08:18:17
回答 2查看 2.2K关注 0票数 1

我使用list.js对表的数据进行简单的排序。排序功能运行良好,但我想修改它的初始行为。当用户第一次看到该表时,他/她应该在一个列中(在本例中使用我的本地货币)将带有某些特定值的记录放在顶部。只是最初,以后的排序应该以一种标准的方式工作。

让我们看看代码:

代码语言:javascript
复制
<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>

代码语言:javascript
复制
<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>

我唯一想做的就是把所有的记录与‘波兰兹罗提’货币放在最上面的开始。为什么我不按我想要的方式在HTML中按我想要的方式排序,然后启用排序,而不进行初始排序?因为实际上,这些记录是由PHP生成的(我简化了上面的代码,只是展示了生成的HTML示例),我无法预测我将得到哪些数据。

我需要在这个地方写一个排序函数:

代码语言:javascript
复制
accountsList.sort("currency-td", {
    order: "desc",
    sortFunction: function () {}
});

你有什么想法吗?)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-11 12:00:51

我是这样想出来的:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2017-05-11 08:56:31

尝试使用List.js的字母表功能,例如:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43910047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档