How to create Ext.data.Store from an unusual JSON store?

前端 未结 3 860
时光取名叫无心
时光取名叫无心 2021-01-15 00:38

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\',
fiel         


        
相关标签:
3条回答
  • 2021-01-15 01:05

    For your example you have to modify your data to this:

    data : [
        { id: "aa", 'name' : "Afar" },
        { id: "ab", 'name' : "Abkhazian" },
        ...
    ]
    
    0 讨论(0)
  • 2021-01-15 01:09

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

    EDIT: working example

    0 讨论(0)
  • 2021-01-15 01:18

    Firstly, in your combobox configuration you must have: store : Ext.create('MA.store.Language'), instead of: store : 'Language',

    And secondly, your store definition must look like this:

    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"
    } ],
    
    read : function() {
     var me = this; 
     var oldData = me.getProxy().data[0];
     var data = [];
     for (var prop in oldData) {
        if (oldData.hasOwnProperty(prop)) {
            data.push({
                id: prop,
                name: oldData[prop]
            });
        }
     } 
     me.loadData(data);
    }
    
    });
    
    And it will work as you expect with your combobox.

    EDIT: instead of this:

    data.push({
        id: prop,
        name: oldData[prop]
      });
    
    I had this:
    data.push({
        id: prop,
        value: oldData[prop]
      });
    

    0 讨论(0)
提交回复
热议问题