javascript - EXT 4.2 ComboBox 使用 XTemplate 对结果进行分组

标签 javascript extjs combobox grouping extjs4.2

我正在尝试对从商店获得的结果进行分组,以便在 ComboBox 中显示。 我有一个看起来像这样的组合框: enter image description here

我需要它看起来像这样:

enter image description here

这意味着按类别(订单/发票)分组。

我的组合框是这样定义的:

Ext.define('NG.view.searchcombo.Combo', {
    requires: ['Ext.form.field.ComboBox'],
    extend: 'Ext.form.ComboBox',
    alias: 'widget.searchcombo',
    minChars:3,
    fieldLabel: 'Choose Search',
    store: 'Search',
    displayField: 'name',
    valueField: 'id',
    typeAhead: false,
    hideLabel: true,
    hideTrigger:false,
    anchor: '100%',

    listConfig: {
        loadingText: 'Searching...',
        emptyText: 'No matching posts found.',

        // Custom rendering template for each item
        getInnerTpl: function() {
            return '<h3>{name} / {category}</h3>' +'{excerpt}' ;
        }
    },
    pageSize: 10,
    initComponent: function () {    

        this.callParent(arguments);
    }
});

我的数据是这样的:

[{
    "id": 1,
    "name": "one",
    "category": "invoice"
}, {
    "id": 2,
    "name": "two",
    "category": "invoice"
}, {
    "id": 3,
    "name": "one",
    "category": "order"
}, {
    "id": 4,
    "name": "two",
    "category": "order"
}, {
    "id": 5,
    "name": "three",
    "category": "invoice"
}, {
    "id": 6,
    "name": "four",
    "category": "invoice"
}, {
    "id": 7,
    "name": "three",
    "category": "order"
}, {
    "id": 8,
    "name": "four",
    "category": "order"
}, {
    "id": 9,
    "name": "five",
    "category": "invoice"
}, {
    "id": 10,
    "name": "six",
    "category": "invoice"
}, {
    "id": 11,
    "name": "five",
    "category": "order"
}, {
    "id": 12,
    "name": "six",
    "category": "order"
}, {
    "id": 13,
    "name": "seven",
    "category": "invoice"
}, {
    "id": 14,
    "name": "eight",
    "category": "invoice"
}, {
    "id": 15,
    "name": "seven",
    "category": "order"
}, {
    "id": 16,
    "name": "eight",
    "category": "order"
}]

我认为这可以通过使用 Ext.XTemplate 来完成,但我不熟悉 Ext.XTemplate

最佳答案

我想要一个更简单的解决方案,所以我将分享我的想法。

出于我的目的,我有一个,我想根据它进行分组,它是一个字符。我知道我想为每个键显示的 header ,所以我预先对列表进行排序以确保类型放在一起,然后每次看到新键时只呈现一个组 header 。

myStore.sort('key', 'DESC');

Ext.create('Ext.form.field.ComboBox', {
  store: myStore,
  queryMode: 'local',
  displayField: 'name',
  valueField: 'id',
  listConfig: {
    cls: 'grouped-list'
  },
  tpl: Ext.create('Ext.XTemplate',
    '{[this.currentKey = null]}' +
    '<tpl for=".">',
      '<tpl if="this.shouldShowHeader(key)">' +
        '<div class="group-header">{[this.showHeader(values.key)]}</div>' +
      '</tpl>' +
      '<div class="x-boundlist-item">{name}</div>',
    '</tpl>',
    {
      shouldShowHeader: function(key){
        return this.currentKey != key;
      },
      showHeader: function(key){
        this.currentKey = key;
        switch (key) {
          case 's': return 'Structures';
          case 'f': return 'Filters';
          ...
        }
        return 'Other';
      }
    }
  )
});

使用以下 CSS:

.grouped-list .x-boundlist-item {
  padding: 1px 3px 0 10px
}

.grouped-list .group-header {
  padding: 4px;
  font-weight: bold;
  border-bottom: 1px solid #ddd;
}

还有这个数据:

[
    { key: 's', name: '2014 Product Development' },
    { key: 'f', name: 'Message Filter' },
    { key: 's', name: '2014 Product Development (Little)' },
    { key: 's', name: 'Global Structure' },
    { key: 'f', name: 'My SW' }
]

我得到一个像这样的漂亮的分组列表:

关于javascript - EXT 4.2 ComboBox 使用 XTemplate 对结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17217266/

相关文章:

WPF:将组合框中的 DisplayMemberPath 绑定(bind)到项目

javascript - Jquery 下拉页面?触摸屏友好的下拉菜单

javascript - extjs4.1 根据用户定义的标准对商店进行排序

javascript - 如何向 EXTjs 编辑器网格面板中具有列变量的特定列添加样式?

c# - 为 ComboBox 中的项目分配非顺序索引

javascript - 使用 NodeJS TLS 套接字的 HTTPS 通信

javascript - 使用 CSS 和 jQuery 的简单选项卡 - 自定义和复制

c# - 保存 Site.master 回发事件中的 <LI> 类值

javascript - Lightswitch 2013 - HTML 客户端 -> commitChanges() 和 showOtherPage() 联合

css - Sencha 构建 - app.scss 崩溃