Search code examples
extjsextjs3

How do I do a custom sort for a store in ExtJS?


I currently have the code below that overrides the sort for all stores. What I need to do is create a sort for an individual store. How do I do that?

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));
        };
    }

What I want to implement is something like this:

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?
}

Solution

  • I am not sure if I understood the purpose of your code correctly but I believe the same effect can be achieved by using the following code (sorry, I haven't tested it but it should work).

    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);
        }
    });
    

    If this does not achieve your desired behaviour because I missed something, you can just override the createSortFunction instead of sort. However, since createSortFunction is supposed to be private, it is better to handle custom logic by overriding sort. Anyway, if you prefer to stay with your first approach, your AircraftStore should look like this:

    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
        };
    });