PDA

View Full Version : Problem populating a combobox with json



Doomsday
31 May 2007, 8:40 PM
hi.. im new here and id spend the last 3 hours trying to populate a combobox without having to reformat all my arrays.

heres the problem
i have an application in php that have a array of objects, web i pass this array for json_encode i get this


[{"id_bodega":"5","id_sucursal":"16","descripcion":"Bodega Central (5)","estado":"0"},{"id_bodega":"4","id_sucursal":"16","descripcion":"Bodega Ventas (4)","estado":"0"},{"id_bodega":"2","id_sucursal":"24","descripcion":"Bodega Prat (2)","estado":"0"},{"id_bodega":"3","id_sucursal":"21","descripcion":"Bodega Iquique (3)","estado":"0"}]so i pass the encode array to a variable $json_list and passit to the template to "print it" in the html

now... how can i use this json array to populate my combobox?

i used



var store = new Ext.data.SimpleStore({
fields: ['id_sucursal', 'descripcion'],
data : <? echo $json_listado; ?>
});
but doesn't work

and yes.. i already swim in the docs and forums....
any help?

PS: sorry about my english ;)

Animal
31 May 2007, 10:59 PM
That looks OK as far as it goes. The Store is the cache of data. You can debug it to check it contains the data you think it should using Firebug. You should be using Firebug if you want to get things working.

What about the code where you create and render the combobox? Show us that.

Doomsday
1 Jun 2007, 7:05 AM
the combobox code is copypasted from the examples...



var combo = new Ext.form.ComboBox({
store: store,
displayField:'descripcion',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText:'Select a state...',
selectOnFocus:true
});
combo.applyTo('local-states');



the doubt that i have its the storage format... in the example its an array of just 2 elements without ani element description



[
['AL', 'Alabama'],
['AK', 'Alaska'],
...
}


but mine has more atributes and the atribute names on it



[
{
"id_bodega":"5",
"id_sucursal":"16",
"descripcion":"Bodega Central (5)",
"estado":"0"
}
...
]



any way to tell the combobox witch fields to use?

Doomsday
1 Jun 2007, 2:04 PM
finnaly i make it work :)

the final code to populate a combobox




<script>

Ext.onReady(function(){

var store = new Ext.data.Store({
reader : new Ext.data.JsonReader({},['id', 'descripcion'])
});
store.loadData(<?=$json_listado?>);

var combo = new Ext.form.ComboBox({
store: store,
displayField:'descripcion',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText:'Seleccione una bodega...',
selectOnFocus:true
});
combo.applyTo('local-states');

});

</script>