PDA

View Full Version : Populate Form Data to JSon



jojo3091
28 Jun 2013, 1:09 AM
Hi Team,




I want to populate form data and some extra information (which is not direct form fields) to existing JSON file and send to server.
The sample JSON format is:


"@class": "com.xyz.his.adt.vo.Example",
"ipdaSeqId": "",
"discAdviseRaisedBy": "",
"discAdviseDate": "",
"expectedDischDate": "",
"gnDischDestination": ",
"remarks": "",
"absInformedTo": "",
"abscondingDate": "",
"deathDate": "",
"isPostmartenReq": "",
"informedTo": "",
"facilityId": {
"com.xyz.his.adt.vo.ExampleMaster": {
"code": "",
"description": ""
}
},
"ipdaId": "",
"doctor": {
"com.xyz.his.adt.vo.ExampleDoctor": {
"doctSeqId": "",
"firstname": ""
}
},
....
....
....


Note that there are composite objects inside an object.


I want to do something like this :- discAdviseRaisedBy = form.getValues().dischargeAdvice_remarks;
and update the JSON with dynamic form values.


Quick response is appreciated.


Thanks in Advance.

tvanzoelen
28 Jun 2013, 2:28 AM
Do you mean this?

To send data to the server



var params = form.getValues();

//add or change some data

params['name'] = 'whatever';

Ext.Ajax.request({
url: 'urltoserver',
params: params,
success: function(response, opts) {
var resp = Ext.decode(response.responseText);
//set new values in the form
form.setValues(resp.data);//or the property you have the new data
},
failure: function(response, opts) {

}
});

jojo3091
28 Jun 2013, 4:55 AM
Hi there!!

I know the procedure to send data to server, but before sending, I need to construct the request which is in JSON format.
I have the JSON structure that needs to be sent to server, my problem is to fill that JSON with form data on click of save button.
What I'm doing currently is:

//Construct the JSON string in reqd format, manuallyvar mainObj = new Object();
mainObj.classString =
"com.xyz.his.adt.vo.DischargeAdvise";
mainObj.ipdaSeqId = "123";
mainObj.discAdviseRaisedBy = "Qwerty";
mainObj.discAdviseDate = "01/01/2013";
mainObj.expectedDischDate = "11";
mainObj.gnDischDestination = "11";
mainObj.remarks = "11";
mainObj.absInformedTo = "11";
mainObj.abscondingDate = "11";
mainObj.deathDate = "11" ;
mainObj.isPostmartenReq = "11";
mainObj.informedTo = "11";
//Facility Part
var facility = new Object();
facility.facility1 = "x";
facility.facility2 = "y";

var faclPart = new Object();
faclPart.FacilityMasterTBR = facility;
mainObj.facilityId = faclPart;
mainObj.ipdaId = "123";
//DoctorPart
var doctor = new Object();
doctor.doctorDetails1 = "x";
doctor.doctorDetails2 = "y";

var doctorPart = new Object();
doctorPart.DoctorTBR = doctor;
mainObj.doctor = doctorPart;
var jsonString = JSON.stringify(mainObj, null,2);
This will generate the below JSON:

"ipdaSeqId": "123",
"discAdviseRaisedBy": "Qwerty",
"discAdviseDate": "01/01/2013",
"expectedDischDate": "11",
"gnDischDestination": "11",
"remarks": "11",
"absInformedTo": "11",
"abscondingDate": "11",
"deathDate": "11",
"isPostmartenReq": "11",
"informedTo": "11",
"facilityId": {
"com.xyz.his.adt.vo.masters.FacilityMaster": {
"code": "x",
"description": "y"
}
},
"ipdaId": "123",
"doctor": {
"com.xyz.his.adt.vo.emrmasters.Doctor": {
"doctSeqId": "x",
"firstname": "y"
}
},

server.buildServiceRequest(jsonString); //call server method.

The above proceduere is not feasable if theer are more no.of fields.What I'm looking for is - some how prepare this JSON structure with form data populated/ set dynamically.
Any better approach for achieving this as, I already have the JSON format, just fill the values with form data.
Note: The JSON should show the composite assiciation between the objects.

Thanks in advance.

tvanzoelen
28 Jun 2013, 5:29 AM
If you have your json in a string and you want to add properties to it

create an object first


var jsonObj= Ext.decode(jsonstring);
jsonObj['newproprty'] = 'newvalue';
var newJsonObj = Ext.encode(jsonObj);