[CLOSED] CustomSummaryType and ColumnName

  1. #1

    [CLOSED] CustomSummaryType and ColumnName

    Is there a way to get the Column ID calling the CustomSummaryType function?
    I need to implement some runtime consideration before to calculate the summary and I would avoid to create different function for different columns, since I can implement a switch for two different calc methods and 15 columns.

    I tried to retrieve more than one parameter, and I found that the 2nd parameter returns an array of column values; I should search for which column has a specific value, but if there is a better way it could be appreciated.

    In case there is no better way to find the column id, could you please consider to add an overload?

    Thank you!
    Last edited by Daniil; Aug 18, 2015 at 3:17 PM. Reason: [CLOSED]
  2. #2
    Hi @bbros,

    Indeed, I don't see a built-in way to get a Column's information. Well, it was designed so - separation of concepts. I.e. calculations are done on the data level and not quite related to UI.

    I was able to find relatively a simple way to get a Column's DataIndex. Hope this helps you.
    Ext.data.Store.override({
        getAggregate: function (fn, scope, records, field) {
            var values = [],
                len = records.length,
                i;
    
            for (i = 0; i < len; ++i) {
                values[i] = records[i].get(field);
            }
    
            return fn.call(scope || this, records, values, field && field[0]); // added the field argument
        }
    });
    Then in a summary function:
    var summaryFunction = function (records, values, field) {
        console.log(field);
    }
  3. #3
    Thank you for the quick answer.

    I pasted your
    Ext.data.Store.override
    inside a <script> block in the page as first call, and then I added the summaryFunction.

    Inside the console I get always 0 as result and it seems to me that the function has not been overridden.
    I added inside the getAggregate function a
    console.log("overridden");
    which does not log.

    I add the store in codebehind, after a button click; may this cause some issue?

    Where am I wrong?
    Thank you again.
  4. #4
    Do you have any JavaScript error in a browser console?

    Do you add an override in a partial view or in a main view?

    Providing us with a sample to reproduce might cause sorting out the problem quickly.
  5. #5

    Sample code

    Here is a sample project; in this sample I have 3 columns in store and I expect to get in console something like
    1
    2
    1
    2
    instead of
    0
    0
    0
    0
    <%@ Page Language="vb" AutoEventWireup="false" Inherits="BBros.ExtCookbook.GridAggregateOverride" %>
    
    <script runat="server">
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim rm As New ResourceManager
            Me.form1.Controls.Add(rm)
            Me.form1.Controls.Add(GetGridPanel())
            Dim btn As New Ext.Net.Button With {.Text = "Load data", .Icon = Icon.DatabaseConnect}
            AddHandler btn.DirectClick, AddressOf btn_DirectClick
            Me.form1.Controls.Add(btn)
        End Sub
    
        Private Function GetGridPanel() As GridPanel
            Dim m As New Model
            m.Fields.Add("groupMember", ModelFieldType.String)
            m.Fields.Add("Num", ModelFieldType.Int)
            m.Fields.Add("Num2", ModelFieldType.Int)
    
            Dim s As New Store
            s.ID = "MyStore"
            s.Model.Add(m)
            s.GroupField = "groupMember"
    
    
            Dim grd As New GridPanel With {
                .ID = "MyGrid",
                .Width = 300,
                .Header = 200,
                .Title = "Grid"}
    
            grd.ColumnModel.Columns.Add(New Column With {.DataIndex = "groupMember", .Text = "groupMember"})
            grd.ColumnModel.Columns.Add(New Column With {.DataIndex = "Num", .Text = "Num", .CustomSummaryType = "summaryFunction"})
            grd.ColumnModel.Columns.Add(New Column With {.DataIndex = "Num2", .Text = "Num2", .CustomSummaryType = "summaryFunction"})
            grd.Store.Add(s)
    
            grd.Features.Add(New GroupingSummary With {.GroupHeaderTplString = "{name}",
                                                       .HideGroupedHeader = True,
                                                       .EnableGroupingMenu = False})
    
            Return grd
        End Function
    
        Private Sub btn_DirectClick(sender As Object, e As DirectEventArgs)
            Ext.Net.X.GetCmp(Of Store)("MyStore").DataSource = GetEvenOddDataSource()
            Ext.Net.X.GetCmp(Of GridPanel)("MyGrid").DataBind()
        End Sub
        
        Public Shared Function GetEvenOddDataSource() As Object()
            Dim o(19) As Object
            For n = 0 To 19
                o(n) = New With {.groupMember = CStr(IIf(n Mod 2 = 1, "Odd", "Even")), .Num = n, .Num2 = Now.Second}
            Next
            Return o
        End Function
    
    </script>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style>
            .x-grid-row-summary .x-grid-cell-inner {
                font-weight: bold;
                font-size: 11px;
                background-color: #f1f2f4;
            }
        </style>
        <script>
            Ext.data.Store.override({
                getAggregate: function (fn, scope, records, field) {
                    console.log("overridden");
    
                    var values = [],
                        len = records.length,
                        i;
    
                    for (i = 0; i < len; ++i) {
                        values[i] = records[i].get(field);
                    }
    
                    return fn.call(scope || this, records, values, field && field[0]); // added the field argument
                }
            });
    
            var summaryFunction = function (records, values, field) {
                console.log(field);
            };
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            </div>
        </form>
    </body>
    </html>
  6. #6
    Thank you for the test case. Yes, in a case with grouping it requires another override.

    Please try this one:
    Ext.util.Collection.override({
        aggregateItems: function (items, property, operation, begin, end, scope) {
            var me = this,
                range = Ext.Number.clipIndices(items.length, [
                    begin,
                    end
                ]),
    
                subsetRequested = (begin !== 0 && end !== items.length),
                i, j, rangeLen, root, value, values, valueItems;
    
            begin = range[0];
            end = range[1];
    
            if (!Ext.isFunction(operation)) {
                operation = me._aggregators[operation];
                return operation.call(me, items, begin, end, property, me.getRootProperty());
            }
    
            root = me.getRootProperty();
            values = new Array(rangeLen);
            valueItems = subsetRequested ? new Array(rangeLen) : items;
    
            for (i = begin, j = 0; i < end; ++i, j++) {
                if (subsetRequested) {
                    valueItems[j] = value = items[i];
                }
                values[j] = (root ? value[root] : value)[property];
            }
    
            return operation.call(scope || me, items, values, property); // replaced "0" with "property"
        }
    });
  7. #7

    Perfect

    Thank you very much, this works like a charm.

    Is in your opinion possible to get this override in some next release?
    Should Sencha take this decision and is it possible to submit to Sencha this request?

    You can mark this post as Closed, thank you again.
  8. #8
    Personally, I am reluctant to add this into SVN. First of all, I am not 100% sure it will work in all cases. Also it adds an effort on maintenance.

    Yes, ideally, it would be nice if this is added by Sencha. You are welcome to submit a feature request on their forums.

    By the way, it looks like I've found a way to avoid overrides and still have one summary function with a DataIndex attribute passed.
    Dim dataIndex As String = "Num"
    grd.ColumnModel.Columns.Add(New Column With {.DataIndex = dataIndex, .Text = dataIndex, .CustomSummaryType = "Ext.bind(summaryFunction, null, ['" + dataIndex + "'], 2)"})
    dataIndex = "Num2"
    grd.ColumnModel.Columns.Add(New Column With {.DataIndex = dataIndex, .Text = dataIndex, .CustomSummaryType = "Ext.bind(summaryFunction, null, ['" + dataIndex + "'], 2)"})
  9. #9

    This looks great!

    This way is much more than I could imagine; I'll be able to implement this override inside my little codebehind-framework.

    Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks
  10. #10
    I am happy you liked the alternative approach! Thank you for using Ext.NET!

Similar Threads

  1. Replies: 3
    Last Post: Nov 19, 2014, 5:30 AM
  2. [CLOSED] Get ColumnName from ColumnModel using javascript
    By speedstepmem4 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 17, 2013, 12:28 PM
  3. [CLOSED] ColumnName with Special Characters in GridPanel
    By UnifyEducation in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Jun 07, 2012, 7:12 PM
  4. [0.8.2] Bug in gridpanel (invalid columnname)
    By plykkegaard in forum Bugs
    Replies: 6
    Last Post: Dec 02, 2010, 3:15 PM

Posting Permissions