tree - 如何使用具有 'auto' 映射的 TreeStore?

标签 tree sencha-touch

我有一个看起来像这样的模型:

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,因此无法呈现每个列表项的信息。

有没有人有关于如何实现这一点的建议?提前致谢。

最佳答案

您应该将条件逻辑从函数移动到模板中。这是一个示例,演示了您将如何执行此操作(尽管您可能需要对其进行修改才能使其正常工作):

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展示它是如何 build 的。您可能还想查看我关于 Xtemplates 主题的两部分截屏视频系列( part onepart two)

关于tree - 如何使用具有 'auto' 映射的 TreeStore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864238/

相关文章:

php - 在 PHP 中反转二叉树

apache-flex - Flex 获取选定树项目的 XML 父项

sencha-touch - 如何使用sencha touch与数据库建立远程连接

extjs - sencha mac 工作目录无法识别

javascript - Sencha Touch AJAX 调用 MVC

python - 如何在Python中绘制这个树形图案?

c# - 如何删除 roslyn 语法树中的标记。例如从属性中删除虚拟关键字标记?

algorithm - 如何将树形成通往每片叶子的单独路径

javascript - 超时后重定向到其他 View

javascript - 使用 JSBuilder 或 Apache ANT 构建 Sencha Touch 应用程序