PDA

View Full Version : Json data problem in combobox



ahsan
1 Mar 2010, 8:47 PM
Hi all,

I have faced a problem to load data in a combobox which is in json format. My code is:



Json data return format:
d:[{"9":"chittagong","10":"Dhaka","11":"Rajshahi","12":"Shylet"}]



var dstore = new Ext.data.JsonStore({
autoLoad: true,
proxy: new Ext.data.HttpProxy({
url: 'Service.asmx/PopulateDistrict',
method: 'GET', // ASP.NET WebService call in GET format
headers: {
'Content-type': 'application/json' // IMPORTANT! Without this FireFox will not understand WebService response
}
}),
root: 'd', // Root Json element, always 'd'
id: 'Id', // Identifier column, ExtJS needs it to recognize rows
fields: ['Id', 'name'] // Simple definition of columns
});

// convert html combo top ExtJs combo
var ComboDistrict = new Ext.form.ComboBox({
store: dstore,
id: 'cmbDistrict',
name: 'cmbDistrict',
transform: 'cmbDistrict',
valueField: 'Id',
displayField: 'name',
mode: 'local',
typeAhead: false,
editable: true,
triggerAction: 'all',
emptyText: 'Select a district...',
selectOnFocus: true
});


I am new to use json and extjs can any one guide me.

thanks & regards

Ahsan

kneth
1 Mar 2010, 10:55 PM
you should use the fields (those you defined in the JsonStore config) as array index for the values/text. So your json data return format should be:


{"d":[{"Id":"9","name":"chittagong"},{"Id":10,"name":"Dhaka"},{"Id":11,"name":"Rajshahi"},{"Id":12,"name":"Shylet"}]}

ahsan
1 Mar 2010, 11:20 PM
Hi kneth,
Thanks for your reply.

I just place this two data into a hash table and it returns from asp.net web method in this format:

d:[{"9":"chittagong","10":"Dhaka","11":"Rajshahi","12":"Shylet"}]would you give me any idea how to format the data into json format you describe earlier.
Service.asmx/PopulateDistrict contains this code lik this,


[web Method]
[ScriptService....Responseformat=Response.json....... .....]
{
.....
HashTable ht=new HashTable();

Datatable dt =new Datatable();
if(dt.rows.count>0)
{
foreach(DataRow row in dt.rows)
{
ht.Add(row["id"].toString,row["name"].toString);
}
}
return ht;

....
}

kneth
2 Mar 2010, 12:22 AM
Hi kneth,
Thanks for your reply.

I just place this two data into a hash table and it returns from asp.net web method in this format:

d:[{"9":"chittagong","10":"Dhaka","11":"Rajshahi","12":"Shylet"}]would you give me any idea how to format the data into json format you describe earlier.
Service.asmx/PopulateDistrict contains this code lik this,


[web Method]
[ScriptService....Responseformat=Response.json....... .....]
{
.....
HashTable ht=new HashTable();

Datatable dt =new Datatable();
if(dt.rows.count>0)
{
foreach(DataRow row in dt.rows)
{
ht.Add(row["id"].toString,row["name"].toString);
}
}
return ht;

....
}




sorry i have no background with asp.net. :D

Based on your code: if you have the function/method in asp.net which converts the dt (Datatable) to JSON format, that would be fine as long as you change the Ext.data.JsonStore field configuration from
fields: ['Id', 'name'] to
fields: ['id', 'name'].

ahsan
2 Mar 2010, 2:20 AM
If anyone have any idea pls give some guide line.

ahsan
2 Mar 2010, 8:34 PM
Using List<> I have solved that issue.
Thanks to all for their contribution.