简体   繁体   English

ExtJS 在网格中为 hasMany 关联添加过滤

[英]ExtJS Add filtering in Grid for hasMany association

I have a data model that I want to be able to add a generic amount of filters to.我有一个数据 model 我希望能够添加通用数量的过滤器。 I am specifying a name and a value.我正在指定一个名称和一个值。 How can I add these hasMany associated fields as filters to my grid?如何将这些 hasMany 关联字段作为过滤器添加到我的网格中? I have attempted to write custom filtering option, but I can't have filters show up without an attached dataIndex, which is not available for the hasMany association.我试图编写自定义过滤选项,但是如果没有附加的 dataIndex,我无法显示过滤器,这对于 hasMany 关联不可用。

Ext.define('AirGon.model.Project', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'Link', type: 'string' },
        { name: 'Title', type: 'string' },
        { name: 'Description', type: 'string' },
        { name: 'MappedMetadata', mapping: 'Metadata'},
        { name: 'XMax', type: 'float' },
        { name: 'XMin', type: 'float' },
        { name: 'YMax', type: 'float' },
        { name: 'YMin', type: 'float' }
    ],
    hasMany: { model: 'Metadata', name: 'Metadata', associationKey: 'Metadata' }    
});

Ext.define('Metadata', {
    extend: 'Ext.data.Model',
    fields: ['Name', 'Value']
});

This is my custom filtering attempt.这是我的自定义过滤尝试。 I am getting the DataStore from the main store and then adding custom rendering, but I can't filter or sort this column because of the lack of a dataIndex.我从主商店获取 DataStore,然后添加自定义渲染,但由于缺少 dataIndex,我无法过滤或排序此列。

var column = {
                header: 'Meta',
                //?????????dataIndex: 'MappedMetadata[0]',????? 
                sortable: true,
                filterable: true,
                filter: {
                    type: 'string'
                },
                renderer: function (value, meta, record) {
                    console.log(record.MetadataStore.data.items[index].data)
                    return record.MetadataStore.data.items[index].data.Value;
                }
            }

Data.数据。 The data is modeled so that a variable amount of metadata can be entered and the 'tag' will not be known by the system.数据被建模,以便可以输入可变数量的元数据,并且系统不会知道“标签”。

{
      "Link": "link.com",
      "Title": "project",
      "Description": "descript",
      "State": "",
      "Metadata": [
         {
            "Name": "County",
            "Value": "32"
         },
         {
            "Name": "Info",
            "Value": "info"
         },
         {
            "Name": "State",
            "Value": "TN"
         }
      ],
      "XMin": "-1",
      "XMax": "-1",
      "YMin": "1",
      "YMax": "1"
   } 

I was able to solve this by dynamically flattening the data model and adding columns once the store has been loaded.我能够通过动态展平数据 model 并在加载存储后添加列来解决此问题。 Although this breaks the MVC structure this was the best solution I came up with so it might be able to help you.尽管这破坏了 MVC 结构,但这是我想出的最佳解决方案,因此它可能会对您有所帮助。

var defaultColumns = [];
var store = Ext.getStore('store');

        store.on('load', function (store) {
            var model = store.model;
            var fields = model.getFields();
            store.getAt(0).MetadataStore.data.items.forEach(function (item, index) {                
                var header = item.data.Name;
                //isField returns a boolean if the field is already part of the data model
                if (!isField(fields, header)) {
                    //Add a new metadata field to the data model
                    var field = { name: header, mapping: 'Metadata[' + index + '].Value' }
                    fields.push(field)

                    //Add a new metadata column
                    var column = {
                        header: header,
                        dataIndex: header,
                        sortable: true,
                        filterable: true,
                        filter: {
                            type: 'list'
                        },
                        flex: 0.2
                    }

                    defaultColumns.push(column);
                }
            });
            model.setFields(fields);

            //reload the grid after adding columns
            var gridView = Ext.ComponentQuery.query('gridpanel')[0];
            gridView.reconfigure(store, defaultColumns);      
        });

        //reload the data after creating new fields
        store.load();

I then set the columns of the grid to defaultColumns.然后我将网格的列设置为 defaultColumns。

{
    xtype: 'grid',                    
    store: 'Projects',
    overflowX: 'auto',
    autoScroll: true,
    features: [filters],
    columns: defaultColumns
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM