extjs 4.1.1 - 本地数据网格中的分页

标签 extjs extjs4

我对 extjs 比较陌生。我正面临一个我无法解决的问题。我一直在网上搜索,但无法找到答案。感谢任何帮助。

在我的代码中,我试图为网格上显示的数据添加分页。该代码向服务器发出ajax 调用以获取数据。服务器发送的数据比需要的多。因此客户端提取响应文本 (json) 的部分,并将其显示在网格面板中。

网格显示数据很好。但是分页不起作用。它始终显示“第 0 页,共 0 页”和“无数据可显示”。

但是如果我用硬编码数据替换数据,并用硬编码数据创建存储和网格,分页就可以工作了。所以看起来我必须在创建商店和网格时已经有了数据。

这是简化的代码。使用 myStore1/myData1(硬编码数据)时,分页有效。将 myStore1 和 myData1 替换为从服务器检索数据后加载的 myStore2 和 myData2,分页不起作用。

我在网上看到的所有示例都涉及硬编码数据......感谢任何见解或指针。

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}});   
 };

};

最佳答案

要将数据加载到内存代理上,您必须将数据分配给代理,然后调用 store.load();https://fiddle.sencha.com/#fiddle/8ia

我的灵感来自 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();
}

关于extjs 4.1.1 - 本地数据网格中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25166052/

相关文章:

javascript - ExtJS 4 更改 pagingtoolbar 中的自定义按钮位置

extjs - 为什么 Ext.form.field.disable 不自动重新验证

javascript - 如何在弹出消息框的按钮上设置样式

javascript - 在选项卡上方显示 tbar

extjs - 工具栏中字段的宽度为 100%

events - ExtJS 4 - 组合框模糊事件被触发两次

无限滚动网格面板的 ExtJs 刷新

javascript - 编辑器网格中的自定义类型存储字段未正确映射

extjs4 - 将商店合并到另一个

extjs - 如何在 Extjs 4 中设置网格行高?