maecy m
maecy m

Reputation: 1397

Extjs 4.2: Get sum of values of a column

I have a grid, and I want to get all the values of a column and get the sum. I've tried grid summary but I can't get it working.

This is my grid code:

Ext.ns('dlti.view.widget');


Ext.define('dlti.view.widget.PlaylistDetailsGrid' ,{
extend: 'Ext.grid.Panel',
id: 'playlist-details',
alias: 'widget.PlaylistDetailsGrid',
forceFit: true,
stripeRows: true,
selType: 'rowmodel',
autosync: true,
height: 150,
width: 950,


store: new dlti.store.PlaylistDetailsStore(),


columns: [


    {
        text: 'Filename',
        dataIndex: 'filename',
        renderer:   function renderDescTarget(val, p, record) {
            var desc = '';
            desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
            return desc;
        }
    },
    {
        text: 'Transition',
        dataIndex: 'transition',
        renderer:   function renderDescTarget(val, p, record) {
            var desc = '';
            desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
            return desc;
        }
    },
    {
        text: 'Stay Time',
        dataIndex: 'timeframe',
        renderer:   function renderDescTarget(val, p, record) {
            var desc = '';
            desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
            return desc;
        }
    }



]
 });

Please help me. I really need to finish my project.

EDIT

Ext.define('dlti.store.PlaylistDetailsStore', {
extend: 'Ext.data.Store',
model: 'dlti.model.PlaylistDetailsModel',
storeId: 'playlist-details',
proxy: {
            type: 'memory',
            autosync: true,
            reader: {
                type: 'json',
                root: 'result'
            }
        }
});

Upvotes: 1

Views: 7673

Answers (5)

Luca Migliori
Luca Migliori

Reputation: 119

summaryRenderer: function(value, summaryData, field) {
        // TODO: assign value to an in-scope variable
        // or pass value to another function
        console.log(value.toString());
        return value;
    }

Upvotes: 0

Rio Adetya Rizky
Rio Adetya Rizky

Reputation: 361

i assume your grid has an ID that named "YourGridID" and you have a grid with 3 column that named "goods | qty | price"

var grid = Ext.getCmp('YourGridID'),
    store = grid.getStore();

var totalPrice = store.sum('price');

console.log(totalPrice);

if you're not familiar with console.log you can use alert to show totalPrice

Upvotes: 0

Rajinder
Rajinder

Reputation: 346

maecy, try this if it helps:

    myStore.on('load', function(store, records){
        console.log(records);  //see if store has any records
        console.log(store.sum('fieldName')); //if store has any records, it would print sum of all values of fieldName column.
    }, this);

Upvotes: 1

Naresh Tank
Naresh Tank

Reputation: 1568

Try this new code for your store.

Ext.define('dlti.store.PlaylistDetailsStore', {
extend: 'Ext.data.Store',
config: {
  model: 'dlti.model.PlaylistDetailsModel',
  storeId: 'playlist-details',
  proxy: {
            type: 'memory',
            autosync: true,
            reader: {
                type: 'json',
                root: 'result'
            }
        }
 }
});

Upvotes: 0

Evan Trimboli
Evan Trimboli

Reputation: 30092

The store already has this functionality:

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    fields: ['visits']
});

Ext.require('*');

Ext.onReady(function() {
    var s = new Ext.data.Store({
        model: MyModel,
        data: [{
            visits: 1
        }, {
            visits: 2
        }, {
            visits: 3
        }]
    });
    console.log(s.sum('visits'));
});

Doc link: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store-method-sum

Upvotes: 0

Related Questions