【问题标题】:ExtJS sort grid column of string as floatExtJS将字符串的网格列排序为浮点数
【发布时间】:2015-11-04 23:02:37
【问题描述】:

我有一个网格,其中有一列将浮点记录字段呈现为字符串。所以,当我对列进行排序时,它是用字符串格式排序的,因此,以错误的方式:

我尝试了不同的解决方案。我理解它应该起作用的一个是使用 sortType 函数。但是到目前为止我还没有...

这些是我的代码的重要部分。

我用数据加载网格的模型:

Ext.define('AMA.model.AssetModel', {
extend: 'Ext.data.Model',

fields: [
    {name: 'Account', type: 'string'},
    {name: 'AltLabel'},
    {name: 'Amortizable', type: 'int'},
    {name: 'AmortizationsVNC',
        type: 'float',
        convert: function(value,model){
            return parseFloat(Math.round(value * 100) / 100).toFixed(2);
        }
    },
    {name: 'AssetsAccountingPrice',
        type: 'float',
        convert: function(value,model){
            return parseFloat(Math.round(value * 100) / 100).toFixed(2);
        }
    },
    .....

我的网格列受到影响:

items: [{
xtype: 'grid',
reference:'grdGestion',
id:'grdGestion',
//store: assetsStore,
//height: Ext.getCmp('MainContenedor').lastBox.height-150-50,
columns: [{
    .....

    },{
        text: l10n.translate('Coste'),
        flex: 1,
        sortable: true,
        dataIndex: 'AssetsAccountingPrice',
        xtype: 'numbercolumn',
        decimalPrecision: 2,
        decimalSeparation: ',',
        thousandSeparation: '.'
    }, {
        text: l10n.translate('VNC'),
        flex: 1,
        sortable: true,
        dataIndex: 'AmortizationsVNC',
        renderer: function(value){ 
            return Ext.util.Format.number(value, '0,000.00');
        }
    }, {
    .....

这两个字段都没有按我的预期对数据进行排序。

注意(可能有用):字符串格式为“0.000,00”

我该如何解决这个问题?

【问题讨论】:

  • ext 版本好吗?

标签: javascript string sorting extjs grid


【解决方案1】:

请看这个:FIDDLE

{
    name:'fieldName', 
    type: 'float', 
    sortType: 'asFloat', // you just need to add this 
    convert: function(value,model) {
        return parseFloat(Math.round(value * 100) / 100).toFixed(2);
    }
}

【讨论】:

  • 太棒了!那工作得很好!非常感谢!我已经厌倦了“sortType:'asFloat'”,甚至我自己实现了一个sortType函数,但我直接在Grid项目上使用它,也许这就是它不起作用的原因......
  • @daniegarcia254 - 我看到你确实提到 sortType 不起作用并且有点困惑。但是,是的,它必须在模型领域。 :)
猜你喜欢
  • 2012-10-14
  • 2011-11-25
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多