4

I am relatively new to extjs. I am facing a problem that I can not resolve. I have been searching online but unable to find the answer. Appreciate any help.

In my code, I am trying to add pagination to the data displayed on the grid. The code makes an ajax call to the server to get the data. The server sends more data than needed. So the client extracts the portion of the response text (json), and displays it in a grid panel.

The grid displays the data fine. But the pagination is not working. It always shows "page 0 of 0" and "No data to display".

But if I substitute the data with hardcoded data, and create the store and grid with the hardcoded data, the pagination works. So it seems like I must already have the data when the store and the grid are being created.

Here's the simplified code. When using myStore1/myData1 (hardcoded data), paging works. Replacing myStore1 and myData1 with myStore2 and myData2, which is loaded after data is retrieved from the server, paging does not work.

All the examples I see online deals with hardcoded data...Appreciate for any insight or pointer.

Ext.Loader.setConfig({
     enabled: true
    });

Ext.Loader.setPath('Ext.ux', 'ext-4.1.1/examples/ux');
Ext.require('Ext.ux.data.PagingMemoryProxy');

Ext.application({
    name: 'MS GUI',
    launch: function() {
    main();
    }
});

function main() {

  itemsPerPage = 2;

  // hardcoded data
  myData1 =  [{'xxx':'a1','yyy':'b1','zzz':'c1'},
  {'xxx':'a2','yyy':'b2','zzz':'c2'},
  {'xxx':'a3','yyy':'b3','zzz':'c3'},
  {'xxx':'a4','yyy':'b4','zzz':'c4'}];

  // data to be loaded after the ajax call
  myData2 = [];



  // define model
  Ext.define('QueryResultModel', {
  extend: 'Ext.data.Model',
  fields: [
    {name: 'xxx', type: 'string'},
    {name: 'yyy', type: 'string'},
    {name: 'zzz', type: 'string'}
  });

  // define store1
  myStore1 = Ext.create('Ext.data.Store', {
  model: 'QueryResultModel',
  storeId: 'QueryResultStore1',
  autoLoad: false,
  pageSize: itemsPerPage, 
  data: myData,    // *** use hardcoded data here, and paging works ***
  proxy : {
    type: 'pagingmemory'
    }
  });


  // define store2
  myStore2 = Ext.create('Ext.data.Store', {
  model: 'QueryResultMode',
  storeId: 'QueryResultStore2',
  autoLoad: false,
  pageSize: itemsPerPage, 
  data: {},         // *** empty data here, and once data loaded, paging does not work ****
  proxy : {
    type: 'pagingmemory'
    }
  });


function createGrid(){
   return Ext.create('Ext.grid.Panel', {
         title: 'Query Result',
         store: myStore1, // *** hardcoded data
         columns: [
            {text: 'XXX' dataIndex: 'xxx'},
            {text: 'YYY' dataIndex: 'yyy'},
            {text: 'ZZZ' dataIndex: 'zzz'}  
         ],
         dockedItem : [{
            xtype: 'pagingtoolbar',
            id: 'pagingBar_id',
            store : myStore1,  // *** use hardcoded data - paging works
            displayInfo: true,
            displayMsg: 'Displaying records {0} - {1} of {2}',
            displayMsg: "No data to display"
         }]

  });
 }; 


myContainer = Ext.create('Ext.container.Viewport', {
   id: 'mainPanel_id',
   layout: { type: 'border', padding: 5},
   renderTo: Ext.getBody(),
   items: [{
      region: 'north',
      title: 'Whatever Title Here',
      collapsible: true,
      autoHeight: true,
      // *** other congig params here       
       }, 
       {
          region: 'west',
          title: 'Query Input',   
          // other config params here

          buttons : [
            {
               text : 'Submit',
               id: 'submit_btn',
               listeners : { click: function(){ sendQuery();}
            },
            {
               text : 'Reset',
               // other config parems here
            }
          ]
       },
       {
          region: 'center',
          xtype:  'tabpanel',
          autoScroll: true,
          layout: 'fit',
          items : [ createGrid()]

       },
       // other config for other regions here
   ]
 }); 


 function sendQuery() {
   // code here to make the ajax call to the server and
   // retrive the data, if success
   // load response date into myData2
   // note: the grid and pagingtoolbar are pointing to myStore2 (by replacing the myStore1)
   myStore2.loadData(myData2, false);

   // after the sendQuery() call, the paging tool bar still shows no data... while the grid is populated with 

   // none of the followings works:
   // Ext.getCmp('pagingBar_id').doRefresh();
   // Ext.getCmp('pagingBar_id').getStore().load();
   // myStore2.load({params: {start:0}});   
 };

};

ted
  • 43
  • 1
  • 4
  • In your example, you're never passing `myStore2` into any grid. If you can reproduce your problem at https://fiddle.sencha.com/#home I can look at it for you. Also this code is not syntactically correct, meaning that I can't paste it and try to run it. – Ruan Mendes Aug 06 '14 at 17:13
  • `myData1` is also invalid JavaScript. Please be sure your code is syntactically valid before posting a question – Ruan Mendes Aug 06 '14 at 17:19
  • I've fixed your code a little bit here https://fiddle.sencha.com/#fiddle/8ia but it still needs to be fixed more before it can be examined – Ruan Mendes Aug 06 '14 at 17:26
  • In order to run the second scenario (i.e. using real data from the server), I have to replace myStore1 by myStore2 in the grid and in the dockedItem (i.e. code in the createGrid() method). Thanks for the offer. The code is in a classified system, and I can not access it. I will have to work out a simplified version on my unclass computer. It will take a while... – ted Aug 06 '14 at 17:28
  • Thanks for pointing out that myData1 is of invalid format. It was a typo and then magnified by copy and paste. It has been corrected. – ted Aug 06 '14 at 17:40
  • Did you take a look at the link I sent? https://fiddle.sencha.com/#fiddle/8ia It's almost working. You have to create your own Sencha accoutn to be able to save your own versions – Ruan Mendes Aug 06 '14 at 17:40
  • Yes, I have taken a look at the link. You replaced myStore1 with myStore2 in the grid and in the dockedItems config. And in the sendQuery() method, you are loading the hardcoded myData1 to myStore2. And it behaves exactly as I explained. Once you click the "Submit" button, you see the hard-coded data displayed in the grid. But pagination does not work. It suppose to show two rows per page. I believe I do have a sencha account. Will check wether it still works. – ted Aug 06 '14 at 18:03

1 Answers1

8

To load data onto a memory proxy, you have to assign the data to the proxy, then call store.load(); See https://fiddle.sencha.com/#fiddle/8ia

I got inspiration from http://www.sencha.com/forum/showthread.php?267955-Load-data-into-Memory-Proxy-Store

var myStoreNoData = Ext.create('Ext.data.Store', {
    model: 'QueryResultModel',
    autoLoad: false,
    pageSize: itemsPerPage,
    proxy: {
        type: 'pagingmemory'
    }
});


function sendQuery() {
    myStoreNoData.proxy.data = myData;
    myStoreNoData.load();
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • @ted Please mark this answer as accepted by clicking the tick mark. Not only does this give me a little rep, but it indicates to others that this has been satisfactorily answered. – Ruan Mendes Aug 06 '14 at 20:02