简体   繁体   English

Sencha Touch限制列表中的项目数

[英]Sencha Touch limit number of items in list

I'm building an app that should run on mobile/tablet/pc. 我正在构建应在手机/平板电脑/电脑上运行的应用程序。 And the main feature contains an enormous list of items that I'm getting from json file hosted on another server. 而且主要功能包含大量项目列表,这些项目是我从另一台服务器上托管的json文件获取的。 Currently I'm working on a PC and the list takes about a minute to populate (over 800 objects, the number grows everyday), I'm guessing that's because it takes time to generate the markup for 800 divs... 目前,我正在PC上工作,并且列表需要大约一分钟的时间来填充(超过800个对象,这个数字每天都在增加),我猜这是因为生成800格的标记需要时间...

Note: I'm working locally, when this is online it would be a nightmare, overkill.. 注意:我在本地工作,当它在线上时,将是一场噩梦,过度杀伤力。

My thoughts were to get all the data from json, but display limited amount of items in the list (let's say 30). 我的想法是从json获取所有数据,但列表中显示的项目数量有限(假设为30)。 But, to be able to search and filter all of them, and still display only 30 items MAX. 但是,为了能够搜索和过滤所有内容,并且最多仍显示30个项目。

The below code is working, without the limit option I want: 下面的代码正常工作,没有我想要的限制选项:

        //model
    Ext.define('User', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                {name: 'Name', type: 'string'},
                {name: 'Address', type: 'string'},
                {name: 'ID', type: 'int'},
                {name: 'WebUrl', type: 'string'},
                {name: 'InfoUrl', type: 'string'},
                ]
        }
    });
    //store
    store = Ext.create('Ext.data.Store', {
        model: 'User',
        sorters: 'Name',
        grouper: {
            groupFn: function(record) {
                return record.get('Name')[0];
            }
        },
        proxy: {
            type: 'ajax',
            url: 'http://localhost:8088/Services/RestaurantList.ashx',
            reader: {
                type: 'json',
                rootProperty: 'users'
            }
        },
        autoLoad: true
    });

    //the list
    list = Ext.create('Ext.List', {
        flex: 2,
        itemTpl: ['<div class="contact">{Name}</div>'],
        store: store,
        listeners: {
                        itemtap: function(list, index, target, record) {
                            mainContainer.setActiveItem(1);
                            detailsPanel.setRecord(record);

                        }
                    },
        grouped: true,
/* maxVisibleRecords: 30,
limit: 30,
params: { limit: 30 },
ExtraParams: { limit: 30} */ //none worked
    });

Thanks! 谢谢! :) :)

Just add pageSize config to the store. 只需将pageSize配置添加到商店。

For sending a 'limit' param on the url that reads the json, set the limitParam config to the proxy. 要在读取json的网址上发送“限制”参数,请将limitParam配置设置为代理。

You probably may like to use the listpaging list plugin, which adds a Load More built in functions. 您可能想使用listpaging列表插件,该插件添加了Load More内置函数。

Hope it helps- 希望能帮助到你-

EDIT: 编辑:

//store
store = Ext.create('Ext.data.Store', {
    model: 'User',
    sorters: 'Name',
    grouper: {
        groupFn: function(record) {
            return record.get('Name')[0];
        }
    },
    proxy: {
        type: 'ajax',
        url: 'http://localhost:8088/Services/RestaurantList.ashx',
        reader: {
            type: 'json',
            rootProperty: 'users'
        }
    },
    autoLoad: true,
    pageSize: 30,
});

//the list
list = Ext.create('Ext.List', {
    flex: 2,
    itemTpl: ['<div class="contact">{Name}</div>'],
    plugins: [{type: 'listpaging'}],
    store: store,
    listeners: {
        itemtap: function(list, index, target, record) {
            mainContainer.setActiveItem(1);
            detailsPanel.setRecord(record);
        }
    },
    grouped: true,
});

I found a solution myself. 我自己找到了解决方案。 I created two stores, the first one gets all the data from json, and I simply sliced that, and I set the sliced data to my second store. 我创建了两个存储,第一个存储从json获取所有数据,我只是对其进行了切片,然后将切片的数据设置为第二个存储。 The search works perfect too, I can search through all the items in the "big" store, and display them in the second, also sliced (no more than 30 items) 搜索效果也很完美,我可以搜索“大型”商店中的所有商品,并在第二个商品中也将它们切片(不超过30个商品)

here's the code: 这是代码:

        //model
    Ext.define('User', {
        extend: 'Ext.data.Model',
        config: {
            idProperty: 'Name',
            fields: [
                {name: 'Name', type: 'string'},
                {name: 'Address', type: 'string'},
                {name: 'ID', type: 'int'},
                {name: 'WebUrl', type: 'string'},
                {name: 'InfoUrl', type: 'string'},
                {name: 'Answered', type: 'boolean'},
                ]
        }
    });


    //store

    aStore = Ext.create('Ext.data.Store', {
        model: 'User',
        sorters: 'Name',
        grouper: {
            groupFn: function(record) {
                return record.get('Name')[0];
            }
        }
    });

    //full store

    store = Ext.create('Ext.data.Store', {
        model: 'User',
        sorters: 'Name',
        grouper: {
            groupFn: function(record) {
                return record.get('Name')[0];
            }
        },
        proxy: {
            type: 'ajax',
            url: '/Services/RestaurantList.ashx',
            reader: {
                type: 'json',
                rootProperty: 'users'
            }
        },
        listeners:{
            load: function(){
                var all = store.data.all;
                aStore.setData(all.slice(0,30));
            }
        },
        autoLoad: true
    });


    //the list
    list = Ext.create('Ext.List', {
        flex: 8,
        itemTpl: ['<div class="contact">{Name}</div>'],
        store: aStore,
        listeners: {
            itemtap: function(list, index, target, record) {
                mainContainer.setActiveItem(1);
                detailsPanel.setRecord(record);

            }
        },
        plugins: [
            {
                xclass: 'Ext.plugin.PullRefreshFn',
                refreshFn: function(){
                    store.clearData();
                    aStore.clearData();
                    store.clearFilter();
                    aStore.clearFilter();
                    store.load();
                    list.refresh();
                }
            }
        ],
        grouped: true
    });

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

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