2009-10-22 53 views
8

我有一个ExtJS xtemplate因为返回json对象的方式而引发错误。Ext JS xtemplate问题 - 检查字段是否存在

有时json对象有一个字段customer定义,但有时它完全从对象中丢失。

很明显,我得到一个错误客户没有定义当我的模板应用到JSON的字段丢失。

所以我的问题是:
有没有办法,我可以检查一个未定义的字段在xtemplate?

像:

<tpl if="customer!=undefined">{customer}</tpl> 

很明显,我已经试过这一点,但它不工作。

感谢您的任何帮助。

回答

4

你试过吗?

<tpl if="customer == undefined"> 
    <b>{customer}</b> 
</tpl> 
+0

这工作。谢谢:) – 29er 2009-10-22 22:59:10

13

除非我记错了,你就应该能够做到像下面这样:

<tpl if="customer"> 
    <b>{customer}</b> 
</tpl> 

或者,如果我的三元例子中,你应该能够嵌入(尽管是有限,不知道将按预期工作)Javascript到XTemplates像这样:

{[values.customer]} 
{[values.customer ? customer : 'Empty']} 
+0

这个工作太。谢谢!! – 29er 2009-10-22 22:58:31

+2

对我而言,只适用于:{[values.customer? values.customer:'Empty']}否则我收到错误:[INFO] [Ext.XTemplate#apply]错误:客户未定义 – 2013-05-13 21:46:37

+0

这也可以工作:'{[values.customer || '空']}' – 2014-12-09 21:15:57