PDA

View Full Version : Array data is lost outside of store.load function



guilan
11 Jun 2013, 1:49 AM
Hi:
I am try to write getValue function for combobox so that the combobox's value is assigned one instead of default.
The store is load successfully and data is found in store.each loop. I tried to assign this found value to a global variable, it didn't work. I tried to push all the data to an global array. But once outside the scope of store.load, the array is empty. So what can I do to achieve this. Any help would be appreciated. Below is my code.

getValue: function (store, num) {
this.banner = 'N/A';
var bannerData = [];
store.load({
scope: this,
callback: function(records, operation, success) {
store.each(function(record, idx) {
bannerData.push(record.data);
var event_bannerID = record.get('banner_id');
event_banner = record.get('banner');
if(event_bannerID == num)
{
banner = event_banner;
return;
}
});
}
});
console.log(bannerData.length);
return this.banner;
},

guilan
11 Jun 2013, 4:18 AM
After a couple of hours intensive searches and numerous trials. I have found the solution. I put the store inline of the combobox. It is not the best solution anyway. I have 5 comboboxes , I have to repeat this chunk of code 5 times. If you have the better solution, please share it with me. Many thanks. Here is my code:

{
xtype: "combo",
id : 'banner4',
editable: false,
triggerAction: 'all',
width: 700,
listWidth: 700,
displayField: 'banner',
valueField: 'event_id',
name: 'banner4',
typeAhead:true,
mode: 'local',
forceSelection: true,
store:new Ext.data.JsonStore( {
url: 'event_url',
fields: ['event_id','banner_id', 'banner'],
root: 'data',
autoLoad: true,
listeners: {
load : function() {
var event_bannerID;
var event_banner;
store.each(function(record, idx) {
event_bannerID = record.get('banner_id');
event_banner = record.get('banner');
if(event_bannerID == 4)
{
Ext.getCmp('banner4').setValue(event_banner);
return;
}

});
if (!Ext.getCmp('banner4').getValue())
{
Ext.getCmp('banner4').setValue('No Banner');
}
}
}

}),

//value: this.getValue(store, '4'),
selectOnFocus:true,

},

willigogs
11 Jun 2013, 8:34 AM
Could you clarify exactly what it is you're wanting to achieve?

And could you also use the CODE tags, as it makes your provided examples impossible to read.

guilan
12 Jun 2013, 12:29 AM
I am very inexperience in this forum, just joined it a few days ago. I haven't figured out how to use the <code> tag. Could you please send me some instructions or a help link? Thank you!

What I want to achieve is as following:

We have an event module. Two tables are involved in this problem, event and banner. The key is event_id which is the the valuefield in combobox, the displayfield is event title. There are many events but only 5 event banners which will be in carousel. If an event has been assigned to one of the banners, the banner of the event will be used. Therefore the Jsonstore is loaded from fields in this two tables. I need to check the items in the combobox. If any item is associated with banner 1-5, I need to select the event title accordingly. For example, if event 'Annual Conference' is assigned to banner 4, in 4th combobox will display 'Annual conference' otherwise the combobox will display 'No banner'.

Think it over I haven't really solved this problem. I have set the value but it is not the same as making an item selected. I need to select the item so that when the save button is clicked, the same values are saved to the database if user hasn't changed anything.

I hope I have made myself clearly understood. Thank you for your interesting in my question.

willigogs
12 Jun 2013, 2:04 AM
I think I understand what you're wanting - basically to set the value of multiple combos based on the data held within a store.

Here's a quick example I knocked up over on JSFiddle which might be close to what you're wanting:
http://jsfiddle.net/Whinters/ge3tW/

guilan
12 Jun 2013, 4:31 AM
Hi Willigogs:

Thank you very much for your help. Finally I have this problem solved. Your code is very clear and helpful. Your help is much appreciated.