我的模型看起来像这样:
Ext.regModel('TreeItem', {
fields: [
{ name: 'ItemId', type: 'int' },
{ name: 'ItemType', type: 'string' },
{ name: 'Article', type: 'auto' },
{ name: 'Container', type: 'auto' },
{ name: 'Category', type: 'auto'}]
});
ItemType指示该特定项目是否应呈现为文章,容器或类别。 Article,Container和Category对象中的每一个都有一个关联的ArticleName,ContainerName和CategoryName。
我想基于记录的ItemType在NestedList中呈现这些名称。所以我像这样覆盖了getItemTextTpl:
getItemTextTpl: function (recordnode)
{
var template = '<div class="{ItemType}-icon"></div>';
if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Article')
{
template += recordnode.firstChild.attributes.record.data['Article']["ArticleName"];
}
if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Container')
{
template += recordnode.firstChild.attributes.record.data['Container']["ContainerName"];
}
if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Category')
{
template += recordnode.firstChild.attributes.record.data['Category']["CategoryName"];
}
return template;
}
但是,似乎getItemTextTpl仅针对树的每个级别调用一次,因此无法为每个列表项呈现信息。
有没有人有关于如何实现这一目标的建议?提前谢谢。
答案 0 :(得分:3)
您应该将条件逻辑从函数移动到模板中。这是一个演示如何执行此操作的示例(尽管您可能需要对其进行修改才能使其正常工作):
getItemTextTpl: function (recordnode)
{
var template = '<div class="{ItemType}-icon"></div>' +
'<tpl if="ItemType === \'Article\'">' +
'{ArticleName}' +
'</tpl>' +
'<tpl if="ItemType === \'Container\'">' +
'{ContainerName}' +
'</tpl>' +
'<tpl if="ItemType === \'Category\'">' +
'{CategoryName}' +
'</tpl>' +
'</div>';
return template;
}
我创建了一个使用此技术的NestedList demo。 code is on github,还有a screencast证明了它是如何构建的。您可能还想查看我关于Xtemplates(part one和part two)主题的两部分截屏视频
答案 1 :(得分:0)
这不可能。我将我的对象泛化并使用常规映射。您不能将自动映射与模板一起使用。