Extn JS 4.2.0网格分页不工作

时间:2015-03-04 11:50:59

标签: javascript extjs pagination

这是我的app.js代码

Ext.require([
    'Ext.data.*',
    'Ext.grid.*'
]);

var TOTAL = 94; //random

var fetchedData = function(){
    this.data = null;
    this.total = 0;
}




// code to create grid

Ext.onReady(function(){
    Ext.define('Person',{
        extend: 'Ext.data.Model',
        fields: [
            { name: 'fname', type: 'string' },
            { name: 'lname', type: 'string' },
            { name: 'email', type: 'string' },
            { name: 'uid', type: 'string' },
            { name: 'isSup', type: 'boolean' },
            { name: 'upDate', type: 'string' },
            { name: 'upBy', type: 'string' }
        ]
    });

    // create the Data Store
    var store = Ext.create('Ext.data.Store', {
        model: 'Person',
        pageSize : 2,
        proxy: {
            type: 'ajax',
            url : 'supUserStore.json',
            reader: {type: 'json', root : 'data', totalProperty : 'total'}
        },
        sorters: [{
            property : 'Id',
            direction:'ASC'
        }],

    });
store.loadPage(1);
    // create the grid
    Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [
              { header: 'First Name',dataIndex: 'fname' },
              { header: 'Last Name', dataIndex: 'lname' },
              { header: 'Email', dataIndex: 'email'},
              { header: 'User ID', dataIndex: 'uid' },
              { header: 'Super Admin', dataIndex: 'isSup'},
              { header: 'Updated Date',dataIndex: 'upDate'},
              { header: 'Updated By',dataIndex: 'upBy'}
             ],
        // paging bar on the bottom
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '{0} - {1} of {2}',
            emptyMsg: "No topics to display"
        }),
        renderTo:'grid-example',
        width: 350,
        height: 280,
    });

    // trigger the data store load
    store.loadPage(1);
});

我的json数据是一个json文件:

{
    "success": true,
    "total": 11,
    "data":  [
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com"}
            ] 
}

我能够使用数据加载网格,但我无法进行分页,所有记录都在一个页面中加载。请帮助我,告诉我我做错了什么。

1 个答案:

答案 0 :(得分:1)

此代码对我来说运行正常,我删除了对loadPage的top调用,并且还删除了fetchedData函数,因为它没有被使用。

我没有改变任何其他事情。只是将代码包装在启动函数中,而不是在Ext.ready中为小提琴包装。

fiddle

// data1.json
{
    "success": true,
    "total": 11,
    "data":  [
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jane","lname":"Smith","email": "j.smith@netapp.com", "uid": "jsmith","isSup":false,"upDate":"11-19-2012","upBy":"aaron@netapp.com" },
                { "fname": "Jim","lname":"Smith","email": "jm.smith@netapp.com", "uid": "jmsmith","isSup":true,"upDate":"11-23-2012","upBy":"aaron@netapp.com"}
            ] 
}

// app.js
Ext.application({
    name: 'Fiddle',

    launch: function() {

        Ext.define('Person', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'fname',
                type: 'string'
            }, {
                name: 'lname',
                type: 'string'
            }, {
                name: 'email',
                type: 'string'
            }, {
                name: 'uid',
                type: 'string'
            }, {
                name: 'isSup',
                type: 'boolean'
            }, {
                name: 'upDate',
                type: 'string'
            }, {
                name: 'upBy',
                type: 'string'
            }]
        });

        // create the Data Store
        var store = Ext.create('Ext.data.Store', {
            model: 'Person',
            pageSize: 2,
            proxy: {
                type: 'ajax',
                url: 'data1.json',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'total'
                }
            },
            sorters: [{
                property: 'Id',
                direction: 'ASC'
            }],

        });

        // create the grid
        Ext.create('Ext.grid.Panel', {
            store: store,
            columns: [{
                header: 'First Name',
                dataIndex: 'fname'
            }, {
                header: 'Last Name',
                dataIndex: 'lname'
            }, {
                header: 'Email',
                dataIndex: 'email'
            }, {
                header: 'User ID',
                dataIndex: 'uid'
            }, {
                header: 'Super Admin',
                dataIndex: 'isSup'
            }, {
                header: 'Updated Date',
                dataIndex: 'upDate'
            }, {
                header: 'Updated By',
                dataIndex: 'upBy'
            }],
            // paging bar on the bottom
            bbar: Ext.create('Ext.PagingToolbar', {
                store: store,
                displayInfo: true,
                displayMsg: '{0} - {1} of {2}',
                emptyMsg: "No topics to display"
            }),
            renderTo: Ext.getBody(),
            width: 350,
            height: 280,
        });

        // trigger the data store load
        store.loadPage(1);
    }
});