EXTJS 5:如何在EXT JS 5中对网格列进行排序

Mic*_*l P 5 javascript extjs extjs5

我最近将EXT JS的版本更新为5,并且覆盖doSort功能不再有效.有人在想怎么办?

覆盖范围:

{
    text: 'Custom',
    sortable : true,
    dataIndex: 'customsort',
    doSort: function(state) {
        var ds = this.up('grid').getStore();
        var field = this.getSortParam();
        ds.sort({
            property: field,
            direction: state,
            sorterFn: function(v1, v2){
                v1 = v1.get(field);
                v2 = v2.get(field);

                return v1.length > v2.length ? 1 : (v1.length < v2.length ? -1 : 0);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑1: 我只是尝试@tomgranerod的解决方案,但me.sortState始终是'undefined'.所以我这样做来更新我的变量:

    sort: function () {
        var me = this,
            grid = me.up('tablepanel'),
            store = grid.store;

        me.sortState = me.sortState === 'ASC' ? 'DESC' : 'ASC';

        Ext.suspendLayouts();
        me.sorting = true;
        store.sort({
            property: me.getSortParam(),
            direction: me.sortState,
            sortFn: function (v1, v2) {
                v1 = v1.get(field);
                v2 = v2.get(field);

                return v1.length > v2.length ? 1 : (v1.length < v2.length ? -1 : 0);
            }
        });
        delete me.sorting;
        Ext.resumeLayouts(true);
    }
Run Code Online (Sandbox Code Playgroud)

但是从不调用sortFn函数.我不知道为什么.===> !!!! 它适用于EXT JS 5.0.1,但始终不会调用sortFin函数.!!!!

编辑2: 这是我试图拥有的:

ASC:

if (v1 and v2 are numbers) return v1 > v2;
else if (v1 is a number and v2 a string) return false;
else if (v1 is a string and v2 a number) return true;
else if (v1 and v2 are strings) return v1 > v2;
Run Code Online (Sandbox Code Playgroud)

DESC:

if (v1 and v2 are numbers) return v1 < v2;
else if (v1 is a number and v2 a string) return true;
else if (v1 is a string and v2 a number) return false;
else if (v1 and v2 are strings) return v1 < v2;
Run Code Online (Sandbox Code Playgroud)

Jua*_*des 5

你重写了私有方法.所以几乎可以预期它会在重大发布后破裂.如果你看一下http://docs.sencha.com/extjs/5.0.0/apidocs/source/Column2.html#Ext-grid-column-Column你会发现它已经没有doSort了.

Ext的建议方法是使用sortTypeconfig可以使用一个函数将您的值转换为自然排序的东西,通常最简单的方法是将其转换为数字.因此,如果您想要稍微不同的东西,您可以修改我发布的代码来执行您想要的操作而不会覆盖私有方法.

运行示例:https://fiddle.sencha.com/#fiddle/8km

var store = Ext.create('Ext.data.Store', {
    fields: [{
        name: 'ref',
        sortType: function(str)  {
            // Ext-JS requires that you return a naturally sortable value
            // not your typical comparator function.
            // The following code puts all valid integers in the range 
            // Number.MIN_SAFE_INTEGER and 0
            // And assumes the other values start with T and sorts 
            // them as positive integers               
            var parsed = parseInt(str, 10); 
            if ( isNaN( parsed ) ){
                return parseInt(str.substring(1), 10);
            } else {
                return Number.MIN_SAFE_INTEGER + parsed;
            }          
        }
    }],
    data: {
        'items': [
            {'ref': '1'},
            {'ref': '12'},
            {'ref': 'T0134'},
            {'ref': '121878'},
            {'ref': 'T0134343'},
            {'ref': 'T01POPI'},
            {'ref': '103'},
            {'ref': 'T01'}            
        ]
    },
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            rootProperty: 'items'
        }
    }
});

Ext.create('Ext.grid.Panel', {
    title: 'Grid custom',
    store: store,
    columns: [{
        text: 'Reference',
        dataIndex: 'ref',
    }],
    height: 300,
    width: 400,
    renderTo: Ext.getBody()
});
Run Code Online (Sandbox Code Playgroud)

如果您要重用此功能,请查看http://spin.atomicobject.com/2012/07/20/simple-natural-sorting-in-extjs/

/** Sort on string length  */
Ext.apply(Ext.data.SortTypes, {
    myCrazySorter: function (str) {
        // Same as above
    }
});

// And use it like
var store = Ext.create('Ext.data.Store', {
    fields: [{
        name: 'ref',
        sortType: 'myCrazySorter'
    }],
Run Code Online (Sandbox Code Playgroud)

  • @MickaëlP我向您展示了使用示例实现自定义排序的正确方法,并且我在您的代码显示时实现了它.您是否尝试过实施您所描述的算法?这似乎超出了问题的范围 (2认同)