如何从一个不寻常的

如何从一个不寻常的

本文介绍了如何从一个不寻常的 JSON 存储创建 Ext.data.Store?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 JSON 存储,但它没有正确编码.它的正确语法是什么?

I have this JSON store but it`s not coded correctly. What is the correct syntax for it?

Ext.define('MA.store.Language', {
extend : 'Ext.data.Store',
fields : [ {
    name : 'id'
}, {
    name : 'name'
} ],
data : [ {
    "aa" : "Afar",
    "ab" : "Abkhazian",
    "ace" : "Achinese",
    "ach" : "Acoli",
    "ada" : "Adangme",
    "ady" : "Adyghe",
    "ae" : "Avestan",
    "af" : "Afrikaans",
    "afa" : "Afro-Asiatic Language",
    "afh" : "Afrihili",
    "ain" : "Ainu",
    "ak" : "Akan"
} ]
});

我需要这家商店来做这样的组合框,但它不起作用:

I need this store for a combobox like this but it wont work:

{
xtype : 'combo',
name : 'language',
fieldLabel : 'Language',
store : 'Language',
queryMode : 'local',
displayField : 'name',
valueField : 'id',
typeAhead : true,
forceSelection : true
}

推荐答案

所以你无论如何都想使用你不寻常的 JSON.在这种情况下,您可以通过定义自己的阅读器来解决您的问题.像这样:

So you want to use your unusual JSON anyway. In this case you can solve your problem by defining your own reader. Like this:

Ext.define('MA.reader.Language', {
    extend: 'Ext.data.reader.Json',
    alias: 'reader.Language',
    read: function (response) {
        var data = [];
        for (var i in response[0])
            data.push({
                id: i,
                'name': response[0][i]
            });
        return this.callParent([data]);
    }
});

Ext.define('MA.store.Language', {
    extend: 'Ext.data.Store',
    fields: [{
        name: 'id'
    }, {
        name: 'name'
    }],
    data: [{
        "aa": "Afar",
        "ab": "Abkhazian",
        "ace": "Achinese",
        "ach": "Acoli",
        "ada": "Adangme",
        "ady": "Adyghe",
        "ae": "Avestan",
        "af": "Afrikaans",
        "afa": "Afro-Asiatic Language",
        "afh": "Afrihili",
        "ain": "Ainu",
        "ak": "Akan"

    }],
    proxy: {
        type: 'memory',
        reader: {
            type: 'Language'
        }
    }
});

var store = Ext.create('MA.store.Language', {
    storeId: 'Language'
});

var cc = Ext.widget('combo', {
    xtype: 'combo',
    name: 'language',
    fieldLabel: 'Language',
    store: 'Language',
    queryMode: 'local',
    displayField: 'name',
    valueField: 'id',
    typeAhead: true,
    forceSelection: true
});

cc.render(Ext.getBody());

工作示例

这篇关于如何从一个不寻常的 JSON 存储创建 Ext.data.Store?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:55