Search code examples
javascriptarraysextjsstore

How to put array to store?


i got some EXTJS(javascript) code here:

var plik = document.getElementById("myFile");
var x = file_get_contents(plik.value);
var x = x.split(";");

and it returns:

["1", "2", "3", "4", "5", "645", "32213", "55645645", "123123213", "534534543534543242"]

I would like to put it to

        var store = new Ext.data.ArrayStore ({
                    data: myData,
                });

But i dont know, how to transform array correctly just as here:

 var myData = [
        ['3m Co'],
        ['Alcoa Inc'],
        ['Altria Group Inc'],
        ['American Express Company'],
        ['American International Group, Inc.']
];

Please, gimme some clues because it is annoying :(


Solution

  • Firstly you should configure your store with a field, see the example in the docs here http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.data.ArrayStore

    Next your response needs to be an array of arrays each containing one of those strings, so

    [["abc"],["def"],...]

    A quick example of how to make one of these

    var string = "a,b,c";
    var res = string.split(",");
    
    var x = [];
    for (var i = 0; i < res.length; i++) {
       x[i] = [res[i]];
    }
    
    alert(x[0][0]);
    

    You should be able to see how you can use this with your string split result.

    At the minute you are just returning a flat array of string values and the store reader doesn't know enough in order to parse it.

    That should be the minimum you need to start loading your store I think.