Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remotely sort an Ext grid column that renders a nested object?

Simple question here, and I'm really surprised I cannot find an answer to it anywhere.

I have the following product model:

Ext.define('Product', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id', type: 'int'},
        {name: 'name', type: 'string'},
        {name: 'manufacturer', type: 'auto'}, // i.e. nested object literal
    ],
});

As can be seen, a product has a nested object inside it that contains details about a manufacturer (id, name, description, etc.).

I display products in an Ext.grid.Panel, like so:

Ext.create('Ext.grid.Panel', {
    title: 'Products',
    ...
    remoteSort: true,
    columns: [
        {text: 'Id', dataIndex: 'id', sortable: true},
        {text: 'Name',  dataIndex: 'name', sortable: true},
        {text: 'Manufacturer', dataIndex: 'manufacturer', sortable: true, renderer: function(manufacturer) {
                return manufacturer.name; // render manufacturer name
            }
        },
    ],
});

As you can see, all three columns are configured to be sortable, and I am using remote sorting. This works for the first two columns, but the third column (manufacturer) is giving me trouble.

For instance, when a user sorts the grid by product name, Ext sends the following JSON to the server:

{ sort: [{ property: 'name', direction: 'ASC' }] }

This is fine, because the server knows to simply sort by each product's name property.

But when a user sorts the grid by manufacturer, the JSON sent is:

{ sort: [{ property: 'manufacturer', direction: 'ASC' }] }

Since the manufacturer is an object, this does not give the server any idea which property of the manufacturer it should sort by! Is it the manufacturer's id? Or is it the manufacturer's name? Or something else?

For my grid, since I render the manufacturer's name, I'd like to sort it by name. But I see no way to tell the server this. And I certainly don't want to make my server just sort by the manufacturer's name all the time.

If the JSON was sent like this it would be ideal:

{ sort: [{ property: 'manufacturer.name', direction: 'ASC' }] }

Sadly, Ext does not seem to do that (?). So, what's the solution?

like image 789
XåpplI'-I0llwlg'I - Avatar asked Feb 09 '13 13:02

XåpplI'-I0llwlg'I -


1 Answers

Okay, I asked on the Sencha Forums and got a response. It appears you can override getSortParam() in the column config. Example:

columns: [
    ...
    {
        header: 'Manufacturer',
        dataIndex: 'manufacturer',
        sortable: true,
        renderer: function(manufacturer) {
            return manufacturer.name; // render manufacturer name
        },
        getSortParam: function() {
            return 'manufacturer.name';
        },
    }
    ...
]

This will send the ideal JSON I described in my OP. Now I just need to make my server parse this properly! :)

like image 166
XåpplI'-I0llwlg'I - Avatar answered Sep 20 '22 22:09

XåpplI'-I0llwlg'I -