在 DataTables API 中排序前格式化数字
format numbers before sort in DataTables API
我在 DataTables 中有一列,其中包含一些金额,它们的格式类似于 1,200.00,我想获得列中的最大金额。为了做到这一点,我使用下面的代码,如果我在列中有 22.50、250.00 和 1,200.00,它应该给我 1,200.00 作为最大数量,但它会带回 250.00 作为最大数量.
var maxPrice = table
.column(2)
.data()
.sort(function(a,b){
return a - b
})
.reverse()[0];
您可以使用数据表 api,toArray()
方法。
然后用JS或者jquery解析数字并排序
还使用 extractContent()
函数代替 html 获取文本。
/**
* Get Max Price
*/
function extractContent(s) {
var span = document.createElement('span');
span.innerHTML = s;
return span.textContent || span.innerText;
};
var PriceArray = [];
$( table.column(2).data().toArray() ).each(function( index ) {
PriceArray.push(extractContent(this).replace(',',''));
});
var maxPrice = PriceArray.sort(function(a,b){return a-b}).reverse()[0];
console.log(maxPrice);
我在 DataTables 中有一列,其中包含一些金额,它们的格式类似于 1,200.00,我想获得列中的最大金额。为了做到这一点,我使用下面的代码,如果我在列中有 22.50、250.00 和 1,200.00,它应该给我 1,200.00 作为最大数量,但它会带回 250.00 作为最大数量.
var maxPrice = table
.column(2)
.data()
.sort(function(a,b){
return a - b
})
.reverse()[0];
您可以使用数据表 api,toArray()
方法。
然后用JS或者jquery解析数字并排序
还使用 extractContent()
函数代替 html 获取文本。
/**
* Get Max Price
*/
function extractContent(s) {
var span = document.createElement('span');
span.innerHTML = s;
return span.textContent || span.innerText;
};
var PriceArray = [];
$( table.column(2).data().toArray() ).each(function( index ) {
PriceArray.push(extractContent(this).replace(',',''));
});
var maxPrice = PriceArray.sort(function(a,b){return a-b}).reverse()[0];
console.log(maxPrice);