【问题标题】:Sort by custom column type in ExtJs Grid Panel在 ExtJs 网格面板中按自定义列类型排序
【发布时间】:2014-02-21 16:40:21
【问题描述】:

我在 Ext.grid.Panel 中指定了以下列:

{text: 'Home Country', dataIndex: 'homeCountryId', width: 250, xtype: 'country-column'},

xtype 是“country-column”,定义为:

Ext.define("RateManagement.view.Columns.CountryColumn", {
    extend: 'Ext.grid.column.Column',   
    xtype: 'country-column',    
    text: 'Country',
    editor: { xtype: 'country-combo', allowBlank: false, blankText: 'Country is required.'},
    renderer: function(val) {
        var locationStore = Ext.data.StoreManager.lookup('RateManagement.store.CountryStore');
        var index = locationStore.findExact('value', val);
        if (index != -1) {
            var record = locationStore.getAt(index).data;
            return record.label;
        }
    },
    sortable: true
});

当我单击网格中的列标题(“本国”)时,它根本没有排序。

如何使它按record.label 排序以按字母顺序排序?

编辑:这是我更改模型的方式:

{name:'homeLocationId', type: 'string'}, 
{name:'homeLocation', type: 'string', convert: function (newValue, model) { 
        // homeCountryId is appened for uniqueness and additional sort
        return (newValue ? newValue : '' ) + '_' + model.get('homeLocationId');
    }
}, 

这是我的网格列:

{text: 'Home Location', dataIndex: 'homeLocationId', width: 250, xtype: 'location-column'},

【问题讨论】:

    标签: javascript extjs


    【解决方案1】:

    您可以将标签设置为 dataIndex 并附加带有显示“本国”的渲染器 这是工作示例:http://jsfiddle.net/KLX5q/

    // Monel
    Ext.define('CountryModel', {
        extend: 'Ext.data.Model',
        fields: [
           {   name:'homeCountryId', type:'int'},
           {   name:'HomeCountryName', type:'string'},
           {   name:'label', type:'string',
               convert: function (newValue, model) { 
                   // homeCountryId is appened for uniqueness and additional sort
                   return (newValue ? newValue : '' ) + '_' + model.get('homeCountryId');
             }
           }
        ]
    });
    
    // store
    Ext.create('Ext.data.Store', {
        storeId:'simpsonsStore',
        fields:['homeCountryId', 'HomeCountryName', 'label'],
        data:{'items':[
    Ext.create('CountryModel',{ homeCountryId: 1, HomeCountryName: 'Australia', label:'nnnn' }),
    Ext.create('CountryModel',{ homeCountryId: 2, HomeCountryName:'Germany',  label:'bbbb' }),
    Ext.create('CountryModel',{ homeCountryId: 3, HomeCountryName:'Russia',  label:'aaaa' }),
    Ext.create('CountryModel',{ homeCountryId: 4, HomeCountryName:'United States', label:'zzzz' })
        ]},
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        }
    });
    
    // gridpanel
    Ext.create('Ext.grid.Panel', {
        title: 'Countries',
        margin: 5,
        frame: true,
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [
            { 
                text: 'HomeCountryName', 
                dataIndex: 'label', 
                flex: 1,
                renderer: function(value, column, record){
                    return record.data.HomeCountryName;
                }
            }
            //,{ text: 'homeCountryId',  dataIndex: 'homeCountryId' } // uncomment if need
            //,{ text: 'display label', dataIndex: 'label' }
        ],
        height: 200,
        width: 400,
        renderTo: Ext.getBody()
    });
    

    【讨论】:

    • 我不确定你的意思是不是dataIndex: 'label',,我也不确定你的意思是renderer with display 'Home Country'。如果您有时间,能否请您举例说明一下?
    • 我改变了答案)
    • 感谢您的努力,但我无法正常工作。我有原籍国、东道国、原住地和东道国,所以我必须为每个国家创建单独的标签吗?我将您的示例更改为仅使用 HomeCountryName 作为 dataIndex 并且它似乎有效;但是,如果我在我的列上执行此操作,则该列将显示为空白。这是更新的 jsfiddle jsfiddle.net/KLX5q/2
    • 如果您将 Hometsountryname 设置为 dataIndex,那么排序将不是按标签,而是按 HomeCountryName。定义函数 myConvert(newValue, model) { return (newValue ? newValue : '' ) + '_' + model.get('homeCountryId'); } 并在每个字段中设置这样的 'convert: myConvert'
    • 我试过了(我的代码已在原始问题中更新)但它仍然不适合我。我做错了吗?
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2011-08-06
    • 1970-01-01
    相关资源
    最近更新 更多