PDA

View Full Version : Concatenate Fields from a Record to use it in JsonReader/ComboBox



paese
21 Sep 2007, 5:49 AM
Hi,

My goal is to have a Combobox that displays a full name that consists of the values 'name' and 'forename' stored in a database. What I'm trying to find is a solution on the client side. Is see two ways to do it but I don't know how.

First. Generate a new field in the record definition called 'fullname' that consists of 'name' and 'forename'. How?



var PersonObject = Ext.data.Record.create([
{name: 'id', type: 'int'},
{name: 'forename', type: 'string'},
{name: 'name', type: 'string'}
]);


And second. Is there a way to render the displayField that it consist of name+' '+forename?



personCombo = new Ext.form.ComboBox({
valueField: 'id',
displayField:'name',
store: ADB.PersonDS.get(),
mode: 'local',
triggerAction: 'all',
emptyText:'Person auswaehlen...',
width:150,
loadingText: 'Lade Personen...'
});


Thanks for your inputs.
Paese

Animal
21 Sep 2007, 6:05 AM
For now, you can use



var PersonObject = Ext.data.Record.create([
{name: 'id'},
{name: 'name', mapping: 'name+" "+obj.forename'}
]);


This will work until Jack agrees to use my new getJsonAccessor function in JsonReader which uses



with(theRecordObject) return <the mapping expression>


You could add this override if you want:



Ext.override(Ext.data.JsonReader, {
getJsonAccessor: function(expr) {
try {
return (/[\[\.]/.test(expr))
? new Function("rec", "with(rec)return " + expr)
: this.simpleAccess.createDelegate(this, [expr], true);
} catch(e){}
return Ext.emptyFn;
}
});


Then you can use



var PersonObject = Ext.data.Record.create([
{name: 'id'},
{name: 'name', mapping: 'name+" "+forename'}
]);

paese
21 Sep 2007, 6:43 AM
Oh yeah, great.

Although I don't fully understand it that will do the trick for me. I changed it slightly so I don't have to change the origin value from the database.


var PersonObject = Ext.data.Record.create([
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'forename', type: 'string'},
{name: 'fullname', mapping: 'name+" "+obj.forename'}
]);

thx
Paese