【问题标题】:API Pagination,Filtering,Sorting VS CLIENT Pagination,Filtering,SortingAPI 分页、过滤、排序 VS CLIENT 分页、过滤、排序
【发布时间】:2018-01-13 02:43:45
【问题描述】:

场景

  • 网站页面有一个带有分页、过滤、排序的表格视图。

  • 表中的数据是从 REST API 服务器获取的,数据包含 数百万条记录。

  • 数据库 REST API 服务器 Web 服务器 浏览器


问题

  • 哪里是进行分页、过滤、排序的最佳位置。

可能的解决方案

  • 网络服务器中的分页、过滤、排序

    • REST API 服务器发送完整列表(请求的实体可能不需要)
    • Web 服务器必须使用逻辑分页、过滤、排序可能会增加负载
    • 如果 web 服务器只想要记录 REST API 服务器需要有单独的 api,否则 web 服务器必须从完整列表中解析
  • REST API 服务器中的分页、过滤、排序

    • REST API 服务器接受输入参数并进行分页、过滤、排序
    • web server 可以直接绑定到table view,不需要格式化结果
    • 只发送请求的数据,从而节省带宽
    • 更像是 Google 的 fusion tables api

在不违反 REST API 标准的情况下,最好的方法是什么?

【问题讨论】:

    标签: node.js database rest api pagination


    【解决方案1】:

    /* 处理分页。 */ router.post('/products/view-front', function(req, res){

    /* Set our internal DB variable */
    var db = req.db;
    
        /* Set our collection */
        products = db.get('products');
    
        pag_content = '';
        pag_navigation = '';
    
        page = parseInt(req.body.data.page); /* Page we are currently at */
        name = req.body.data.name; /* Name of the column name we want to sort */
        sort = req.body.data.sort == 'ASC' ? 1 : -1; /* Order of our sort (DESC or ASC) */
        max = parseInt(req.body.data.max); /* Number of items to display per page */
        search = req.body.data.search; /* Keyword provided on our search box */
    
        cur_page = page;
        page -= 1;
        per_page = max ? max : 20;
        previous_btn = true;
        next_btn = true;
        first_btn = true;
        last_btn = true;
        start = page * per_page;
    
        where_search = {};
    
    /* Check if there is a string inputted on the search box */
    if( search != '' ){
        /* If a string is inputted, include an additional query logic to our main query to filter the results */
        var filter = new RegExp(search, 'i');
        where_search = {
            '$or' : [
                {'name' : filter},
                {'price' : filter},
            ]
        }
    }
    
    var all_items = '';
    var count = '';
    var sort_query = {};
    
    /* We use async task to make sure we only return data when all queries completed successfully */
    async.parallel([
        function(callback) {
            /* Use name and sort variables as field names */
            sort_query[name] = sort;
    
            /* Retrieve all the posts */
            products.find( where_search, {
                limit: per_page,
                skip: start,
                sort: sort_query
    
            }, function(err, docs){
                if (err) throw err;
                // console.log(docs);
                all_items = docs;
                callback();
    
            });
        },
        function(callback) {
            products.count(where_search, function(err, doc_count){
                if (err) throw err;
                // console.log(count);
                count = doc_count;
                callback();
            });
        }
    ], function(err) { //This is the final callback
        /* Check if our query returns anything. */
        if( count ){
            for (var key in all_items) {
                pag_content += '<div class="col-sm-3">' +
                    '<div class="panel panel-default">' +
                        '<div class="panel-heading">' +
                            all_items[key].name +
                        '</div>' +
                        '<div class="panel-body p-0 p-b">' +
                            '<a href="products-single.php?item=' + all_items[key]._id + '"><img src="img/uploads/' + all_items[key].featured_image + '" width="100%" class="img-responsive" /></a>' +
                            '<div class="list-group m-0">' +
                                '<div class="list-group-item b-0 b-t">' +
                                    '<i class="fa fa-calendar-o fa-2x pull-left ml-r"></i>' +
                                    '<p class="list-group-item-text">Price</p>' +
                                    '<h4 class="list-group-item-heading">$' + parseFloat(all_items[key].price).toFixed(2) + '</h4>' +
                                '</div>' +
                                '<div class="list-group-item b-0 b-t">' +
                                    '<i class="fa fa-calendar fa-2x pull-left ml-r"></i>' +
                                    '<p class="list-group-item-text">Quantity</p>' +
                                    '<h4 class="list-group-item-heading">' + all_items[key].quantity + '</h4>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                        '<div class="panel-footer">' +
                            '</p><a href="products-single.php?item=' + all_items[key]._id + '" class="btn btn-success btn-block">View Item</a></p>' +
                         '</div>' +
                    '</div>' +
                '</div>';
            }
        }
    
        pag_content = pag_content + "<br class = 'clear' />";
    
        no_of_paginations = Math.ceil(count / per_page);
    
        if (cur_page >= 7) {
            start_loop = cur_page - 3;
            if (no_of_paginations > cur_page + 3)
                end_loop = cur_page + 3;
            else if (cur_page <= no_of_paginations && cur_page > no_of_paginations - 6) {
                start_loop = no_of_paginations - 6;
                end_loop = no_of_paginations;
            } else {
                end_loop = no_of_paginations;
            }
        } else {
            start_loop = 1;
            if (no_of_paginations > 7)
                end_loop = 7;
            else
                end_loop = no_of_paginations;
        }
    
        pag_navigation += "<ul>";
    
        if (first_btn && cur_page > 1) {
            pag_navigation += "<li p='1' class='active'>First</li>";
        } else if (first_btn) {
            pag_navigation += "<li p='1' class='inactive'>First</li>";
        }
    
        if (previous_btn && cur_page > 1) {
            pre = cur_page - 1;
            pag_navigation += "<li p='" + pre + "' class='active'>Previous</li>";
        } else if (previous_btn) {
            pag_navigation += "<li class='inactive'>Previous</li>";
        }
        for (i = start_loop; i <= end_loop; i++) {
    
            if (cur_page == i)
                pag_navigation += "<li p='" + i + "' class = 'selected' >" + i + "</li>";
            else
                pag_navigation += "<li p='" + i + "' class='active'>" + i + "</li>";
        }
    
        if (next_btn && cur_page < no_of_paginations) {
            nex = cur_page + 1;
            pag_navigation += "<li p='" + nex + "' class='active'>Next</li>";
        } else if (next_btn) {
            pag_navigation += "<li class='inactive'>Next</li>";
        }
    
        if (last_btn && cur_page < no_of_paginations) {
            pag_navigation += "<li p='" + no_of_paginations + "' class='active'>Last</li>";
        } else if (last_btn) {
            pag_navigation += "<li p='" + no_of_paginations + "' class='inactive'>Last</li>";
        }
    
        pag_navigation = pag_navigation + "</ul>";
    
        var response = {
            'content': pag_content,
            'navigation' : pag_navigation
        };
    
        res.send(response);
    
    });
    

    });

    module.exports = 路由器;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 2011-06-04
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多