PDA

View Full Version : Virtual field in model (or multiple fields in Ext.form.Select)



doncoleman
22 Dec 2010, 3:57 PM
I'd like to display a person's name in a Select control. The model has first and last name as 2 fields.

I think I need to create a virtual field "name" on the model, or use a template in the select. I can't get either to work.

Is there a standard way to do this?



Ext.regModel('Employee', {
fields: [
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'}
// ...
],
name: function() {
// returns the correct value, but doesn't work for Ext.form.Select#displayField
return this.first_name + " " + this.last_name;
}
});




{
xtype: 'selectfield',
id: 'employeeSelect',
name: 'name',
label: 'Name',
store: employeeStore,
valueField: 'code',
// tpl: '{first_name} {last_name}', // Doesn't work
// tpl: displayField: 'name', // Doesn't work
displayField: 'first_name'
}

RWaters
22 Dec 2010, 11:39 PM
Ext.regModel('Employee', {
fields: [
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'},
{name: 'full_name', type: 'string', convert: function(v, rec) { return rec.first_name + " " + rec.last_name }}
// ...
]
});

doncoleman
23 Dec 2010, 7:04 AM
Thanks Rich. This worked with one small change, I needed to access attributes via the data array.



Ext.regModel('Employee', {
fields: [
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'},
{name: 'full_name', type: 'string',
convert: function(v, rec) {
return rec.data.first_name + " " + rec.data.last_name;
}
// ...
}
]
});

RWaters
23 Dec 2010, 7:07 AM
Whoops, yes that is the correct way to access them :P