简体   繁体   中英

Total the amount from Grid and display in textbox in sencha

I have view name PurchaseReport.js

Ext.define("App.view.tabs.PurchaseReport", {
extend: "Ext.panel.Panel",
alias: "widget.PurchaseReportTab",
requires: [
        "App.model.PurchaseReport", "Ext.toolbar.Toolbar"
],
border: false,
layout: "fit",
items: [
    App.Util.buildBrowseConfig({}, {
        controller: "PurchaseReport",
        primaryKeyField: "PurchaseReportId",
        stateful: true,
        stateId: "App.view.windows.PurchaseReport-grid",

        columns: [
          { dataIndex: "PurchaseCost", filter: true, header: "Purchase Cost", width: 150 }
        ],
        features: [{ ftype: "filters", autoReload: false, local: true }],
        store: { model: "App.model.PurchaseReport", sorters: [{ property: "Name", direction: "asc" }],
        height: 200,
        width: 400,
        bbar: [
        {
            dataIndex: "PurchaseCost",
            reference: 'purchaseCostField',
            xtype: 'textfield',
            name: 'Total',
            fieldLabel: 'Total',
            allowBlank: false
        }
        ],
        renderTo: Ext.getBody()

       }
    })
 ]
});

This is my controller part where my grid get bind as PurchaseReport.js,

Ext.define("App.controller.tabs.PurchaseReport", {
extend: "Ext.ux.app.BrowseController",
views: ["tabs.PurchaseReport"],
refs: [
    {
        ref: "myPurchaseReportGrid",
        selector: "PurchaseReportTab > gridpanel"
    }
],

init: function () {
    this.control({
        PurchaseReportTab: {
            bind: function (a, c) {
             **//Grid bind start**
                var b = this.getMyPurchaseReportGrid();
                b.getSelectionModel().deselectAll();
                b.store.removeAll();
                b.fireEvent("bind", b, c)
                **//Grid bind End**

             **//Combobox Bind start**
                var combo = this.getCoachCombo(),
                      store = combo.store,
                      options = store.lastOptions || {};
                options = Ext.apply({
                    callback: function () {
                        combo.select(App.coach.CoachId)
                        //console.log("called rajesh");
                    }
                }, options);
                store.load(options);
                if (App.coach.IsAdmin) {
                    combo.show()
                }
                **//Combobox Bind end**


                var abilities = App.coach.Abilities,
                      toolbar = this.getToolbar();
                for (var x = 0; x < abilities.length; x++) {

                    var ability = abilities[x],
                        button = toolbar.query("button[tooltip=" + ability.Name + "]");
                    if (button.length) {
                        button[0].setVisible(true)
                    }
                }
              store.load(options);
                var purchaseCostField = this.getReferences().purchaseCostField;
                purchaseCostField.setValue(store.sum('PurchaseCost'));
            }
        },

        "PurchaseReportTab > gridpanel": {
            bind: this.bind,
            itemdblclick: this.handleRecord,
            selectionchange: this.selectionChange
        }


    })
}

});

This is my model part name as PurchaseReport.js

 Ext.define("App.model.PurchaseReport", {
extend: "Ext.data.Model",
fields: [
    {
        name: "PurchaseCost",
        type: "date"
    }
],

proxy: {
    type: "ajax",
    url: "ControllerFactory.aspx",
    extraParams: {
        controller: "PurchaseReport",
        operation: "GetPurchaseReportsByCoachIdAndDates"
    },
    reader: {
        type: "json",
        root: "data",
        successProperty: "success"
    }
}

});

The page looks like this and I want display total of PurchaseCost in Textbox like to total of PurchaseCost around 1195 as per the below Image

在此处输入图片说明

A quick way to do it

Add a reference to your textfield:

        {
            dataIndex:"PurchaseCost",
            reference: 'purchaseCostField',
            xtype: 'textfield',
            name: 'Total',
            fieldLabel: 'Total',
            allowBlank: false
        }

In your controller after your store load add the value to the textfield:

store.load(options);
var purchaseCostField = this.getReferences().purchaseCostField;
purchaseCostField.setValue(store.sum('PurchaseCost'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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