Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为List.js编写排序函数?_Javascript_Sorting_List.js - Fatal编程技术网

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

Javascript 如何为List.js编写排序函数?,javascript,sorting,list.js,Javascript,Sorting,List.js,我使用list.js对表中的数据进行简单排序。排序功能工作正常,但我想修改它的初始行为。当用户第一次看到该表时,他/她应该将某个特定值的记录放在一列的顶部(本例中使用我的本地货币)。就在最初,以后的排序应该以标准方式工作 让我们看看代码: <table id="accountsList"> <thead> <tr> <th scope="col" class="sort" data-sort="currency-td" aria-

我使用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>
你有什么想法吗?:)

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

var options = {
    valueNames: ['currency-td', 'accountNo-td', 'used-td']
};

var accountsList = new List('accountsList', options);

accountsList.sort("currency-td", { alphabet: "PLNABCDEFGHIJKMOQRSTUVXYZplnabcdefghijkmoqrstuvxyz" }
);

这里有记录

我是这样想的:

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;
        }
    });
accountsList.sort('currencyTd'{
订单:“asc”,
sortFunction:函数(a,b){
如果((a.currencyTd=='PLN')!=(b.currencyTd=='PLN')){
返回a.currencyTd==‘PLN’?1:-1;
}
返回a.currencyTd>b.currencyTd?1:
a、 currencyTd
建议的解决方案如下:

我认为这确实会将“PLN”记录放在首位,但恐怕它也有副作用。例如,在本例中,“NGN”将位于“JPY”之前。我只需要“PLN”例外。
var options = {
    valueNames: ['currency-td', 'accountNo-td', 'used-td']
};

var accountsList = new List('accountsList', options);

accountsList.sort("currency-td", { alphabet: "PLNABCDEFGHIJKMOQRSTUVXYZplnabcdefghijkmoqrstuvxyz" }
);
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;
        }
    });