在ExtJS模板中对数组进行迭代

Iterating over an array in an ExtJS Template

本文关键字:数组 迭代 ExtJS      更新时间:2023-09-26

我正在使用ExtJS将AJAx调用中的一组数据应用于Ext.Template。其中一个元素是字符串数组。

var data = {
  name: "I am Here!",
  messages: ['aaa', 'bbb', 'ccc', 'ddd']
};

我已经定义了这样的模板

var tpl = new Ext.Template(
  '<h2>{name}</h2>',
  '<tpl for="{messages}">',
    '<p>{.}</p>',
  '</tpl>'
);

当我将此模板应用于数据时,它会从字面上显示{.},而不是打印每个元素。我在ExtJS中使用数组的大多数搜索都使用这种格式。这是JFiddlehttp://jsfiddle.net/u4zkM/2/

我尝试使用数组的方式有什么问题?

对于数组和其他高级功能,您需要使用Ext.XTemplate。同时删除消息周围的括号。

var tpl = new Ext.XTemplate(
    '<h2>{name}</h2>',
    '<tpl for="messages">',
        '<p>{.}</p>',
    '</tpl>'
);

jsFiddle