PDA

View Full Version : Question on Passing a javascript array to an Ext.Data.ArrayStore



Planet_x
14 Oct 2010, 2:03 PM
I am trying to build an arraystore for use with an Ext.Combobox. I already have an array but it contains string data except there is no '[' characters inside it, only strings. Can I set the data attribute of the arraystore to somehow use my existing javascript array instead of going through it and trying to add the [] and ' ' around each string so that it fits in with the datastore? Not sure if this make sense or even if I should use an arraystore or something better to read in my existing array so I can use it as a store for the combobox.. really appreciate any assistance, thanks.

Animal
14 Oct 2010, 9:17 PM
It doesn't make sense.

Array's don't contain '[' characters. Arrays contain objects. Like strings.

Planet_x
15 Oct 2010, 5:26 AM
Yes I know but I see from examples where data is loaded and I had previously loaded my data into the store and when the data is loaded it is set like data: [['string1',['string2'],['string3']] so my question is can I use a premade javascript array which of course has string data like 'string1', 'string2', etc into the arraystore without changing the format of it?

Animal
15 Oct 2010, 5:31 AM
I don't get what you are saying.

An array is

['string1', 'string2', 'string3']

So loading a Store requires MULTIPLE rows, so an Array of Arrays if you are using an ArrayReader to create Records.



[
[ 'fla1val', 'fld2val'],
[...],
[...]
]

Planet_x
15 Oct 2010, 6:00 AM
Ok so an array of arrays is required. But I am loading my data into my original array with this code -

var request_neighbourhoods = OpenLayers.Request.GET({
url: "http://localhost/RedWFS/request.aspx?REQUEST=getFeature&SERVICE=WFS&TYPENAME=Neighbourhoods&version=1.1.0",
async: false
});
//split request_neighbourhoods into arraylist of neighbourhood names
var g = new OpenLayers.Format.GML;
var neighbourhood_Array = new Array();
var features = g.read(request_neighbourhoods.responseText);
for(var feat in features)
{
var attributes;
for (var j in features[feat].attributes)
{
if (j == "NEIGHBOURHOOD_NAME")
{
var strNeighbourhood = new String();
strNeighbourhood = features[feat].attributes[j]
neighbourhood_Array.push(strNeighbourhood);
}
else
{
// do nothing because the attribute is not Neighbourhood_Name
}

}
}

so neighbourhood_Array contains much data with ['string1','string2',] etc. So would I put this into the
var myData = [neighbourhood_Array]

var ds = new Ext.data.Store (http://www.sencha.com/learn/Manual:Data:Stores:Store)({
proxy: new Ext.data.MemoryProxy(myData),
reader: new Ext.data.ArrayReader (http://www.sencha.com/learn/Manual:Data:Readers:ArrayReader)({}, [
{name: 'name'}
])
});

And then can I load this into the combo box?

Planet_x
15 Oct 2010, 9:18 AM
So I have it somewhat working with the below code except one issue. My displayField is not working correctly and obviously it isn't loading the data properly (from neighbourhood_Array) because only the very first field is displaying when I click the dropdown box but the rest of my fields are not, any idea why? I realize it is only showing the field with the ID 0, do I have to do something to show each field? It goes from 0-78 for IDs but not sure where to put this. Appreciate the assistance.

Ext.namespace('Ext.queries');
Ext.queries.neighbourhoods = [neighbourhood_Array];
var neighbourhood_Combo = new Ext.form.ComboBox({
fieldLabel: 'name',
hiddenName: 'name',
store: new Ext.data.ArrayStore({
fields: ['name'],
data: Ext.queries.neighbourhoods
}),
valueField:'name',
displayField:'name',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText: 'Select a Neighbourhood',
selectOnFocus: true,
width: 190
});

var neighbourhoods = [{
bodyStyle: 'padding-right:5px;',
items: {
xtype: 'fieldset',
title: 'Neighbourhood Search',
autoHeight: true,
defaultType: 'checkbox', // each item will be a checkbox
items: [neighbourhood_Combo]
}

}];

Planet_x
15 Oct 2010, 9:56 AM
Ok got it to work correctly. It was an issue with my original array just being a single array instead of Array of Arrays. I modified the push method to push the data like so -

var strNeighbourhood = new String();
strNeighbourhood = features[feat].attributes[j];
neighbourhood_Array.push([strNeighbourhood]);

Then I was able to load the data into the combobox properly -

Ext.namespace('Ext.queries');
Ext.queries.neighbourhoods = neighbourhood_Array;

var neighbourhood_Combo = new Ext.form.ComboBox({
fieldLabel: 'Neighbourhood Name',
hiddenName: 'name',
store: new Ext.data.ArrayStore({
fields: ['name'],
data: Ext.queries.neighbourhoods
}),
valueField:'name',
displayField:'name',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText: 'Select a Neighbourhood',
selectOnFocus: true,
width: 190
});

Animal
15 Oct 2010, 10:35 AM
Why are you using some horrible software to get the data?

OpenLayers.Request.GET? synchronous? Bad idea.

Just let the Store load itself from that URL

Planet_x
15 Oct 2010, 10:47 AM
I was using that method because I could not get a GeoExt.Data.AttributeStore to work it would not return the correct attributes from my WFS and I cannot use JSON. Does Ext allow me to load the data directly from the WFS request? How would I go about doing that, any example available?

Animal
15 Oct 2010, 11:45 AM
Stores load themselves directly from a URL.

Planet_x
15 Oct 2010, 11:48 AM
Ok thanks, I will modify my code to use that instead.. and obviously you were correct about the synchronous call... I am new to ajax etc and loading the neighbourhood names was fine and took a split second but switching to parcel addresses of course was way too much data and caused my map server to crash :) I appreciate your advice.