extjs - extjs的gridpanel中是否可以显示多个汇总行?

标签 extjs grid extjs4 gridpanel summary

以下是我的代码,用于显示带有总成本摘要的网格面板。我想显示另一个带有平均值的摘要行。有什么帮助吗?

    Ext.require(['Ext.data.*', 'Ext.grid.*']);

    Ext.onReady(function() {

    Ext.define('NewEstimate', {
        extend: 'Ext.data.Model',
        fields: ['description', 'cost'],
        validations: [{
            type: 'length',
            field: 'description',
            min: 3
        }, {
            type: 'int',
            field: 'cost',
            min: 1
        }]
    });

    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');

    var store = Ext.create('Ext.data.Store', {
            autoLoad: true,
            autoSync: false,
            model: 'NewEstimate',
            proxy: {
                type: 'rest',
                url: 'app.php/users',
                reader: {
                    type: 'json',
                    root: 'data'
                },
                writer: {
                    type: 'json'
                }
            },
            listeners: {
                write: function(store, operation){
                    var record = operation.records[0],
                        name = Ext.String.capitalize(operation.action),
                        verb;

                    if (name == 'Destroy') {
                        verb = 'Destroyed';
                    } else {
                        verb = name + 'd';
                        alert(name);
                    }
                    Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));

                }
            }
        });

    var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1,
            listeners: {
                edit: function(){
                    grid.getView().refresh();
                }
            }
        });

    var data = [
        {projectId: 100, taskId: 112, description: 'Integrate 2.0 Forms with 2.0 Layouts', cost: 150},
        {projectId: 100, taskId: 113, description: 'Implement AnchorLayout', cost: 150},
        {projectId: 100, taskId: 114, description: 'Add support for multiple types of anchors', cost: 150},
        {projectId: 100, taskId: 115, description: 'Testing and debugging', cost: 0}
    ];

    var gridPanel = new Ext.create('Ext.grid.Panel', {
        width: 600,
        height: Ext.getBody().getViewSize().height * 0.3,
        renderTo: document.body,
        plugins: [rowEditing],
        features: [{
            ftype: 'summary'
        }
        ],
        store: store,
        columns: [{
            dataIndex: 'description',
            flex: 1,
            text: 'Description',
            summaryType: 'count',
            field: {
                    xtype: 'textfield',
                    allowBlank: false
                },
            summaryRenderer: function(value){
                return Ext.util.Format.leftPad('Estimate Total (EUR)');
            }   
        }, {
            dataIndex: 'cost',
            width: 75,
            text: 'Line Total',

             field: {
                    xtype: 'numberfield',
                    allowBlank: false
                },
            summaryType: function(records){
                    var i = 0,
                        length = records.length,
                        total = 0,
                        record;

                    for (; i < length; ++i) {
                        record = records[i];
                        total = total + Number(record.get('cost'));
                    }
                    return total;
                }
        }],
        dockedItems: [
               {
             xtype: 'toolbar',
                items: [{
                    text: 'Add',
                    iconCls: 'icon-add',
                    handler: function(){
                        // empty record
                        store.insert(0, new NewEstimate());
                        rowEditing.startEdit(0, 0);
                    }
                } , '-', 
                {
                    text: 'Delete',
                    iconCls: 'icon-delete',
                    handler: function(){

                        var selection = gridPanel.getView().getSelectionModel().getSelection()[0];

                        if (selection) {
                            store.remove(selection);
                        }
                    }
                }]
            }]
    });
    });

提前致谢

最佳答案

我也有类似的需求。如果有足够的时间,我可能会扩展摘要功能以支持多个聚合。然而,我没有时间,所以我做了一种肮脏的解决方法。

我没有尝试获取两行,而是仅使用summaryType作为函数,并以带有BR标记的字符串形式返回值。它确实很笨重,但它可以满足我的需要。

类似于:

columns: [
   {
      header:'Quantity',
      dataIndex: 'Quantity',
      summaryType"="function( records ) {
         sum = 0;
         for( rec in records ) {
            var record = records[ rec ];
            sum += record.data[ '#guide#_Quantity' ];
         }
         avg= Math.floor( sum/records.length );
         return '<strong>'+ sum + ' Books<br />' + avg + ' Books</strong>';
      }
   }
]

关于extjs - extjs的gridpanel中是否可以显示多个汇总行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12930982/

相关文章:

javascript - 问题包装 Extjs Accordion

java - GridView 获取触摸项目

javascript - 多表查询 + 指定返回值

extjs - 分机 JS 4 : No reloading of stores

javascript - 复选框选择模型 - 检查列标题文本

extjs - 如何防止 extjs 模型/代理在创建时保存空的主 ID

extjs - 如何在存储代理的配置中使用 api 属性 - CRUD 方法

android - 在 Android 的 sencha 应用程序中覆盖后退按钮

javascript - ExtJS 5.0 : Grid remoteSort always sorts by the default 'sorters' only

c# - 网格边框/单元格之间的间隙