PDA

View Full Version : Preserving multiselections after a store reload



stimpy
4 Oct 2012, 7:43 AM
I am trying to preserve multi selections after a store load. I can set and see the values without a problem but if I reload the store then set the values , the values are set but do not appear as selected in the combobox.

what am I doing wrong ?




xtype:'multiselector',
name:'myItemSelector',
itemId:'myItemSelectorId',
allowBlank:true,
store:'mySelOpts' ,
displayField:'selName',
valueField:'selId',
listeners:{
beforerender:function(){
this.store.load();
}
}
//model and store

Ext.regModel('mySelModel',{ fields:[
{type:'string',name:'selId'},
{type:'string',name:'selName'}
] });

var mySelOpts=Ext.create('Ext.data.Store',{
model:'mySelModel',
autoload:true,
proxy:{
type:'ajax',
url:'myproject/mySelectSource',
reader:{
type:'json',
root:'rows'
} } });


I am testing using a function which I can call from firebug


function myTestfunction(){ //if i load the store first, the values get set but are not displayed as selected
//mySelOpts.load();
//if i don't load the store this line sets the values AND they appear as selected Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']);
console.log( Ext.ComponentQuery.query('#myItemSelectorId')[0].getValue());
}

metalinspired
4 Oct 2012, 8:32 AM
try using this (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.selection.Model-cfg-pruneRemoved).
also you could get what is selected from selection model and store it to a variable before loading store and when loading is done instruct selection model to select items based on your saved values

stimpy
4 Oct 2012, 10:07 AM
@metal

thanks for your answer but i don't think prune will solve this .

The store must be reload by customer requirement ( lots of ADDs and REMOVES ).



done instruct selection model to select items based on your saved values


The problem is that after a store load the VALUE is set but the selections in the UI are not .

metalinspired
4 Oct 2012, 10:45 AM
From what you say the only way to do it, unless I'm mistaking, is to get selected items, store them in some manner, reload the store and then reselect items which match those saved before.
Don't really have any code to offer since I never needed this functionality for combobox (only now I noticed you're talking about sombobox which doesn't have selection model), but take a look at this (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.ComboBox-method-select) and it's comments.

stimpy
4 Oct 2012, 1:51 PM
@metalinspired

Thanks for the pointer.

I will try that ,however I don't understand why if I do this



Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']);


it will set the value and the selection but if I do this



mySelOpts.load();
Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']);


it only sets the value . Is this a bug or expected ?If its expected why ? What is not happening ?

metalinspired
4 Oct 2012, 11:04 PM
Not sure but it seems that loading of mySelOpts.load(); isn't completed before you call Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']);.
Try this


mySelOpts.on(
'load',
function() {
Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']);
}
);
mySelOpts.load();

stimpy
6 Oct 2012, 12:56 PM
@metalinspired

You pointed me in the right direction.

Here is what worked




store.load({
callback:function(records, operation, success){
if(success==true){
Ext.ComponentQuery.query('#myItemSelectorId')[0].setValue(['1','2']); }
}});


I am still confused why if the store was not loaded setValue worked at all.

However this solved the problem.

Thanks