Results 1 to 5 of 5

Thread: Load a VERY SIMPLE string array in store

  1. #1
    Sencha User
    Join Date
    Jan 2015
    Posts
    99
    Answers
    11

    Default Answered: Load a VERY SIMPLE string array in store

    Hi guys,

    I have a back end service that gives me an object which only contains an array of string.
    This is for example what the service gives me:
    Code:
    {  
      "DepartementsResult": [
        "AME-CM",
        "BMAU",
        "BMKR",
        "BNVS"
      ]
    }
    So to get this data I want to create a nice and simple store, but a first problem appear: what should be the field???
    Code:
    var store = Ext.create('Ext.data.Store', {
        fields: ['data'], // What should be the fields here, I have none ^^"
        pageSize: 0,
        autoLoad: false,
        proxy: {
            type: 'ajax',
            url: 'data.json', // this file contains the data described above
            reader: {
                type: 'json',
                rootProperty: 'DepartementsResult'
            }
        }
    });
    And then when I want to create a combo using this store I don't know what I should write also:
    Code:
    var combo = Ext.create('Ext.form.field.ComboBox', {
        store: store,
        displayField: 'data', // what field should be displayed ^^" ?
        valueField: 'data', // same here I don't really know what to write
        fieldLabel: 'Departements',
        renderTo: Ext.getBody()
    });
    Here is the link https://fiddle.sencha.com/#fiddle/iau to the sencha fiddle with the code described bellow ! Thanks a lot !!

  2. The kind Digigizmo gave me a very clear answer on stackoverflow (http://stackoverflow.com/questions/2...93300#28593300), let me share it with you :

    Basically when I define fields in my store, the store except an array of objects which properties will match the fields. For example if I have a store like this:
    Code:
    Ext.define('Ext.data.Store', {
        fields: ['name'],
        ....
    });
    The expected data would have this format:
    Code:
    [{'name': 'michel'}, {'name': 'jean'}, ...]
    To parse the data returned by the server before it is parse by the store, I can use the transform method of the proxy reader, just like this:
    Code:
    var store = Ext.create('Ext.data.Store', {
        fields: ['name'],
        proxy: {
            type: 'ajax',
            url: 'data.json',
            reader: {
                type: 'json',
                transform: function(data){
                    data = data.map(function(val){
                        return { name: val };
                    });
                    return data;
                }
            }
        }
    });
    Thanks again to Digigizmo !

  3. #2
    Sencha User
    Join Date
    Feb 2015
    Posts
    11

    Default

    Hi,Don't have a lot of experience with ExtJS but a quick look at the fiddle, could you maybe alter the way how your service returns the data?Because with surrounding each string with brackets works for me. Like this:
    Code:
    {  "DepartementsResult": [    ["AME-CM"],    ["BMAU"],    ["BMKR"],    ["BNVS"],    ["BSLGJ"],    ["BSLM2"],    ["BSV"],    ["CAM"],    ["CCDA"],    ["CCDA1"],    ["CCDA2"],    ["CCDG"],    ["CCDGA"],    ["CCDGO"],    ["CCDGS"],    ["CCDGT"],    ["CCDO"],    ["CCDOA"],    ["CCDOP"],    ["CCDOT"],  ]}

  4. #3
    Sencha User
    Join Date
    Jan 2015
    Posts
    99
    Answers
    11

    Default

    Hi dylanvdb, thanks a lot for your answer =) ! The point is that I can't modify the string returned by the server ^^"

  5. #4
    Sencha User
    Join Date
    Jan 2015
    Posts
    99
    Answers
    11

    Default

    The kind Digigizmo gave me a very clear answer on stackoverflow (http://stackoverflow.com/questions/2...93300#28593300), let me share it with you :

    Basically when I define fields in my store, the store except an array of objects which properties will match the fields. For example if I have a store like this:
    Code:
    Ext.define('Ext.data.Store', {
        fields: ['name'],
        ....
    });
    The expected data would have this format:
    Code:
    [{'name': 'michel'}, {'name': 'jean'}, ...]
    To parse the data returned by the server before it is parse by the store, I can use the transform method of the proxy reader, just like this:
    Code:
    var store = Ext.create('Ext.data.Store', {
        fields: ['name'],
        proxy: {
            type: 'ajax',
            url: 'data.json',
            reader: {
                type: 'json',
                transform: function(data){
                    data = data.map(function(val){
                        return { name: val };
                    });
                    return data;
                }
            }
        }
    });
    Thanks again to Digigizmo !

  6. #5
    Sencha Premium User Nux's Avatar
    Join Date
    Feb 2016
    Location
    3city, Poland
    Posts
    39

    Default

    Just remember `.map` doesn't work in IE8, so you should probably use a loop like:
    PHP Code:
                transform: function(data){
                    var 
    objectsData = [];
                    for (var 
    0data.lengthi++) {
                        
    objectsData.push({ namedata[i] });
                    }
                    return 
    objectsData;
                } 
    Also note that `rootProperty` doesn't apply here. So if data from your server looks like this:
    PHP Code:
    {  
      
    "DepartementsResult": [
        
    "AME-CM",
        
    "BMAU",
        
    "BMKR",
        
    "BNVS"
      
    ]

    Then the loop would be:
    PHP Code:
                    for (var 0data.DepartementsResult.lengthi++) {
                        
    objectsData.push({ namedata.DepartementsResult[i] });
                    } 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •