[CLOSED] Ext.data.Store getTotalCount method reports 0 in GridPanel listeners

  1. #1

    [CLOSED] Ext.data.Store getTotalCount method reports 0 in GridPanel listeners

    Hi,

    Please help me understand the lifecycle of some of the grid panel events and possible workaround. In the code sample below, the number of records reported by the Store getTotalCount() method for the filter is zero if invoked from Grid's Render listener. You can replace the filter's store identity with the main store ID and it shouldn't change a thing. In a more involved and loaded scenario, the ViewReady call also reports zero records albeit not consistently. When debugging, both stores appear to be populated and data bound in the Page_Load method on the server before any client side code is executed. I haven't encountered this issue in 1.x. Please advise if more info is needed.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <%@ Import Namespace="System.Data" %>
    <script runat="server">
        private DataTable TestData
        {
            get
            {
                var now = DateTime.Now;
                var dataTable = new DataTable();
                dataTable.Columns.AddRange(new DataColumn[] { 
                    new DataColumn { ColumnName = "company", DataType = typeof(System.String) },
                    new DataColumn { ColumnName = "industry", DataType = typeof(System.String) },
                    new DataColumn { ColumnName = "price", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "change", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "pctChange", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "lastChange", DataType = typeof(System.DateTime) },
                    new DataColumn { ColumnName = "visible", DataType = typeof(System.Boolean) },
                });
    
                var dataArray = new object[]
                {
                    new object[] { "3m Co", "Conglomerates", 71.72, 0.02, 0.03, now, true },
                    new object[] { "Alcoa Inc", "Basic Materials", 29.01, 0.42, 1.47, now, true },
                    new object[] { "Altria Group Inc", "Consumer Goods",  83.81, 0.28, 0.34, now, true },
                    new object[] { "American Express Company", "Financial", 52.55, 0.01, 0.02, now, true },
                    new object[] { "American International Group, Inc.", "Financial", 64.13, 0.31, 0.49, now, false },
                    new object[] { "AT&T Inc.", "Technology", 31.61, -0.48, -1.54, now, false },
                    new object[] { "Boeing Co.", "Industrial", 75.43, 0.53, 0.71, now, false },
                    new object[] { "Caterpillar Inc.", "Industrial", 67.27, 0.92, 1.39, now, false },
                    new object[] { "Citigroup, Inc.", "Financial", 49.37, 0.02, 0.04, now, false },
                    new object[] { "E.I. du Pont de Nemours and Company", "Basic Materials", 40.48, 0.51, 1.28, now, false },
                    new object[] { "Exxon Mobil Corp", "Energy", 68.1, -0.43, -0.64, now, true },
                    new object[] { "General Electric Company", "Industrial", 34.14, -0.08, -0.23, now, true },
                    new object[] { "General Motors Corporation", "Consumer", 30.27, 1.09, 3.74, now, true },
                    new object[] { "Hewlett-Packard Co.", "Technology", 36.53, -0.03, -0.08, now, true },
                    new object[] { "Honeywell Intl Inc", "Industrial", 38.77, 0.05, 0.13, now, true },
                    new object[] { "Intel Corporation", "Technology", 19.88, 0.31, 1.58, now, false },
                    new object[] { "International Business Machines", "Technology", 81.41, 0.44, 0.54, now, false },
                    new object[] { "Johnson & Johnson", "Consumer", 64.72, 0.06, 0.09, now, false },
                    new object[] { "JP Morgan & Chase & Co", "Financial", 45.73, 0.07, 0.15, now, false },
                    new object[] { "McDonald\"s Corporation", "Consumer", 36.76, 0.86, 2.40, now, false },
                    new object[] { "Merck & Co., Inc.", "Consumer", 40.96, 0.41, 1.01, now, true },
                    new object[] { "Microsoft Corporation", "Technology", 25.84, 0.14, 0.54, now, false },
                    new object[] { "Pfizer Inc", "Consumer", 27.96, 0.4, 1.45, now, false},
                    new object[] { "The Coca-Cola Company", "Consumer", 45.07, 0.26, 0.58, now, true },
                    new object[] { "The Home Depot, Inc.", "Consumer", 34.64, 0.35, 1.02, now, true },
                    new object[] { "The Procter & Gamble Company", "Consumer", 61.91, 0.01, 0.02, now, true },
                    new object[] { "United Technologies Corporation", "Industrial", 63.26, 0.55, 0.88, now, true },
                    new object[] { "Verizon Communications", "Communications", 35.57, 0.39, 1.11, now, true },
                    new object[] { "Wal-Mart Stores, Inc.", "Consumer", 45.45, 0.73, 1.63, now, true }
                };
    
                foreach (object[] row in dataArray)
                {
                    dataTable.LoadDataRow(row, true);
                }
    
                return dataTable;
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                var mainDataSource = this.TestData;
                this.Store1.DataSource = mainDataSource;
                this.Store1.DataBind();
    
                mainDataSource.DefaultView.Sort = "industry ASC";
                var industryTable = mainDataSource.DefaultView.ToTable(true, "industry");
                StoreIndustry.DataSource = industryTable;
                StoreIndustry.DataBind();
            }
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title>Local Data Paging - Ext.NET Examples</title>
        <script type="text/javascript">
            var showTotalRecords = function (caller) {
                var store = App.StoreIndustry;
                var totalCount = store.getTotalCount();
    
                if(totalCount==0)
                    Ext.Msg.alert(Ext.String.format("Called by {0}", caller), Ext.String.format("Total count is {0}", totalCount));
            };
    
            var showTotalRecordsX = function (caller) {
                var store = App.StoreIndustry;
                var totalCount = store.getTotalCount();
    
                Ext.Msg.alert(Ext.String.format("Called by {0}", caller), Ext.String.format("Total count is {0}", totalCount));
            };
        </script>
    </head>
    <body>
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <ext:Store ID="StoreIndustry" runat="server" AutoLoad="true">
            <Model>
                <ext:Model runat="server" IDProperty="industry">
                    <Fields>
                        <ext:ModelField Name="industry" />
                    </Fields>
                </ext:Model>
            </Model>
        </ext:Store>
        <ext:Viewport runat="server" Layout="FitLayout">
            <Items>
                <ext:GridPanel ID="GridPanel1" runat="server" Flex="1" SelectionMemory="false">
                    <Store>
                        <ext:Store ID="Store1" runat="server" RemoteSort="false" AutoLoad="true" PageSize="10">
                            <Model>
                                <ext:Model ID="Model1" runat="server" IDProperty="company">
                                    <Fields>
                                        <ext:ModelField Name="company" />
                                        <ext:ModelField Name="industry" />
                                        <ext:ModelField Name="price" Type="Float" />
                                        <ext:ModelField Name="change" Type="Float" />
                                        <ext:ModelField Name="pctChange" Type="Float" />
                                        <ext:ModelField Name="lastChange" Type="Date" />
                                        <ext:ModelField Name="visible" Type="Boolean" />
                                    </Fields>
                                </ext:Model>
                            </Model>
                        </ext:Store>
                    </Store>
                    <ColumnModel ID="ColumnModel1" runat="server">
                        <Columns>
                            <ext:Column ID="Column1" runat="server" Text="Company" DataIndex="company" Width="200">
                                <Editor>
                                    <ext:TextField ID="TextField1" runat="server" />
                                </Editor>
                            </ext:Column>
                            <ext:Column ID="Column2" runat="server" Text="Industry" DataIndex="industry" Width="200">
                            </ext:Column>
                            <ext:Column ID="Column3" runat="server" Text="Price" Width="75" DataIndex="price">
                                <Editor>
                                    <ext:TextField ID="TextField2" runat="server" />
                                </Editor>
                            </ext:Column>
                            <ext:Column ID="Column4" runat="server" Text="Change" Width="100" DataIndex="change" />
                            <ext:Column ID="Column5" runat="server" Text="% Change" Width="100" DataIndex="pctChange" />
                            <ext:DateColumn ID="DateColumn1" runat="server" Text="Last Updated" Width="120" DataIndex="lastChange"
                                Format="HH:mm:ss" />
                            <ext:Column ID="Column6" runat="server" Text="Visible" DataIndex="visible" Align="Center">
                                <Renderer Handler="return (value) ? 'Yes':'No';" />
                            </ext:Column>
                        </Columns>
                    </ColumnModel>
                    <SelectionModel>
                        <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" Mode="Single" />
                    </SelectionModel>
                    <Features>
                        <ext:GridFilters runat="server" Local="true">
                            <Filters>
                                <ext:ListFilter DataIndex="industry" LabelField="industry" IDField="industry" StoreID="StoreIndustry">
                                    <Listeners>
                                        <Deactivate Handler="Ext.Msg.alert('Prompt', 'De-Activated!');" />
                                        <Activate Handler="Ext.Msg.alert('Prompt', 'Activated!');" />
                                        <Update Handler="Ext.Msg.alert('Prompt', 'Updated!');" />
                                    </Listeners>
                                </ext:ListFilter>
                                <ext:DateFilter DataIndex="lastChange">
                                    <DatePickerOptions runat="server" TodayText="Now" />
                                </ext:DateFilter>
                                <ext:BooleanFilter DataIndex="visible">
                                </ext:BooleanFilter>
                            </Filters>
                        </ext:GridFilters>
                    </Features>
                    <BottomBar>
                        <ext:PagingToolbar ID="PagingToolbar1" runat="server">
                            <Items>
                                <ext:Button runat="server" Text="Show Total Records">
                                    <Listeners>
                                        <Click Handler="showTotalRecordsX('Click');">
                                        </Click>
                                    </Listeners>
                                </ext:Button>
                            </Items>
                        </ext:PagingToolbar>
                    </BottomBar>
                    <Listeners>
                        <ViewReady Handler="showTotalRecords('ViewReady');" />
                        <Render Handler="showTotalRecords('Render');" />
                    </Listeners>
                    <View>
                        <ext:GridView ID="GridView1" runat="server">
                            <Listeners>
                                <BeforeRefresh Handler="showTotalRecords('BeforeRefresh');" />
                            </Listeners>
                        </ext:GridView>
                    </View>
                </ext:GridPanel>
            </Items>
        </ext:Viewport>
        </form>
    </body>
    </html>
    Last edited by Daniil; Aug 16, 2013 at 1:33 PM. Reason: [CLOSED]
  2. #2
    Hello!

    You should use either store's Load event or Grid's Refresh event:
    http://docs.sencha.com/extjs/4.2.1/#...ore-event-load
    http://docs.sencha.com/extjs/4.2.1/#...-event-refresh

    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <%@ Import Namespace="System.Data" %>
    <script runat="server">
        private DataTable TestData
        {
            get
            {
                var now = DateTime.Now;
                var dataTable = new DataTable();
                dataTable.Columns.AddRange(new DataColumn[] { 
                    new DataColumn { ColumnName = "company", DataType = typeof(System.String) },
                    new DataColumn { ColumnName = "industry", DataType = typeof(System.String) },
                    new DataColumn { ColumnName = "price", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "change", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "pctChange", DataType = typeof(System.Double) },
                    new DataColumn { ColumnName = "lastChange", DataType = typeof(System.DateTime) },
                    new DataColumn { ColumnName = "visible", DataType = typeof(System.Boolean) },
                });
     
                var dataArray = new object[]
                {
                    new object[] { "3m Co", "Conglomerates", 71.72, 0.02, 0.03, now, true },
                    new object[] { "Alcoa Inc", "Basic Materials", 29.01, 0.42, 1.47, now, true },
                    new object[] { "Altria Group Inc", "Consumer Goods",  83.81, 0.28, 0.34, now, true },
                    new object[] { "American Express Company", "Financial", 52.55, 0.01, 0.02, now, true },
                    new object[] { "American International Group, Inc.", "Financial", 64.13, 0.31, 0.49, now, false },
                    new object[] { "AT&T Inc.", "Technology", 31.61, -0.48, -1.54, now, false },
                    new object[] { "Boeing Co.", "Industrial", 75.43, 0.53, 0.71, now, false },
                    new object[] { "Caterpillar Inc.", "Industrial", 67.27, 0.92, 1.39, now, false },
                    new object[] { "Citigroup, Inc.", "Financial", 49.37, 0.02, 0.04, now, false },
                    new object[] { "E.I. du Pont de Nemours and Company", "Basic Materials", 40.48, 0.51, 1.28, now, false },
                    new object[] { "Exxon Mobil Corp", "Energy", 68.1, -0.43, -0.64, now, true },
                    new object[] { "General Electric Company", "Industrial", 34.14, -0.08, -0.23, now, true },
                    new object[] { "General Motors Corporation", "Consumer", 30.27, 1.09, 3.74, now, true },
                    new object[] { "Hewlett-Packard Co.", "Technology", 36.53, -0.03, -0.08, now, true },
                    new object[] { "Honeywell Intl Inc", "Industrial", 38.77, 0.05, 0.13, now, true },
                    new object[] { "Intel Corporation", "Technology", 19.88, 0.31, 1.58, now, false },
                    new object[] { "International Business Machines", "Technology", 81.41, 0.44, 0.54, now, false },
                    new object[] { "Johnson & Johnson", "Consumer", 64.72, 0.06, 0.09, now, false },
                    new object[] { "JP Morgan & Chase & Co", "Financial", 45.73, 0.07, 0.15, now, false },
                    new object[] { "McDonald\"s Corporation", "Consumer", 36.76, 0.86, 2.40, now, false },
                    new object[] { "Merck & Co., Inc.", "Consumer", 40.96, 0.41, 1.01, now, true },
                    new object[] { "Microsoft Corporation", "Technology", 25.84, 0.14, 0.54, now, false },
                    new object[] { "Pfizer Inc", "Consumer", 27.96, 0.4, 1.45, now, false},
                    new object[] { "The Coca-Cola Company", "Consumer", 45.07, 0.26, 0.58, now, true },
                    new object[] { "The Home Depot, Inc.", "Consumer", 34.64, 0.35, 1.02, now, true },
                    new object[] { "The Procter & Gamble Company", "Consumer", 61.91, 0.01, 0.02, now, true },
                    new object[] { "United Technologies Corporation", "Industrial", 63.26, 0.55, 0.88, now, true },
                    new object[] { "Verizon Communications", "Communications", 35.57, 0.39, 1.11, now, true },
                    new object[] { "Wal-Mart Stores, Inc.", "Consumer", 45.45, 0.73, 1.63, now, true }
                };
     
                foreach (object[] row in dataArray)
                {
                    dataTable.LoadDataRow(row, true);
                }
     
                return dataTable;
            }
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                var mainDataSource = this.TestData;
                this.Store1.DataSource = mainDataSource;
                this.Store1.DataBind();
     
                mainDataSource.DefaultView.Sort = "industry ASC";
                var industryTable = mainDataSource.DefaultView.ToTable(true, "industry");
                StoreIndustry.DataSource = industryTable;
                StoreIndustry.DataBind();
            }
        }
    </script>
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title>Local Data Paging - Ext.NET Examples</title>
        <script type="text/javascript">
            var showTotalRecords = function (caller) {
                var store = App.StoreIndustry;
                var totalCount = store.getTotalCount();
    
                if (totalCount == 0)
                    Ext.Msg.alert(Ext.String.format("Called by {0}", caller), Ext.String.format("Total count is {0}", totalCount));
            };
    
            var showTotalRecordsX = function (caller) {
                var store = App.StoreIndustry;
                var totalCount = store.getTotalCount();
    
                Ext.Msg.alert(Ext.String.format("Called by {0}", caller), Ext.String.format("Total count is {0}", totalCount));
            };
        </script>
    </head>
    <body>
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <ext:Store ID="StoreIndustry" runat="server" AutoLoad="true">
            <Model>
                <ext:Model runat="server" IDProperty="industry">
                    <Fields>
                        <ext:ModelField Name="industry" />
                    </Fields>
                </ext:Model>
            </Model>
            <Listeners>
                <Load Handler="showTotalRecordsX('Load');"></Load>
            </Listeners>
        </ext:Store>
        <ext:Viewport runat="server" Layout="FitLayout">
            <Items>
                <ext:GridPanel ID="GridPanel1" runat="server" Flex="1" SelectionMemory="false">
                    <Store>
                        <ext:Store ID="Store1" runat="server" RemoteSort="false" AutoLoad="true" PageSize="10">
                            <Model>
                                <ext:Model ID="Model1" runat="server" IDProperty="company">
                                    <Fields>
                                        <ext:ModelField Name="company" />
                                        <ext:ModelField Name="industry" />
                                        <ext:ModelField Name="price" Type="Float" />
                                        <ext:ModelField Name="change" Type="Float" />
                                        <ext:ModelField Name="pctChange" Type="Float" />
                                        <ext:ModelField Name="lastChange" Type="Date" />
                                        <ext:ModelField Name="visible" Type="Boolean" />
                                    </Fields>
                                </ext:Model>
                            </Model>
                        </ext:Store>
                    </Store>
                    <ColumnModel ID="ColumnModel1" runat="server">
                        <Columns>
                            <ext:Column ID="Column1" runat="server" Text="Company" DataIndex="company" Width="200">
                                <Editor>
                                    <ext:TextField ID="TextField1" runat="server" />
                                </Editor>
                            </ext:Column>
                            <ext:Column ID="Column2" runat="server" Text="Industry" DataIndex="industry" Width="200">
                            </ext:Column>
                            <ext:Column ID="Column3" runat="server" Text="Price" Width="75" DataIndex="price">
                                <Editor>
                                    <ext:TextField ID="TextField2" runat="server" />
                                </Editor>
                            </ext:Column>
                            <ext:Column ID="Column4" runat="server" Text="Change" Width="100" DataIndex="change" />
                            <ext:Column ID="Column5" runat="server" Text="% Change" Width="100" DataIndex="pctChange" />
                            <ext:DateColumn ID="DateColumn1" runat="server" Text="Last Updated" Width="120" DataIndex="lastChange"
                                Format="HH:mm:ss" />
                            <ext:Column ID="Column6" runat="server" Text="Visible" DataIndex="visible" Align="Center">
                                <Renderer Handler="return (value) ? 'Yes':'No';" />
                            </ext:Column>
                        </Columns>
                    </ColumnModel>
                    <SelectionModel>
                        <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" Mode="Single" />
                    </SelectionModel>
                    <Features>
                        <ext:GridFilters runat="server" Local="true">
                            <Filters>
                                <ext:ListFilter DataIndex="industry" LabelField="industry" IDField="industry" StoreID="StoreIndustry">
                                    <Listeners>
                                        <Deactivate Handler="Ext.Msg.alert('Prompt', 'De-Activated!');" />
                                        <Activate Handler="Ext.Msg.alert('Prompt', 'Activated!');" />
                                        <Update Handler="Ext.Msg.alert('Prompt', 'Updated!');" />
                                    </Listeners>
                                </ext:ListFilter>
                                <ext:DateFilter DataIndex="lastChange">
                                    <DatePickerOptions runat="server" TodayText="Now" />
                                </ext:DateFilter>
                                <ext:BooleanFilter DataIndex="visible">
                                </ext:BooleanFilter>
                            </Filters>
                        </ext:GridFilters>
                    </Features>
                    <BottomBar>
                        <ext:PagingToolbar ID="PagingToolbar1" runat="server">
                            <Items>
                                <ext:Button runat="server" Text="Show Total Records">
                                    <Listeners>
                                    </Listeners>
                                </ext:Button>
                            </Items>
                        </ext:PagingToolbar>
                    </BottomBar>
                    <Listeners>
                    </Listeners>
                    <View>
                        <ext:GridView ID="GridView1" runat="server">
                            <Listeners>
                                <Refresh Handler="showTotalRecordsX('Refresh');"></Refresh>
                            </Listeners>
                        </ext:GridView>
                    </View>
                </ext:GridPanel>
            </Items>
        </ext:Viewport>
        </form>
    </body>
    </html>
    Last edited by Baidaly; Aug 14, 2013 at 11:55 PM.
  3. #3
    Does it mean that ViewReady and Render events are no longer reliable to examine the range of the grid store?
  4. #4
    These events relate to Grid rendering events and cannot be used for this purpose.
  5. #5
    Thanks Daulet! Is there any guarantee that the GridPanel Render event always fires prior to its Store's Load event?
  6. #6
    There is no guarantee with a Store's AutoLoad="true' by default.

    You can set up AutoLoad="false" and call
    store.load();
    within a GridPanel's Render event. This way there will be the guarantee:)
  7. #7
    I suppose even BeforeRender isn't guaranteed to fire prior to Store's Load event?
  8. #8
    With default AutoLoad="true" still? Well, it depends.

    If you put a Store into a GridPanel's Store collection, that Store should be a lazy component. I.e. a GridPanel creates that Store itself on some stage. Probably, it happens after a GridPanel's BeforeRender event, but I am not 100% sure. As you can understand, a Store cannot load itself before creating.

    Though, it is a different thing if you associate a Store with a GridPanel's StoreID. In this case, there is no guarantee.
  9. #9
    That reasoning makes sense to me. Thanks for further clarification Daniil and please mark this question as resolved.

Similar Threads

  1. [CLOSED] Bind store data in direct method
    By bayoglu in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 14, 2013, 6:13 PM
  2. Replies: 2
    Last Post: Mar 21, 2012, 11:06 AM
  3. Replies: 0
    Last Post: May 12, 2011, 5:37 PM
  4. [CLOSED] HttpProxy sample with Method='GET' to load data for store
    By inayath in forum 1.x Legacy Premium Help
    Replies: 10
    Last Post: Feb 08, 2011, 2:18 PM
  5. [CLOSED] HttpProxy sample with Method='GET' to load data for store
    By inayath in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Feb 07, 2011, 11:55 AM

Tags for this Thread

Posting Permissions