如何在ExtJS中为商店进行自定义排序?

时间:2012-09-26 13:16:04

标签: extjs extjs3

我目前有以下代码覆盖所有商店的排序。我需要做的是为单个商店创建一个排序。我该怎么做?

Ext.override(Ext.data.Store, {
    // override
    createSortFunction: function (field, direction) {
        direction = direction || "ASC";
        var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
        var sortType = this.fields.get(field).sortType;

        //create a comparison function. Takes 2 records, returns 1 if record 1 is greater,
        //-1 if record 2 is greater or 0 if they are equal
        return function (r1, r2) {

            var v1;
            var v2;

            if (field == 'Registered') {
                v1 = sortType(r1.data['AircraftNeedsRegistered']);
                v2 = sortType(r2.data['AircraftNeedsRegistered']);

                if (r1.data['AircraftNeedsRegistered'])
                    r1.data['Register'] = !r1.data['Register'];

                if (r2.data['AircraftNeedsRegistered'])
                    r2.data['Register'] = !r2.data['Register'];

                //store.getAt(rowIndex).data['Registered'] = true;
            }
            else {
                v1 = sortType(r1.data[field]);
                v2 = sortType(r2.data[field]);
            }

            // To perform case insensitive sort
            if (v1.toLowerCase) {
                v1 = v1.toLowerCase();
                v2 = v2.toLowerCase();
            }

            return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0));
        };
    }

我想要实现的是这样的:

function AircraftStore() {
return new Ext.data.JsonStore(Ext.apply({
    url: AVRMS.ROOT_CONTEXT + "/ssl/json/general/GetAircraftByOwnerId.aspx",
    idProperty: 'OwnerOid',
    baseParams: { OwnerOid: 0 },
    fields: ['AircraftOid', 'NNumber', 'Make', 'Model', 'Seats', 'RegistrationType', 'Airworthy', 'IsFaaAirport', 'AirworthyString', 'IsFaaAirportString', 'Airport', 'AircraftNeedsRegistered', 'Register'],
    sort: function (field, direction) {
        return customSort(field, direction);
    }
}));
    };

function customSort(field,direction) {
    //What do I put here?
}

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解了您的代码的目的,但我相信使用以下代码可以达到相同的效果(对不起,我没有测试它但它应该可以工作)。

AircraftStore = Ext.extend(Ext.data.JsonStore, {
    url: AVRMS.ROOT_CONTEXT + "/ssl/json/general/GetAircraftByOwnerId.aspx",
    idProperty: 'OwnerOid',
    baseParams: { 
        OwnerOid: 0 
    },
    fields: ['AircraftOid', 'NNumber', 'Make', 'Model', 'Seats', 'RegistrationType', 'Airworthy', 'IsFaaAirport', 'AirworthyString', 'IsFaaAirportString', 'Airport', 'AircraftNeedsRegistered', 'Register'],
    sort: function (field, direction) {
        if (field == 'Registered') {
            field = 'AircraftNeedsRegistered';
            this.query('AircraftNeedsRegistered', true).each(function(record) {
                record.data['Register'] = !record.data['Register'];
            });         
        }
        return AircraftStore.superclass.sort.call(this, field, direction);
    }
});

如果由于错过了某些内容而无法实现您想要的行为,则可以覆盖createSortFunction而不是sort。但是,由于createSortFunction应该是私有的,因此最好通过覆盖sort来处理自定义逻辑。无论如何,如果您希望保留第一种方法,您的AircraftStore应该如下所示:

AircraftStore = Ext.extend(Ext.data.JsonStore, {
    url: AVRMS.ROOT_CONTEXT + "/ssl/json/general/GetAircraftByOwnerId.aspx",
    idProperty: 'OwnerOid',
    baseParams: { 
        OwnerOid: 0 
    },
    fields: ['AircraftOid', 'NNumber', 'Make', 'Model', 'Seats', 'RegistrationType', 'Airworthy', 'IsFaaAirport', 'AirworthyString', 'IsFaaAirportString', 'Airport', 'AircraftNeedsRegistered', 'Register'],
    createSortFunction: function (field, direction) {
        // copy your createSortFunction here
    };
});