PDA

View Full Version : getDocument for JSON?



pomata
16 Jan 2007, 11:10 AM
Hi,

any easy way to send json datamodel to server in order to save data after CellUpdated ?

I don´t want to send data on each onCellUpdated, but the whole datamodel via a button.

with xml, you can use http://www.yui-ext.com/forum/viewtopic.php?t=124

what about Json?

tx

tryanDLS
16 Jan 2007, 11:19 AM
There's currently no way to do this because the JSONDataModel doesn't maintain the JSON string data you passed to loadData. It uses this to build an array of rows. If you want to pass updates back to the server, you'll have to take this array and serialize it back to a JSON string before posting.

pomata
16 Jan 2007, 11:42 AM
Thanks,

any easy way I can loop grid rows and retrieve data?

any code?

many tx again.

P

tryanDLS
16 Jan 2007, 11:50 AM
grid.dataModel.data is a simple 2 dimensional array that you loop like any other.

jbraband
31 Jan 2007, 2:18 PM
i wrote a getJSON method for JSONDataModel. it returns an identical object to that which was passed into loadData (except with the changes made via the Grid interface).



getJSON: function() {
var json = {};
json[this.schema.root] = [];

var idField = this.schema.id;
var fields = this.schema.fields;

var rowIndexes = [];
for(var i = 0; i < this.getRowCount(); i++) {
rowIndexes.push(i);
}

var rows = this.getRows(rowIndexes);
rows = this.data;
for (var i = 0; i < rows.length; i++) {
var thisRow = rows[i];
var rowData = [];
for(var j = 0; j < fields.length; j++) {
var val = thisRow[j];
if(typeof val == 'undefined'){
val = '';
}
if(this.postprocessors[j]){
val = this.postprocessors[j](val);
}
rowData[fields[j]] = val;
}
json[this.schema.root].push(rowData);
}
return json;
}


jack, can we get this add into the core code (or something similar with your magical twist :P )

jbraband
19 Feb 2007, 4:43 PM
a rather major fix to this function :oops:

rowData needs to be an object and thus instantiated with {} instead of []

var rowData = [];

becomes

var rowData = {};

new code....



getJSON: function() {
var json = {};
json[this.schema.root] = [];

var idField = this.schema.id;
var fields = this.schema.fields;

var rowIndexes = [];
for(var i = 0; i < this.getRowCount(); i++) {
rowIndexes.push(i);
}

var rows = this.getRows(rowIndexes);
rows = this.data;
for (var i = 0; i < rows.length; i++) {
var thisRow = rows[i];
var rowData = {};
for(var j = 0; j < fields.length; j++) {
var val = thisRow[j];
if(typeof val == 'undefined'){
val = '';
}
if(this.postprocessors[j]){
val = this.postprocessors[j](val);
}
rowData[fields[j]] = val;
}
json[this.schema.root].push(rowData);
}
return json;
}



I snooped around the 1.0 source a little bit and didnt see a data retrieval mechanism in the JsonReader class. did I miss something? or can it be added? i think that its invaluable.

-j