javascript - 循环遍历数组并在 XTempate 内执行语句

标签 javascript extjs

我有一个自定义组件,有这样一个商店:

"store":{
    "fields":["name"],
    "data":[{
        "id":"1","name":"John","checked":"truec","tools":["user","doc"]
     },{
        "id":"2","name":"Jack","checked":"false","tools":["user"]
     }]
}

我有一个模板,我在自定义 Ext.ux.CheckList 中使用它。它工作得很好,除了一件小事 - 我不知道如何循环 tools 数组。这就是我现在拥有的:

'<tpl for="tools">',
 // if (curValue == "user") '<div>Value 1</div>'
 // else if (curValue == "doc") '<div>Value 2</div>'           
'</tpl>'

我见过thisthis示例,但它们并不能解决我的问题。 如您所见,我想在循环内使用 tools 值:

if (curValue == "user") '<div>Value 1</div>'
else if (curValue == "doc") '<div>Value 2</div>'

但我不知道如何实现这一点。

最佳答案

迭代平面数组时当前索引处的值可以通过标记中的“{.}”和条件中的“values”属性来访问。

Sencha Fiddle: An Example of Iterating Through a Flat Array

var template = new Ext.XTemplate(
    '<tpl for=".">',
        '<p>{name}</p>',
        '<tpl for="tags">',
            '<tpl if="values === \'user\'">',
                '<p>{.}</p>',
            '<tpl elseif="values === \'doc\'">',
                '<pre>{.}</pre>',
            '</tpl>',
        '</tpl>',
        '<br>',
    '</tpl>'
);

https://www.sencha.com/forum/showthread.php?320083 http://docs.sencha.com/extjs/6.2.0/classic/Ext.XTemplate.html

关于javascript - 循环遍历数组并在 XTempate 内执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574085/

相关文章:

javascript - 使用异步迭代来链接 promise

javascript - jQuery Accordion 事件 : not working in Firefox using ID tag

javascript - 使用:contain in list

javascript - Extjs tagfield 在输入框中保存值

java - java服务器和javascript之间的通信

javascript - 为什么下拉菜单不出现在 Safari 中?

html - sencha 或 Extjs 怎么样?

javascript - 如何指定 ExtJS 网格中单元格的格式?

javascript - Extjs 网格宽度计算错误

javascript - 如何从单元格编辑网格中获取 "Column Name"和 "Previous Value"