ExtJS:动态地在分组的组合框tpl中传递组名

时间:2019-05-30 13:49:01

标签: extjs extjs6

我正在使用一个组合组合框,需要从其配置动态传递组名。

  var data = [{
           group: 'Fubar',
           key: '1',
           name: '2015 Product Development'
       }, {
           group: 'Fubar',
           key: '2',
           name: 'Message Filter'
       }, {
           group: 'Fubar',
           key: '3',
           name: '2014 Product Development (Little)'
       }, {
           group: 'Other',
           key: '4',
           name: 'Global Structure'
       }, {
           group: 'Other',
           key: '5',
           name: 'My SW'
       }];
    Ext.apply(combo, {
            listConfig: {
        tpl = new Ext.create('Ext.XTemplate',
               '<tpl for=".">',
               '<tpl for="group" if="this.shouldShowHeader(group)"><div class="group-header">{[this.showHeader(values.group)]}</div></tpl>',
               '<div class="x-boundlist-item"><input type="checkbox" />{name}</div>',
               '</tpl>', {
                   shouldShowHeader: function(group) {
                       return this.currentGroup !== group;
                   },
                   showHeader: function(group) {
                       this.currentGroup = group;
                       return group;
                   }
               });
}
});
var combo = Ext.create('Ext.data.Store', {
       fields: ['group', 'key', 'name'],
       data: data
   });
 items: [{
           xtype: 'combobox',
           id: 'searchInput',
           store: combo,
           multiSelect: true,
           labelWidth: 50,
           queryMode: 'local',
           displayField: 'name',
           fieldLabel: 'Choose',
           listConfig: {
               cls: 'grouped-list'
           },
           tpl: tpl,
groupName: 'group'
}]

我尝试使用代码,但是没有用。它赋予group属性本身而不是其值。

<tpl for="combo.groupName" if="this.shouldShowHeader(combo.groupName)">
  • 这里的组合是正在使用的组合框实例。

1 个答案:

答案 0 :(得分:0)

应该以这种方式使用Tpl以获得所需的输出。

  '<tpl for=".">',
    '<tpl for="' + combo.groupName + '" if="this.shouldShowHeader(' + combo.groupName + ')"><div class="group-header">{[this.showHeader(values.' + combo.groupName + ')]}</div></tpl>',
    '<div class="x-boundlist-item"><input type="checkbox" />{name}</div>',
  '</tpl>'