Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS Model - Concatenate fields

Tags:

extjs

extjs4

Is there a sensible way in a model to concatenate two fields, something like this:

Ext.define('model.Person', {
    extend: 'Ext.data.Model',
    idProperty: 'Id',
    fields: [
        { name: 'Id', type: 'int' },
        { name: 'FirstName', type: 'string' },
        { name: 'LastName', type: 'string' },
        { name: 'FullName', type: 'string', mapping: 'FirstName + " " + LastName' }
    ]
});

I've tried a multitude of ways, but can't seem to get any to work.

I was going to use a function in the model to stick the two fields together, but I also need to use this as the displayfield inside a 'itemselector' (custom control) and switch this dynamically and that control doesn't seem to like 'FullName()' as a displayfield.

Any thoughts greatly appreciated.

like image 299
dougajmcdonald Avatar asked Sep 26 '13 14:09

dougajmcdonald


1 Answers

Use the convert config of the Ext.data.Field: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Field-cfg-convert

{ name: 'FirstName', type: 'string' },
{ name: 'LastName', type: 'string' },
{
    name: 'FullName',
    type: 'string',
    convert: function( v, record ) {
       return record.get( 'FirstName' ) + ' ' + record.get( 'LastName' )
    }
}

Here's a live example: https://fiddle.sencha.com/#fiddle/mf

like image 57
existdissolve Avatar answered Sep 28 '22 00:09

existdissolve