简体   繁体   English

使用ember进行动态分页

[英]Dynamic pagination using ember

We use EmberJs to make up our webapp. 我们使用EmberJs来构建我们的webapp。 I have a pagination code that displays 5 items per each from a total of 100 items. 我有一个分页代码,从总共100个项目中每个显示5个项目。 All the thousand 100 items are taken from a single API call and will be passed to the 'model' as an array ; 所有的1000 个项目都来自单个API调用,并将作为数组传递给“模型” ; and the controller will slice the array based on the page no and page size. 并且控制器将根据页面号和页面大小对数组进行切片。

I want to make the framework dynamic, which means I should be sending an API call to the server with page no and page size inputs, and get the specific response, for each next-page click. 我想让框架动态化,这意味着我应该向服务器发送一个带有页面号和页面大小输入的API调用,并获得每个下一页单击的特定响应。

My code goes here. 我的代码在这里。

Router: 路由器:

App.AccountPointsRoute = Ember.Route.extend({
    totalTransactions: null,
    model: function(params) {
        var page = this.controllerFor('accountPoints').get('page');
        var pointsURL= App.config.getEndpoint()+'/account/points';
        var getUserDetailsForViewUserAccount = function(){
            if(params.email){
                return Ember.$.getJSON(App.config.getEndpoint()+'/admin/userInfoByEmail?email='+params.email).then(function(data){
                    return data.payload.user.totalPoints;
                })
            }
        }
        var transactionURL = App.config.getEndpoint()+'/account/transactions?page='+page+'&pageSize=2&raceId=0&selectedDate='+ App.current_month() +'%20'+App.current_year();
        if(params.email){
             pointsURL = App.config.getEndpoint()+'/admin/points?email='+params.email;
             transactionURL = App.config.getEndpoint()+'/admin/transactions?page=1&pageSize=100&raceId=0&selectedDate='+ App.current_month() +'%20'+App.current_year()+'&email='+params.email;
        }
        var self = this;
        return Em.RSVP.hash({
            points:  Ember.$.getJSON(pointsURL).then(function(data){
                return data.payload;
            }),
            transactions: Ember.$.getJSON(transactionURL).then(function(data){
                self.set('totalTransactions', data.payload.totalRecords)
                return data.payload.logs;
            }),
            userPoints:getUserDetailsForViewUserAccount(),
        });
    },
    setupController: function(controller, models) {
        controller.set('points', models.points);
        controller.set('transactions', models.transactions);
        controller.set('userPoints',models.userPoints);
        controller.set('model', models.transactions);
        controller.set('totalRecords', this.get('totalTransactions'));
    }
});

And the Controller for this is 而控制器就是这样

App.AccountPointsController = App.PaginationController.extend({
    needs:['application'],
    queryParams:['email', 'page'],
    page : 1,
    pageSize: 5,
    numPages: function () {
        var pageSize = this.get('pageSize'),
            //l = this.get('length');
            l = this.get('totalRecords');
        console.log("numPages "+ Math.ceil(l / pageSize));
        return Math.ceil(l / pageSize);
    }.property('pageSize','length'),

    paged: function() {
        var page = this.get('page');
        var transactions = this.get('model');
        var page = this.get('page') - 1,
        pageSize = this.get('pageSize'),
        start = page * pageSize,
        end = start + pageSize
        length = transactions.get('length');
        transactions = transactions.slice(start,end);
        this.set('length', length);
        return transactions;
    }.property('email', 'page', 'model', 'pageSize','pages'),

}); 

What do I need to code to make an API call inside the 'paged' method to get page-number specific response from server. 我需要编码以在'paged'方法内进行API调用以从服务器获取特定于页码的响应。 I look forward to the answer. 我期待着答案。

You don't want to make an API call from your "paged" method because that's in the controller. 您不希望通过“分页”方法进行API调用,因为它位于控制器中。 Your route should take care of the API calls to retrieve data for your model. 您的路线应该处理API调用以检索模型的数据。

What you want is for your links in your template to include query params specifying the desired page and pagesize. 您想要的是模板中的链接包含指定所需页面和页面大小的查询参数。 So something like: 所以类似于:

<div class ="tableNavigation">
    {{#link-to 'accounts' (query-params page=previousPage pagesize=currentPageSize)}}Previous Page{{/link-to}}
    {{#link-to 'accounts' (query-params page=nextPage pagesize=currentPageSize)}}Next Page{{/link-to}}
    Current Page: {{page}}
    Next: {{nextPage}}
    Prev: {{previousPage}}
</div>

Then in your route include params.nextPage and params.currentPageSize in your query strings for your AJAX calls. 然后在您的路由中包含params.nextPage和params.currentPageSize在您的AJAX调用的查询字符串中。 Note that only changing query params won't usually trigger another API call, so also include in your route: 请注意,仅更改查询参数通常不会触发另一个API调用,因此也包括在您的路由中:

queryParams: {
        pagesize: {
            refreshModel: true
        },
        page: {
            refreshModel: true
        }
    },

Then in your controller include queryParams: ['page', 'pagesize'] and functions like this but with bounds checking: 然后在你的控制器中包含queryParams: ['page', 'pagesize']和这样的函数但带边界检查:

nextPage: function() {
        return (parseInt(this.get('page')) + 1);
}.property('page'),

previousPage: function() {
        return (parseInt(this.get('page')) - 1);
}.property('page'),

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

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