PDA

View Full Version : How to get JSON to Form?



Cipher
21 Jun 2007, 7:52 AM
Hi, I'm trying make a Ext Form.

My Ext Code:


docForm = function(){
fds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: '/getDoc.php'}),
reader: new Ext.data.JsonReader(
{root: 'rows', id: 'id'},
[
{name: 'id'},
{name: 'title'}
]
)
});
fds.load({params:{id: docId},scope: this});
alert(fds.getAt(0)); // it appears "undefined".

//constructor
docForm.superclass.constructor.call(this, domFormId,{
labelWidth: 75,
url: 'save-form.php'
});

this.add(
new Ext.form.TextField({
fieldLabel: 'Title',
name: 'title',
value: fds.title, //I'm trying put value from JSON here
width: '400',
allowBlank: false
})
);

this.addButton('Save');
this.addButton('Cancel');
this.render('abc');
}

Ext.extend(docForm, Ext.form.Form, {

})

My JSON data :


{"rows":[
{"id":14,"title":"test"},
{"id":15,"title":"test1"},
{"id":16,"title":"test2"}
]}And FireBug review is attached.

My question is how to get JSON value in Form?
I tried to search forums posts and tried to change my JSON format, but it doesn't work. What's wrong with my code?

Cipher
21 Jun 2007, 5:23 PM
Please refer the post:
http://extjs.com/forum/showthread.php?t=6902&highlight=JSON+data.Store+form&page=1



ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: '/backend.php/aboutAjax/getDoc'}),
reader: new Ext.data.JsonReader(
{root: 'rows', id: 'id'},
[
{name: 'id'},
{name: 'title'}
]
)
});

ds.on("load", function() {
alert (ds.data.items[0].data.title); //notice: it will alert right data and ds.on() must put before ds.load
});
ds.load({params:{id: docId}});

alert(ds.data.items[0].data.title); //it will show error message.

Cipher
21 Jun 2007, 8:23 PM
Excuse me, another question.

ds.on("load", function() {
alert (ds.data.items[0].data.title); //notice: it will alert right data and ds.on() must put before ds.load
});
ds.load({params:{id: docId}});

alert(ds.data.items[0].data.title); //I can't get the value here

My question is : How to get value of ds.getAt(0) out of ds.on('load') function.

I tried these code:


var a;
ds.on('load', function(){
a = ds.getAt(0);
});
alert(a);
and

ds.on('load', function(){
this.a = ds.getAt(0);
});
alert(this.a);


But only got "undefined" message.

It will very helpful if you can give me some advice.

tryanDLS
22 Jun 2007, 9:26 AM
You need to look at your code in Firebug. Is ds in scope inside the function called by your load handler - look at args passed to the function?