PDA

View Full Version : Combo DisplayTpl



Mteixeira
18 Oct 2012, 1:52 AM
In my combobox i have something like this:



displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'{Nome} ({Valor})',
'</tpl>')


It works fine except that if there isn't a pre-selected value for the combo, it shows this "()"


So i've tried to create a template that when the value is empty then it show nothing like this:



displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<tpl if="this.isEmpty({Nome})">',
'',
'<tpl else>',
'{Nome} ({Valor})',
'</tpl>',
'</tpl>',
{
isEmpty: function (value) {
return value == '';
}
})


But i keep getting a error message "Expected :" when the tpl is evaluated (extjs-all-debug)



compile: function (tpl) {
var me = this,
code = me.generate(tpl);


return me.useEval ? me.evalTpl(code) : (new Function('Ext', code))(Ext);


Any ideas on how to do this?

vietits
18 Oct 2012, 5:04 AM
Try to fix your code as below:


Ext.create('Ext.XTemplate',
'<tpl for=".">',
// '<tpl if="this.isEmpty({Nome})">',
'<tpl if="this.isEmpty(Nome)">',
'',

'<tpl else>',
'{Nome} ({Valor})',
'</tpl>',
'</tpl>',
{
isEmpty: function (value) {
return value == '';
}
}
)

Mteixeira
18 Oct 2012, 5:13 AM
Doesn't show the error anymore, but also doesn't do anything..

I've put an breakpoint but the function isEmpty isn't called

vietits
18 Oct 2012, 5:33 AM
This code works fine with Ext 4.1.1 and Chrome:


Ext.onReady(function(){
var tpl = Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<tpl if="this.isEmpty(Nome)">',
'',
'<tpl else>',
'{Nome} ({Valor})',
'</tpl>',
'</tpl>',
{
isEmpty: function (value) {
console.log('isEmpty is called with value:', value)
return value == '';
}
}
);
tpl.apply([{
Nome: '1', Valor: '1'
},{
Nome: '', Valor: '2'
}]);
});

Mteixeira
18 Oct 2012, 5:52 AM
I've exactly that but the function isEmpty just isn't called....

I've tried this

tpl if="Nome == \'\'" but it produces the same result

Mteixeira
18 Oct 2012, 6:02 AM
I've solve it :)



displayTpl: Ext.create('Ext.XTemplate',
'<tpl for=".">',
'<tpl if="Nome != \'\'">',
'{Nome} ({Valor})',
'<tpl else>',
'',
'</tpl>',
'</tpl>'
)


Since i can't seem to understand what value is being passed to the combo i figure if it's different from empty then return the structure i want, if not then return ''