【问题标题】:Summary + ajax paging on ExtJS.GridExtJS.Grid 上的摘要 + ajax 分页
【发布时间】:2015-09-04 17:44:35
【问题描述】:

我有一个网格,其数据类似于http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/array-grid.html,但使用类似于http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/paging.html 的分页代理。

我需要添加一个类似于http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/group-summary-grid.html的摘要,在网格底部只有1个。它必须汇总所有数据,而不仅仅是当前页面的数据。

我可以进行数据库查询,不需要 Ext 对值求和。但是我如何将汇总数据发送到 Ext.store 并从 store 发送到 grid?

【问题讨论】:

  • 也许向绑定到存储的模型添加附加字段会更容易(例如摘要)。从 json 填充该字段并显示在网格中的某处?
  • 也许,你见过我可以使用的例子吗?尽管如此,我还是需要ExtJs.Grid总结的视觉效果,它不能只是另一个网格行。
  • @Hikari 您始终可以使用 Column 的 SummaryRenderer 属性,您可以从中返回自定义值并应用格式。

标签: ajax extjs extjs4 paging summary


【解决方案1】:

将此作为示例处理。

这就是它的样子(当然它会是不同的数据,但你明白这一点):

让我们看看 json 的示例(它可能是怎样的以及我如何理解您的问题):

{
   "stats":[
      {
         "name":"Amount summary",
         "value":"2300"
      },
      {
         "name":"Mortgage",
         "value":"1100"
      }
   ],
   "success":true,
   "totalCount":2,
   "items":[
      {
         "Amount":"1000",
         "Delta":"3",
         "Mortgage":"500"
      },{
         "Amount":"1300",
         "Delta":"4",
         "Mortgage":"600"
      }
   ]
}

型号

Ext.define('Application.model.MyModel', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'Amount',
        type: 'string'
    },{
        name: 'Delta',
        type: 'string'
    },{
        name: 'Mortgage',
        type: 'string'
    }]
});

商店

var writer = new Ext.data.JsonWriter({
    type: 'json',
    encode: false,
    listful: true,
    writeAllFields: true,
    returnJson: true
});

var reader = new Ext.data.JsonReader({
    type: 'json',
    totalProperty: 'totalCount',
    rootProperty: 'items',
    successProperty: 'success'
});

var proxy = new Ext.data.HttpProxy({
    reader: reader,
    writer: writer,
    type: 'ajax',
    url: 'urlForJson',
    headers: {
        'Content-Type': 'application/json; charset=UTF-8'
    }
});

Ext.define('Application.store.MyStore', {
    extend  : 'Ext.data.Store',
    storeId : 'MyStore',
    model   : 'Application.model.MyModel',
    autoLoad: false,
    autoSync: true,
    proxy:proxy
});

让我们定义将处理我们的摘要/统计的控制器:

Ext.define('Application.controller.SummaryController', {
    extend: 'Ext.app.Controller',
    refs: [
        {
            selector: 'summaryPanel',
            ref: 'summaryPanel'
        }
    ],
    init: function () {
        var controller = this;
        controller.listen({
            controller: {
                '*': {
                    addSummary: 'addSummary'
                }
            }
        });
    },

    /**
     * Add summary.
     */
    addSummary: function addSummary(summary) {
        var controller = this;
        var summaryPanel= controller.getSummaryPanel();
        summaryPanel.removeAll();
        Ext.Object.each(stats, function(property, stat){
            var labelFieldName = new Ext.form.Label({
                text: stat.name+': '
            });
            var labelFieldValue = new Ext.form.Label({
                text: stat.value+'  ',
                cls: 'bold-item'
            });
            summaryPanel.items.add(labelFieldName);
            summaryPanel.items.add(labelFieldValue);
        });
        summaryPanel.up('window').doLayout();
    }
});

这是我们的SummaryPanel

Ext.define('Application.view.SummaryPanel', {
    extend: 'Ext.Panel',
    alias: 'widget.summaryPanel',
    xtype: 'summaryPanel',
    layout: 'anchor',
    bodyStyle: 'padding: 5px 5px;',
    items: []
});

在下一步中,我们将把数据加载到网格中,然后我们将用统计信息/摘要填充我们的SummaryPanel

Ext.define('Application.controller.GridController', {
    extend: 'Ext.app.Controller',
    init: function () {
        var controller = this;

        controller.control({
            'gridControlPanel': {
                'afterrender': function () {
                    controller.loadStore();
                }
            }
        })
    },
    loadStore: function () {
        var controller = this;
        var store = Ext.getStore('Application.store.MyStore');
        store.load({
            callback: function (records, operation, success) {
                var data = Ext.JSON.decode(operation._response.responseText);

                if (!Ext.isEmpty(data.stats)) {
                    //After loading data, make statistics.
                    controller.fireEvent('addSummary', data.stats);//This event is handled by SummaryController
                }
            }
        });
    }
});

还有这个UI,涵盖了以上所有内容:

Ext.define('Application.view.GridWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.gridWindow',
    xtype: 'gridWindow',
    addMode: false,
    autoShow: true,
    width: 1200,
    height: 700,
    resizable: true,
    layout: 'fit',
    items: [{
        xtype: 'grid',
        store: 'Application.store.MyStore',
        dockedItems: [{
            xtype: 'pagingtoolbar',
            dock: 'bottom',
            store: 'Application.store.MyStore',
            displayInfo: true
        }]
    }],
    dockedItems: [{
        xtype: 'summaryPanel'//This is our summary panel that will show your summarize.
    }]
});

【讨论】:

  • 有趣,感谢您的详细描述。但是如果这段代码导致的网格类似于http://i.stack.imgur.com/VVTsQ.png,我的经理不会接受。看不懂里面写了什么,不过好像在网格上面有总结。我需要看起来像http://dev.sencha.com/deploy/ext-4.0.1/examples/grid/group-summary-grid.html 中的“特殊行”的摘要。我只是不需要分组,并且需要从服务器提供的值,而不是由 Ext 在客户端计算。如果我将摘要添加到网格之外,我只需将它作为纯 HTML 添加到 Ext 之外。
  • 这类问题不是微不足道的,而且可能不会带来正确的答案。这是需要很好地实施并根据您的需求量身定制的东西。我刚刚提供了一些概念证明,您可以将它们用于您的实现。
  • 是的,我同意,我需要将多个示例“合并”为一个。我会试试你的代码,我首先将当前版本部署到服务器。基本上我需要的是一个类似于摘要示例的视图,没有分组,并且由于分页而从服务器提供摘要数据。
  • 我了解view.getFeature('group').toggleSummaryRow(showSummary); 使 Ext.Grid 显示摘要,并且每一列都有其 summaryRendererattribute 来格式化摘要值。在您的示例中, json 中的 "stats" 具有汇总值。 SummaryController.addSummary() 获取 stats 值并将其添加到摘要中。我无法理解的是您如何使用 SummaryController 并将其连接到网格中。我不需要工具栏来切换显示摘要,我只需要它。
  • 为了简化:哪些代码使 Ext.Grid 显示摘要以及哪些代码挂钩 json 的 "stats" 以使摘要显示其值。我可以将"stats" 添加到 json 中,如果可以的话,它会起作用。
【解决方案2】:

您可以使用 ExtJS 网格功能 -

Ext.grid.feature.Summary

在网格底部显示摘要。

这是回答您问题的简单示例 - https://fiddle.sencha.com/#fiddle/tq7

例如,我直接在summaryRenderer 的列中使用了summary 对象。

【讨论】:

  • 我不认为这回答了特定于带有分页工具栏的网格的问题。
【解决方案3】:

因此,对于摘要行,您已获得 Summary feature。您可以使用dock: 'bottom' 选项将其粘贴在网格底部。最后,要使用远程计算的数据进行摘要,您可以使用remoteRoot 选项。瞧。

...除了这件事显然是半生不熟和remoteRootis really only used with grouping。哎呀。 “不需要处理简单的案件”,他们一定是这样想的。

这是一个覆盖您需要的缺失部分的替代 (fiddle):

Ext.define('Ext.grid.feature.RemoteSummary', {
    override: 'Ext.grid.feature.Summary',

    // adds 'remote' summary type
    getSummary: function (store, type, field, group) {
        if (type === 'remote') {
            return this.getRemoteSummaryRecord(store).get(field);
        } else {
            return this.callParent(arguments);
        }
    },

    // helper for our custom summary type, mainly copied from:
    // http://docs.sencha.com/extjs/4.2.3/source/AbstractSummary.html#Ext-grid-feature-AbstractSummary-method-generateSummaryData
    getRemoteSummaryRecord: function(store) {
        if (!this.remoteSummaryRecord) {
            var reader = store.proxy.reader,
                remoteRoot = this.remoteRoot,
                root;

            if (remoteRoot && reader.rawData) {
                root = reader.root;
                reader.root = remoteRoot;
                reader.buildExtractors(true);

                this.remoteSummaryRecord = reader.read(reader.rawData).records[0];

                if (!reader.convertRecordData) {
                    reader.buildExtractors();
                }

                reader.root = root;
                reader.buildExtractors(true);
            }
        }
        if (!this.remoteSummaryRecord) {
            this.remoteSummaryRecord = store.model.create({});
        }
        return this.remoteSummaryRecord;
    },

    // ensure our remoteSummaryRecord stays fresh
    onStoreUpdate: function() {
        delete this.remoteSummaryRecord;
        return this.callParent(arguments);
    }
});

然后你会像这样使用它:

Ext.create('Ext.grid.Panel', {
    features: [{
        ftype: 'summary',
        remoteRoot: 'summary', // summary key in the server response
        dock: 'bottom'
    }],
    columns: [{
        text: 'Age',
        dataIndex: 'age',
        summaryType: 'remote'
    }]
    //...
});

用这种形式的一些数据:

{
    "summary": {
        "age": 95,
    },
    "items": [/*...*/]
}

(而且,是的,对年龄求和很有意义)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多