PDA

View Full Version : how to auto setValue on COMBO on the foreign key val ?



valititi
12 Jan 2011, 3:10 AM
So, I have (pays) countries table and then (city) cities table (pays.pays_iden=city.pays_iden)... I edit city record . There, normal, i have a combo... With Countries list (valuField need to be equal to city pays_iden value from database, which is the foreign key in city for pays). So, I have :


// the reader for my current city record
var evRd=new Ext.data.JsonReader({idProperty: 'city_iden',root: 'arrVals',
fields: [
{name: 'city_iden', type:'int'},
{name: 'paysIden', type:'int', mapping:'pays_iden'},
{name: 'city_nume', type: 'string'}
]
});
// then the store for the combo with the linked country...
var paysCboSt=new Ext.data.JsonStore({
url: 'dbgt/getPays.php?o=pays_nume&d=ASC&i='+pId+'',method:'POST',
idProperty: 'pays_iden',successProperty:'success',root: 'arrVals',
fields: [
{name:'pays_iden',type:'int'},
{name:'pays_nume',type:'string'}
]
}).load();

// then in my form, except the cityID and cityNAME i have :
{id:'paysIden',name:'paysIden',xtype:'hidden',value:''},
{fieldLabel:'Country', id:'pays_iden', name:'pays_iden',
xtype:'combo', store:paysCboSt, valueField:'pays_iden', displayField:'pays_nume',
autoSelect:true,forceSelection:true, triggerAction:'all',editable:false,
initComponent:function(){
var me=this;
this.store.on('load',function(store){
me.setValue(store.getAt(0).get('pays_iden'));
});
this.store.load();
}
}
I so have a hidden field in the form called paysIden. And I need that the form autoSelect this value on load. So what must I call instead :
me.setValue(store.getAt(0).get('pays_iden'));
I can also delete this field (the fake hidden paysIden, because the real one is pays_iden and is the combo !) but I don't know how to call this value from my json response (because i set it there) :



{
success:true,
arrVals:[
{"pays_iden":"1","pays_nume":"France"},
{"pays_iden":"2","pays_nume":"Germany"},
{"pays_iden":"4","pays_nume":"Italy"},
{"pays_iden":"-10","pays_nume":"---"}
],
selId:2
}
So,
1. how setValue in combo valueField the hidden fake paysIden value ? or
2. how setValue in combo the json selId value which come by the json of combo ?
thanks

vishalnnsingh
12 Jan 2011, 3:50 AM
Use the select event in the country combo, which resets the city field on country change and afte the country is selected pass the id to the server and reload your city store.

valititi
12 Jan 2011, 4:06 AM
thanks for answer. but the country WILL not be re-selected ! i needed, on LOAD, auto...
i have two possibilities. please read my thread. I am sure the data that I already have in it must be enough.
1. i have a fake paysIden, a hidden input which contains the city record pays_iden correct value (ex: 2).
2. i have also the json for countries (pays) list to put in combo and here i have a selId field (which contains same value as the hidden fake paysIden), ex:2
see the and of my first post. I got the idea, but I cannot guess all the details for it. Use select event... Means, I think to put a listener on combo... But this will not resets city... alone, I think... "after the contry is load"... which of them ? the combo ? the id of the country id the hidden fake id field ? or in the returned json ? "pass the id to the server and reload city store" I hope that is "pass the id as param to the store, and then reload it from server ?" and why from there... this is over my newbie capacities. thanks anyway for advice.
but, thanks to help me, please :-)

valititi
12 Jan 2011, 5:33 AM
I learn, learn, learn... And I cannot change my opinion (3 months now) about ExtJS from "amazing" !
Not nice to be rookie :-)... But here are all the excitement ! When know all... Become bored !
Like they said on a Discovery advertising : "If we have all the answers, there will be nothing left to discover. So, ignorance is bliss"
http://www.youtube.com/watch?v=e1-MNP7ptgk
And I think even ignorance is not only bliss, but is also bless ! :-)
--------
So for my thread problem... I am sure that there are something in ExtJS stronger than the function below... But not find (within 1- hours...) :-( And because I really must go quickly ahead, I think I re-invented somehow the wheel... :-)
So, I call this after window loads... And if this can help others with similar problems, feel free to use it. I also use a similar function for showing a mask until a form is populated...



function loadComboDelayed(form,fieldFrom,comboTo){
if( !document.getElementById(fieldFrom) ||
document.getElementById(fieldFrom).value=='' ||
document.getElementById(fieldFrom).value==0){
setTimeout('loadComboDelayed('+form+','+fieldFrom+','+comboTo+')',100);
}
else{
Ext.getCmp(comboTo).setValue(document.getElementById(fieldFrom).value);
return
}
}so, still awaiting for a pure extjs elegant solution... :-)

devtig
12 Jan 2011, 1:08 PM
Wow, you managed to fit in one small piece of code:
1. misuse the asynchronous way of callbacks with a delay
2. unnecessary use of Ext.getCmp
3. unnecessary use of document.getElementById

Read here:
http://dev.sencha.com/deploy/dev/docs/?class=Ext.Ajax
http://dev.sencha.com/deploy/dev/docs/?class=Ext.form.FormPanel

The asynchronous way:

Ext.Ajax.request({
url: 'ajax_demo/sample.json',
success: function(response, opts) {
//do something with the server response that
//arrives some time later.
var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response.status);
}
});

But if you're just starting with Ext JS you better start reading here:
http://www.sencha.com/learn/Learn_About_the_Ext_JavaScript_Library

valititi
13 Jan 2011, 6:02 AM
I threat all my Ajax request Exactly in your way. But here is form.load()... Whatever, really thanks for your asnwer.
c pm.. :-)