在后台网格中对自定义(货币)格式的列进行排序



我正在使用Backgridjs来显示从json对象到表的数据。我目前正在使用格式化程序将字符串数字格式化为货币。一旦我这样做了,排序就不再正常工作,因为它作为字符串而不是数字排序。格式化列后如何启用带有后台网格的排序?

Backgrid 支持数字、int、date/momentjs。找不到货币的扩展名

这是我的格式化程序类

formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
fromRaw: function(rawData) {
var re = /-/;
if (rawData === "" || rawData == null) {
return "";
} else if (rawData.match(re)) {
return "-" + accounting.formatMoney(rawData.substr(1));
} else {
return accounting.formatMoney(rawData);
}
},
toRaw: function(formattedData) {
return formattedData;
}

}),

这是我的网格

var grid = new Backgrid.Grid({
collection: collection,
columns: [
{
name: "cost",
label: "Cost",
cell: "number",
formatter: currencyFormater 
sortable: true
},
{
name: "type",
label: "Type",
cell: Backgrid.NumberCell,
sortable: true
}
]});

数据示例:

{ id: 1, cost: "150", type: 3 },
{ id: 2, cost: "12516.30", type: 2 },
{ id: 3, cost: "21400.85", type: 1 },
{ id: 4, cost: "146558.50", type: 1 },
{ id: 5, cost: "139982.75", type: 1 }

我最终使用sortValue根据值进行特定的排序。就我而言,我将parseFloat与字符串值一起使用。

var grid = new Backgrid.Grid({
collection: collection,
columns: [
{
name: "cost",
label: "Cost",
cell: "number",
sortValue: function(model) {
return parseFloat(model.get("cost"));
},
formatter: currencyFormater 
sortable: true
},
…
]});

最新更新