PDA

View Full Version : Fetching record from json based on condition



bunty
20 Oct 2012, 7:46 PM
I want to fetch data from JSON based on the employeeID. This is how the JSON data looks:


{"empDetails":

[
{
"employeeId": 1,
"employeeName": 'Anton DSouz',
"age": 25,
"birthday": '05/05/1988',
"sex": 'Male'
},
{
"employeeId": 2,
"employeeName": 'John Hussain',
"age": 26,
"birthday": '03/25/1987',
"sex": 'Female'
} ]}This is the code which I am using to fetch the data based on employeeID:
Ext.define('EmpDetails',{
extend: 'Ext.data.Model',
fields: [
{name: 'employeeId', type: 'int'},
{name: 'employeeName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'birthday', type: 'date', dateFormat: 'd/m/Y'},
{name: 'sex', type: 'string', defaultValue: 'Male'}
],
proxy: {
type: 'rest',
url: 'data.json',
format: 'json',
reader: {
type: 'json',
root: 'empDetails'
}
}
});

Ext.onReady(function(){
EmpDetails.load(1,{
success: function(empDetails){
console.log("Age: " + empDetails.get('age'));
}
});});
But it is not working. The URL it is looking for is incorrect:

../../Proxy/data.json/1.json
1.json does not exists at all, i is the employeeID present in data.json.
Please help

vietits
21 Oct 2012, 6:28 PM
. The URL it is looking for is incorrect:

../../Proxy/data.json/1.json
1.json does not exists at all, i is the employeeID present in data.json.
Please help
The url is generated basing on your config. If you don't want url to contain "1" instead of "1.json", just remove format config for proxy.


Ext.define('EmpDetails',{
extend: 'Ext.data.Model',
fields: [
{name: 'employeeId', type: 'int'},
{name: 'employeeName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'birthday', type: 'date', dateFormat: 'd/m/Y'},
{name: 'sex', type: 'string', defaultValue: 'Male'}
],
proxy: {
type: 'rest',
url: 'data.json',
// format: 'json',
reader: {
type: 'json',
root: 'empDetails'
}
}
});

To fetch data basing on employeeID, you should write server script to do the job, not with static json file.

bunty
22 Oct 2012, 4:51 AM
Thanks for the help, I got it.

I am waiting for this for a long long time, something silly which I am missing: Can you please help me on this?

http://www.sencha.com/forum/showthread.php?245188-Redirecting-with-json-object/page2


(http://www.sencha.com/forum/showthread.php?245188-Redirecting-with-json-object/page2)

bunty
22 Oct 2012, 4:52 AM
Thanks for the information.

I am waiting for a long time for this answer:

http://www.sencha.com/forum/showthread.php?245188-Redirecting-with-json-object/page2

Can you please help me on this?