We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    I want to get the total number of records in a grid after the store has already been loaded.
    The bottom paging bar displays this down the bottom right so the value must be stored somewhere.

    Within my grid top bar I've tried something like:
            ,{
                xtype: 'tbtext'
                ,text: this.getStore().getCount()
            }
    


    but firebug shows me that it's undefined.
    From scouring the ExtJS forums I'm guessing I need to use a listener to make sure the store is loaded first but I can't work out how to do this with MODX.grid
    Any hints would be much appreciated smiley

    This question has been answered by muzzstick. See the first response.

      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
    • discuss.answer
      • 44195
      • 293 Posts
      Ah I forgot to post the solution to this.

      I did indeed need to use a listener to wait for the store to be initially loaded as the rendering will be faster.

      ,{
      xtype: 'tbtext'
      ,id: 'recNum'
      ,text: 'Loading...'
      }


      The text will initially show "Loading..." when rendered and this will be updated when the store loads and we can get access to the data.

      ,listeners: {
          // This listener is added to the grid itself so "this" represents "grid" and the function will run when the grid is rendered.
          render: function() {
              // Check for any existing records (this is not a good tactic if it's possible your grid won't have any records)
              if (this.store.getCount() == 0) {
                  console.log('store not loaded yet');
                  // The load listener is applied to this grid's store.
                  this.store.on('load', function() {
                      console.log('load after render');
                      // We can now get the data as it's been loaded and count the number of records. We use the update function to replace the "Loading..." text.
                      Ext.getCmp('recNum').update(this.store.getCount());
                  }, this, {
                      single: true
                  });
              } else {
                  console.log('store already loaded');
              }
          }
      }
      


        I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
        Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com