我的JSON大多数时候在它的结构中有这两个:"type"one_answers"comments"。有时它会用"type", "survey", "comments"代替。因此,我想使用"if"来让ext.xtemplate显示它找到的那些。例如,我试过这个,但不工作:
new Ext.XTemplate(
'<div style="text-align:justify;text-justify:inner-word">',
'<b>Type:</b> {type}<br/>',
'<tpl if="survey">',
<b>Survey:</b> {survey}<br/>',
'</tpl>',
'<b>Comments:</b> {comments}',
'</div>'
我也试过这些,但没有成功:
<tpl if="survey != {}">
<tpl if="survey != undefined">
探测不存在的物体的正确方法是什么?,谢谢。
p。我使用ExtJS 3.4
使用values
局部变量,例如:
var tpl = new Ext.XTemplate(
'<div style="text-align:justify;text-justify:inner-word">',
'<b>Type:</b> {type}<br/>',
'<tpl if="values.survey">',
'<b>Survey:</b> {values.survey}<br/>',
'</tpl>',
'<b>Comments:</b> {values.comments}',
'</div>'
);
除了values
,还有其他可用的变量,它们在某些情况下是有用的:parent
, xindex
, xcount
。
模板预处理后作为函数执行,你的模板是这样的:
function (values, parent, xindex, xcount){ // here are values, parent, etc
with(values){ // each property of values will be visible as local variable
return [
'<div style="text-align:justify;text-justify:inner-word"><b>Type:</b> ',
(values['type'] === undefined ? '' : values['type']),
'<br/>',
this.applySubTemplate(0, values, parent, xindex, xcount), // each <tpl> is converted into subtemplate
'<b>Comments:</b> ',
(values.comments === undefined ? '' : values.comments),
''
].join('');
}
}
这些知识通常有助于理解XTemplates。
上述变量的示例用法:http://jsfiddle.net/gSHhA/
我用<tpl if="!!survey>"