Sum values in grid

Page 1 of 2 12 LastLast
  1. #1

    Sum values in grid

    Hi all
    filter works OK, and now I need to LOOP through filtered grid records and add it to some variable.
    I have found some example for exporting:

            var exportData = function (format) {
                FormatType.setValue(format);
                var store = GridPanel1.store;
                store.directEventConfig.isUpload = true;
    
                var records = store.reader.readRecords(store.proxy.data).records,
                    values = [];
    
                for (i = 0; i < records.length; i++) {
                    var obj = {}, dataR;
                    if (store.reader.meta.id) {
                        obj[store.reader.meta.id] = records[i].id;
                    }
    
                    dataR = Ext.apply(obj, records[i].data);
                    if (!Ext.isEmptyObj(dataR)) {
                        values.push(dataR);
                    }
                }
                store.submitData(values);
                store.directEventConfig.isUpload = false;
            };
    complette code goes through grid and export it to Excel.
    how can I get sum of some columns in grid (for example Float1 and Float2 columns)?

    For example:
    Float1
    2.00
    1.00

    at the end, I need to have
    Float1= 3.00

    What is the code for adding values into single variable?
    I know (from above example) that records.length is number of records in the grid, but don't know what is the name of each grid column variable?
    Thanks
  2. #2

    Sum by column only

    Quote Originally Posted by UserClarion View Post
    Hi all
    What is the code for adding values into single variable?
    I know (from above example) that records.length is number of records in the grid, but don't know what is the name of each grid column variable?
    Thanks
    Anybody will help on how to LOOP through filtered Grid and sum some values?
    Please?

    I have few columns. I need to know (it don't need to be visible, just calculated) sum of each column on page_load event, and after when records are filtered.
    Have done filtering, but can not find how to write simple LOOP from 0 to NumberOfRecordsInGrid and add values to the variable, for example:
    Var1 += col1Value
    Var2 += col2Value

    Anyone, please?
    Thanks.
  3. #3
    Hi,

    The following example should help.
    https://examples1.ext.net/#/GridPane...Grid_TotalRow/
  4. #4
    Quote Originally Posted by Daniil View Post
    Hi,

    The following example should help.
    https://examples1.ext.net/#/GridPane...Grid_TotalRow/
    Hi
    thanks a lot.
    That example do the job, but there is some code which I don't need (for example, I need only sum of numbers in the second column, and that sum don't need to be displayed because I'm using PagingToolbar and I need some buttons on it.)
    Problem is - I'm a total newbie and if possible, I will see some simple example :how to sum values on second column?
    Have try to remove unwanted code but it is not simple to me :(
    Thanks a lot.
  5. #5
    Here you are.

    Example
    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.Store1.DataSource = new object[] 
                { 
                    new object[] { 1, 4 },
                    new object[] { 2, 5 },
                    new object[] { 3, 6 }
                };
                this.Store1.DataBind();
            }
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    
        <script type="text/javascript">
            var getSum = function (grid, index) {
                var dataIndex = grid.getColumnModel().getDataIndex(index),
                    sum = 0;
    
                grid.getStore().each(function (record) {
                    sum += record.get(dataIndex);
                });
    
                return sum;
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
                <Store>
                    <ext:Store ID="Store1" runat="server">
                        <Reader>
                            <ext:ArrayReader>
                                <Fields>
                                    <ext:RecordField Name="test1" />
                                    <ext:RecordField Name="test2" />
                                </Fields>
                            </ext:ArrayReader>
                        </Reader>
                    </ext:Store>
                </Store>
                <ColumnModel runat="server">
                    <Columns>
                        <ext:Column Header="Test1" DataIndex="test1" />
                        <ext:Column Header="Test2" DataIndex="test2" />
                    </Columns>
                </ColumnModel>
            </ext:GridPanel>
            <ext:Button runat="server" Text="Sum 1">
                <Listeners>
                    <Click Handler="alert(getSum(GridPanel1, 0));" />
                </Listeners>
            </ext:Button>
            <ext:Button runat="server" Text="Sum 2">
                <Listeners>
                    <Click Handler="alert(getSum(GridPanel1, 1));" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
    The methods I used you can find in the ExtJS docs, for example:
    http://docs.sencha.com/ext-js/3-4/#!...d-getDataIndex
  6. #6
    Quote Originally Posted by Daniil View Post
    Here you are.

    Example
    <%@ Page Language="C#" %>
    The methods I used you can find in the ExtJS docs, for example:
    http://docs.sencha.com/ext-js/3-4/#!...d-getDataIndex
    Hi Daniil,
    this is what I looking for.
    Thanks a lot.
  7. #7
    Quote Originally Posted by UserClarion View Post
    Hi Daniil,
    this is what I looking for.
    Thanks a lot.
    I know I'm not finished :(
    Problem is: I'm call JavaScript function on some Filter event.

    <ext:DateFilter DataIndex="DATES" >
        <DatePickerOptions runat="server" TodayText="Today" />
        <Listeners>
            <Activate Handler="chartallprojects(getSum(GridPanel1, 2) / getSum(GridPanel1, 1) * 100);" />
            <Update Handler="chartallprojects(getSum(GridPanel1, 2) / getSum(GridPanel1, 1) * 100);" />
            <Deactivate Handler="chartallprojects(getSum(GridPanel1, 2) / getSum(GridPanel1, 1) * 100);" />
        </Listeners>
    </ext:DateFilter>
    chartallprojects is javascript which draw some gauge from returned value.
    Now, looks like I need to read values from grid instead of from store?
    or, I need to somehow refresh filtered grid (have try with grid.reload(); but it does not work.
    Thanks
  8. #8
    Now, looks like I need to read values from grid instead of from store?
    All the same a data is in a store, not in a grid.

    I would suggest you to use the Store's DataChanged event.
    http://docs.sencha.com/ext-js/3-4/#!...nt-datachanged
  9. #9
    Quote Originally Posted by Daniil View Post
    All the same a data is in a store, not in a grid.

    I would suggest you to use the Store's DataChanged event.
    http://docs.sencha.com/ext-js/3-4/#!...nt-datachanged
    Hi
    thanks again for your support.
    I must say that I will never find it.
    But...
    If I use your previous example and put getsum into the DataChanged event:

    <Store>
        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:ArrayReader>
                    <Fields>
                        <ext:RecordField Name="test1" />
                        <ext:RecordField Name="test2" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
    
        <Listeners>
            <DataChanged Handler="alert(getSum(GridPanel1, 1));" />
        </Listeners>
    
        </ext:Store>
    </Store>
    nothing is displayed.
    How to use DataChanged?
    Thanks a million.
  10. #10
    Please set up Delay="10".
    <DataChanged Handler="alert(getSum(GridPanel1, 1));" Delay="10" />
Page 1 of 2 12 LastLast

Similar Threads

  1. Adding control values to the grid
    By Vaishali in forum 1.x Help
    Replies: 0
    Last Post: Apr 17, 2012, 11:55 AM
  2. How to get values from grid panel selected row
    By mehmood in forum 1.x Help
    Replies: 9
    Last Post: Apr 11, 2012, 12:09 PM
  3. Replies: 2
    Last Post: Jul 06, 2011, 8:04 PM
  4. [CLOSED] Grid Values Loop
    By majunior in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Apr 15, 2011, 1:34 PM
  5. how to get checked grid rows values in MVC
    By vs.mukesh in forum 1.x Help
    Replies: 0
    Last Post: Jun 23, 2010, 3:23 PM

Posting Permissions