最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remotely sort an Ext grid column that renders a nested object? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Nov 20, 2013 at 6:29 XåpplI'-I0llwlg'I - asked Feb 9, 2013 at 13:58 XåpplI'-I0llwlg'I -XåpplI'-I0llwlg'I - 22.4k28 gold badges107 silver badges154 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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! :)

发布评论

评论列表(0)

  1. 暂无评论