PDA

View Full Version : How to reload a DataStore with new data.. ? :-0



hananyariv
16 Feb 2011, 5:24 AM
Hello All,
I am trying to insert data into my grid, the data comes from my DataBase, and i use $jsonresult = JEncode($arr); to pass the data back..
The data is passing nicely back but i dont know how to use it...

i have tried using Ext.Ajax.request({
waitMsg: 'Please wait...',
url: 'database.php',
params: {
task: 'saleItems',
NumOItems: amountItems.getValue(),
p_Item : ItemIs.getValue()

},
to send my command to the server , and it work but i dont know how to use the data the server is returning and adding it to my grid..

Can anyone please help .. ? :) tnx.

steffenk
16 Feb 2011, 5:27 AM
store.load();

mitchellsimoens
16 Feb 2011, 5:27 AM
What does the JSON look like?

Also, please wrap your code in the code tags by highlighting and clicking on the number sign '#' in the toolbar of the HTML Editor

hananyariv
16 Feb 2011, 5:37 AM
Hi, here is the Data coming back from the server side :

({"total":"2","results":[{"0":"38","AI":"38","1":"P_4d5a780bac7f3","IMEI":"P_4d5a780bac7f3","2":"Nokia E50","Item":"Nokia E50","3":"2011-02-08","DateCame":"2011-02-08","4":"0000-00-00","DateSold":"0000-00-00","5":"N","Sold":"N"}]})({"total":"2","results":[{"0":"38","AI":"38","1":"P_4d5a780bac7f3","IMEI":"P_4d5a780bac7f3","2":"Nokia E50","Item":"Nokia E50","3":"2011-02-08","DateCame":"2011-02-08","4":"0000-00-00","DateSold":"0000-00-00","5":"N","Sold":"N"},false]})

i can see it in the client side with response.textrespons , but i dont know how to load it into the store..
Tnx

fay
16 Feb 2011, 5:43 AM
Why not just use a JsonStore (http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.JsonStore)? You can specify your url in the initial config and pass (updated?) params in the load method.

mitchellsimoens
16 Feb 2011, 5:44 AM
Your JSON is not correct.

You provided this which at first glance looked similar but is not:


({
"total": "2",
"results": [
{
"0": "38",
"AI": "38",
"1": "P_4d5a780bac7f3",
"IMEI": "P_4d5a780bac7f3",
"2": "Nokia E50",
"Item": "Nokia E50",
"3": "2011-02-08",
"DateCame": "2011-02-08",
"4": "0000-00-00",
"DateSold": "0000-00-00",
"5": "N",
"Sold": "N"
}
]
})({
"total": "2",
"results": [
{
"0": "38",
"AI": "38",
"1": "P_4d5a780bac7f3",
"IMEI": "P_4d5a780bac7f3",
"2": "Nokia E50",
"Item": "Nokia E50",
"3": "2011-02-08",
"DateCame": "2011-02-08",
"4": "0000-00-00",
"DateSold": "0000-00-00",
"5": "N",
"Sold": "N"
},
false
]
})

btw, I copied and pasted your JSON into jsonlint.com and it told me right away that it was not valid JSON but still formatted it to be readable.

This is valid:


[
{
"total": "2",
"results": [
{
"0": "38",
"AI": "38",
"1": "P_4d5a780bac7f3",
"IMEI": "P_4d5a780bac7f3",
"2": "Nokia E50",
"Item": "Nokia E50",
"3": "2011-02-08",
"DateCame": "2011-02-08",
"4": "0000-00-00",
"DateSold": "0000-00-00",
"5": "N",
"Sold": "N"
},
{
"0": "38",
"AI": "38",
"1": "P_4d5a780bac7f3",
"IMEI": "P_4d5a780bac7f3",
"2": "Nokia E50",
"Item": "Nokia E50",
"3": "2011-02-08",
"DateCame": "2011-02-08",
"4": "0000-00-00",
"DateSold": "0000-00-00",
"5": "N",
"Sold": "N"
}
]
}
]

I just copied the first result twice as I the JSON said you had 2 results but couldn't find the second.

@fay, was going to be getting around to that but first trying to see what's being returned.

hananyariv
16 Feb 2011, 7:03 AM
Thank you for your reply, I looked into JsonStore but the problem is that i need to send paramters to the PHP..

i will try to reprase my question ..

i a grid that is empty, the user fills out 2 textfiles that are parameters for the quary, after he submits the php make a quary based on this parameter and need to return an array of items , this array is what i am hoping to fill my empty Grid with...

as i said, i am able to get the data that is the array back to the client via Ext.Ajax.request , the onlu problem is that i am unsure how to handle the data and insert it into the Grid.. :0

mitchellsimoens
16 Feb 2011, 7:05 AM
When you load a store you can specify a params object:


store.load({
params: {
param1: "Hi",
param2: "Hello"
}
});

hananyariv
16 Feb 2011, 7:26 AM
Just cant seem to pull it off .. :(
Here is my Store Code :


SaleDataStore = new Ext.data.Store({
id: 'SaleDataStore',
proxy: new Ext.data.HttpProxy({
waitMsg: 'Please Wait',
url: 'database.php',
method: 'POST'
}),
// baseParams:{task: "LISTING"}, // this parameter is passed for any HTTP request
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
id: 'id'
},
[ {name: 'AI', type: 'int', mapping: 'AI'},
{name: 'IMEI', type: 'string', mapping: 'IMEI'},
{name: 'p_Item', type: 'string', mapping: 'Item'},
{name: 'DateCame', type: 'date', mapping: 'DateCame'},
{name: 'DateSold', type: 'date', mapping: 'DateSold'},
{name: 'Sold', type: 'string', mapping: 'Sold'},

]), sortInfo:{field: 'AI', direction: "DESC"}
});

And this is the function that sends the params :

function addToSale(){




//load the stores:
//making the call

SaleDataStore.reload({
params: {
task: 'saleItems',
NumOItems: amountItems.getValue(),
p_Item : ItemIs.getValue()

}


})
SaleListingGrid.doLayout();
}

Where SaleListingGrid is the Grid, - i am unsure if i need to call doLayout , its just a try because i cant see anything .. :)

Can anyone spot the problem here? :-z

mitchellsimoens
16 Feb 2011, 7:29 AM
Did you fix or make sure your JSON is correct also?

I haven't looked at your code because it isn't wrapped in the code tags. Highlight and click the number sign '#' in the toolbar.

hananyariv
16 Feb 2011, 7:37 AM
Here is the code as requested, i havent checked my Json but i have the same code on another gird and its working fine there .. i am using firebug but this is impossible to debug .. anyway ...


Just cant seem to pull it off ..
Here is my Store Code :



SaleDataStore = new Ext.data.Store({
id: 'SaleDataStore',
proxy: new Ext.data.HttpProxy({
waitMsg: 'Please Wait',
url: 'database.php',
method: 'POST'
}),
// baseParams:{task: "LISTING"}, // this parameter is passed for any HTTP request
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
id: 'id'
},
[ {name: 'AI', type: 'int', mapping: 'AI'},
{name: 'IMEI', type: 'string', mapping: 'IMEI'},
{name: 'p_Item', type: 'string', mapping: 'Item'},
{name: 'DateCame', type: 'date', mapping: 'DateCame'},
{name: 'DateSold', type: 'date', mapping: 'DateSold'},
{name: 'Sold', type: 'string', mapping: 'Sold'},

]), sortInfo:{field: 'AI', direction: "DESC"}
});

And this is the function that sends the params :


function addToSale(){




//load the stores:
//making the call

SaleDataStore.reload({
params: {
task: 'saleItems',
NumOItems: amountItems.getValue(),
p_Item : ItemIs.getValue()

}


})
SaleListingGrid.doLayout();
}

Where SaleListingGrid is the Grid, - i am unsure if i need to call doLayout , its just a try because i cant see anything ..

[/CODE][/CODE]

mitchellsimoens
16 Feb 2011, 7:47 AM
This is just me writing code so I'm not sure if it will work in your instance:


var store = new Ext.data.JsonStore({
url : 'database.php',
storeId : 'SaleDataStore',
root : 'results',
idProperty : 'AI',
fields : [
{ name : 'AI', type : 'int' },
{ name : 'IMEI', type : 'string' },
{ name : 'p_Item', type : 'string', mapping: 'Item' },
{ name : 'DateCame', type : 'date' },
{ name : 'DateSold', type : 'date' },
{ name : 'Sold', type : 'string' }
],
sortInfo : {
field : 'AI',
direction : 'DESC'
},
baseParams : {
task : 'LISTING'
},
addToSale : function(NumOItems, p_Item) {
this.load({
params: {
task : 'saleItems',
NumOItems : NumOItems,
p_Item : p_Item
}
});
}
});

store.addToSale(amountItems.getValue(), ItemIs.getValue());

hananyariv
16 Feb 2011, 8:04 AM
SOLVED ! :))

Thank you very much mitchellsimoens for you time,

unbelievably the problem was in my php code where i resent the json code - it was your spotting due ! :)
thank you :)

hananyariv
16 Feb 2011, 8:15 AM
Just 1 more question about this subject,
is there a way to append to the datastore ? - i see theres a function called loadData that uses append but i need to also use the params..

hananyariv
16 Feb 2011, 8:19 AM
Found it in the api ;)
add : ture


THANK YOU ALL :)