2

在下面的代码中,我在嵌套列表中获取静态数据。但是代替静态数据,我想要动态数据,即从jsp获取的数据。我不知道该怎么做。

Ext.define('SenchaApp.store.Items', {
    extend: 'Ext.data.TreeStore',

    config: {
        model: 'SenchaApp.model.Item',
        defaultRootProperty: 'items',
        root: {
            items: [
            {
                text: 'Categories1',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
            {
                text: 'Categories2',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
             {
                text: 'Categories3',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
             {
                text: 'Categories4',
                items: [
                    {
                        text: 'Subcategories1',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories2',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories3',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },
                    {
                        text: 'Subcategories4',
                        items: [
                            { text: 'Product1', leaf: true },
                            { text: 'Product2', leaf: true },
                            { text: 'Product3', leaf: true },
                            { text: 'Product4', leaf: true }
                        ]
                    },

                ]
            },
        ]
    }
    }
});
4

1 回答 1

0

实际上你不必在这里使用 JSONP。将 Ajax 请求与 JSON 阅读器一起使用要容易得多。

根据您的代码,我想您的模型定义应该是这样的:

Ext.define('SenchaApp.model.Item', {
        extend: 'Ext.data.Model',
        config: {
            fields: ['text'],
        }
});

然后这将从服务器获取 JSONP 并填充您的商店:

Ext.define('SenchaApp.store.Items', {
    extend: 'Ext.data.TreeStore',

    config: {
        model: 'SenchaApp.model.Item',
        proxy: {
            type: 'ajax',
            url: enter_your_api_url_here,
            extraParams: {set your extra params if needed},
            reader: {
                type: 'json',
                rootProperty: 'items'
            }
        },
        autoLoad: true,
    }
});

希望能帮助到你。

PS:如果它不起作用,请在此处复制并粘贴您的 jsonp 文件链接,我会帮助您。

于 2012-04-11T07:34:34.110 回答