简体   繁体   English

Ext js网格分页

[英]Ext js Grid Pagination

I am new to Ext JS and I have tried the Example from the Ext JS docs, but I am unable to get pagination. 我是Ext JS的新手,并且已经尝试了Ext JS文档中的示例,但无法分页。

I have designed my application using MVC architecture. 我已经使用MVC架构设计了应用程序。

Here is my Code: 这是我的代码:

title : 'Trade Data',
store : 'RamDataStore',
id:'tradedatagrid',
dockedItems:[{
    xtype:'pagingtoolbar',
    store:'TradeDataStore',
    displayInfo:true,
    id:'tradepage',
     itemId:'tradepage',
     displayMsg:'{0}-{1} of {2}',
     emptyMsg:'no topics to show',
     dock:'bottom'}
     ],
     columns : [
      {
           xtype : 'gridcolumn',
           width : 85,align : 'center',
           dataIndex : 'tradeid',
           text : 'TradeId'
      },
      {
           xtype : 'gridcolumn',
           width : 120,
           dataIndex : 'instrumentType',
           text : 'InstrumentType'
      },
      {
           xtype : 'gridcolumn',
           width : 103, align : 'center',
           dataIndex : 'tradeBook',
           text : 'TradingBook'
      },
      {
           xtype : 'gridcolumn',
           width : 120, align : 'center',
           dataIndex : 'otherBook',
           text : 'CustomerBook'
      },
 ]

Here my paging tool bar store and my grid store are the same. 在这里,我的分页工具栏存储区和网格存储区是相同的。

Store: 商店:

I defined my store with some default properties and I created an instance for the same store in the controller to dynamically bind. 我使用一些默认属性定义了商店,并在控制器中为同一商店创建了一个实例以进行动态绑定。

Ext.define('Myapp.store.RamDataStore', {
    extend: 'Ext.data.Store',
    requires: ['MyApp.model.ram.RamDataModel'],
    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            storeId: 'tradedata',
            autoLoad: false,
            pageSize: 4,
            model: 'MyApp.model.ram.RamDataModel',

            proxy:{
                 writer:{
                      type:'json'
                 },
                 reader:{
                      type:'json'
                 },
                  enablePaging: true
            },

            sorters: [{
                property: 'tradeid',
                direction: 'ASC'
            }]

        }, cfg)]);
    }
});

Model: 模型:

Ext.define('MyApp.model.ram.RamDataModel', {
     extend : 'Ext.data.Model',
     fields : [{
                    name:'tradeid',
                    type:'int'
               }, {
                    name : 'tradeBook',
                    type : 'string'
               }, {
                    name : 'otherBook',
                    type : 'string'
               }, {
                    name : 'tradeDate',
                    type : 'auto'
               }, {
                    name : 'tradedDate',
                    type : 'auto'
               }});

Controller: 控制器:

I wrote a function that will call on button clicks, and I got a JSON result from the server: 我编写了一个函数,该函数将调用按钮单击,并且从服务器获得了JSON结果:

 data = [{"tradeid":1,"tradingbook":"ram"},{"tradeid:2,"tradingbook":"testbook"}] //(etc)

Here is my controller code: 这是我的控制器代码:

 var datastore = Ext.create('MyApp.store.RamDataStore',{
      model:'Myapp.model.ram.RamDataModel',
      data:Ext.decode(result,true),
      pageSize:4,
      start:0,
      limit:4,
      enablePaging : true,
      proxy:{
           type:'memory',
           reader:{type:'json'},
           writer:{type:'json'},
      },
      listeners:{
      beforeload:function(store,operation,eOpts){
           store.proxy.data.total=Ext.decode(result,true).length;
           //store.proxy.data=Ext.decode(result,true);
      }
 },
 });

 Ext.getCmp('tradedatagrid').getDockedComponent('tradepage').bind(datastore);
 Ext.getCmp('tradedatagrid').getView().bindStore(datastore);
 Ext.getCmp('tradedatagrid').getView().loadMask.hide();

 }
 });

With this code, I can add data to my grid, but can't add store to my paging tool bar. 使用此代码,我可以将数据添加到网格中,但不能将存储添加到页面工具栏中。

Please help on this. 请帮忙。 If you have any examples, please suggest & I will follow. 如果您有任何示例,请提出建议,我将继续。

Thanks. 谢谢。

You specify the store for paging toolbar as string what means that Store Manager assumes the string is storeId and tries to find the instance of it. 您将分页工具栏的商店指定为字符串,这意味着商店管理器假定该字符串为storeId并尝试查找它的实例。 But it cannot because the store is probably created later. 但这不是因为商店可能是在以后创建的。 Also, the store must be same for both the grid and paging toolbar. 此外,网格和分页工具栏的存储必须相同。

You have two options: 您有两种选择:

  1. declare the store in your controller: stores:['RamDataStore'] 在您的控制器中声明商店: stores:['RamDataStore']
  2. create it manually during grid initComponent where you would also create the paging toolbar and assign the store to it. 在网格initComponent期间手动创建它,您还将在其中创建分页工具栏并将商店分配给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM