在Bootstrap表中对货币进行排序

问题描述 投票:0回答:3

我想将货币符号添加到我的号码中,同时使值仍然可以作为我的Bootstrap表中的数字排序(第30-34行;表格中的第4列):

for (var p=0; p<variable_data.length; p++){
    try{
        variable_data[p]["4"] = "$" + Math.round(variable_data[p]["4"])
    } catch(e){ }
}

http://jsfiddle.net/mademoiselletse/s0d1xgzt/

附加'$'的值按字符串而不是数字排序。我在网上找到的所有货币排序问题都与DataTable和tablesorter()插件有关。是否比安装更多jQuery插件更快地解决了这个问题?

非常感谢您的帮助!

javascript jquery twitter-bootstrap sorting bootstrap-table
3个回答
0
投票

检查自定义示例here

您需要发送一个data-sorter function,从字符串中删除$,然后将其作为数字进行比较。


0
投票

我遇到了类似的问题,最后从他们的实际网站上查看了自定义排序器示例的源代码:http://issues.wenzhixin.net.cn/bootstrap-table/#options/custom-sort.html

首先,在定义包含货币/货币/美元值的列时,将分拣机的引用添加到您要使用的任何javascript函数中。这是我的货币列定义:

[
    "field" => "total",
    "title" => "Total",
    "sortable" => true,
    "sorter" => "totalCurrencySort"
]

之后,您将需要创建在特定列排序时执行的javascript函数。这里我们定义totalCurrencySort,它可以采用4个参数,但只需要前两个参数。

function totalCurrencySort(a, b, rowA, rowB) {  
    a = +a.substring(1); // remove $
    b = +b.substring(1);
    if (a > b) return 1;
    if (a < b) return -1;
    return 0;
}

为了进行适当的比较,根据需要剥离“$”,并且数值评估确定两个值之间的正确排序状态并相应地返回。 rowN引用提供行的完整数据集,以防您的自定义排序逻辑需要使用行内其他数据进行更复杂的计算。


0
投票

我太新了,无法对Art的帖子发表评论,但想要为我的作品添加注释。 Art的帖子中的totalCurrencySort函数为我工作。在表头中,我将属性作为数据排序器(而不仅仅是排序器)。

function totalCurrencySorter(a, b, rowA, rowB) {
a = +a.substring(1);  // remove $
b = +b.substring(1);
if (a > b) return 1;
if (a < b) return -1;
return 0;
}

function totalCurrencyFormatter(data) {
var field = this.field
return '$' + data.map(function (row) {
return +row[field].substring(1)
}).reduce(function (sum, i) {
return sum + i;
}, 0)
}

对于HTML表,我的标题大致如下:

<thead>
<tr>
<th data-field="id" data-sortable="true">ID</th>
<th data-field="exp_date" data-sortable="true">Date</th>
<th data-field="amount" data-sortable="true" formatter="totalCurrencyFormatter" data-sorter="totalCurrencySorter">Amount</th>
</tr>
</thead>
© www.soinside.com 2019 - 2024. All rights reserved.