码迷,mamicode.com
首页 > Windows程序 > 详细

OData 协议 asp.net Mvc WebAPI分页笔记

时间:2015-05-12 11:42:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:function   asp.net   success   window   count   

// 针对 OData协议的Mvc WebAPI 2.2 分页插件
    ; (function ($, window) {

        var tbPQ = function () {
        };
        // get the length from ‘urlAndParamter‘
        function getCount(urlAndParamter, ok_handler) {
            $.ajax(urlAndParamter, {
                type: "get",
                async: true,
                dataType: ‘json‘,
                crossDomain: true,
                success: function (count) {
                    if (!isNaN(count)) {
                        if (typeof ok_handler === ‘function‘) {
                            ok_handler.call(count, count);
                        }
                    } else {
                        throw new Error("can‘t convert to number ");
                    }
                },
                error: function (XMLHttpRequest, error) {
                    throw new Error("can‘t get the data count from the url ");
                }
            });
        };

        // get the data list from ‘urlAndParamter‘
        function getData(urlAndParamter, page, psize, total, ok_handler) {
            // compute and set the $skip value
            var $skip = (page - 1) * psize;
            if ($skip > total) $skip = 0;
            if (urlAndParamter.indexOf(‘?‘) < 0) {
                urlAndParamter = urlAndParamter + "?l=p";
            }
            urlAndParamter = urlAndParamter + "&$skip=" + $skip + "&$top=" + psize;

            $.ajax(urlAndParamter, {
                type: "get",
                async: true,
                dataType: ‘json‘,
                crossDomain: true,
                success: function (data) {
                    if (typeof ok_handler === ‘function‘) {
                        // data.total -> 记录总数
                        // data.skip  -> 跳过条数(一般计算方式为:单页显示数*上一页的页码值)
                        // data.top   -> 单页条数(单页数据最多显示的数据)
                        // data.page  -> 当前页码
                        // data.mpage -> 最大页码
                        // data.list  -> 数据列表
                        // build the result for return
                        var result = {
                            total: total,
                            skip: $skip,
                            top: psize,
                            page: page,
                            mpage: parseInt(Math.floor(total / psize)),
                            list: data
                        };
                        ok_handler.call(result, result);
                    }
                },
                error: function (XMLHttpRequest, error) {
                    // alert(error);
                    throw new Error("can‘t get the data count from the url ");
                }
            });
        }

        // tbPQ Plugin
        tbPQ.fn = tbPQ.prototype = {
            constructor: tbPQ,
            init: function () {
                return new tbPQ().constructor;
            },
            buildPage: function (psize, total) {
            },
            getPagedData: function (durlAndParam, qurlAndParam, page, psize, ok) {
                getCount(qurlAndParam, function (count) {
                    getData(durlAndParam, page, psize, count, ok);
                })
            },
            getPagedData1: function (url, page, psize, ok) {
                tbPQ.fn.getPagedData(url, url + "/?qc=1", page, psize, ok);
            },
        };
        tbPQ.fn.init.prototype = tbPQ.prototype;
        window.TbPQ = tbPQ.fn.init();
    })(jQuery, window);

    //TbPQ.fn.getPagedData1("http://localhost:43402/api/loginlog", 1, 20, function (data) {
    //    // data.total -> 记录总数
    //    // data.skip  -> 跳过条数(一般计算方式为:单页显示数*上一页的页码值)
    //    // data.top   -> 单页条数(单页数据最多显示的数据)
    //    // data.page  -> 当前页码
    //    // data.mpage -> 最大页码
    //    // data.list  -> 数据列表
    //    alert(data.list.length);
    //});


本文出自 “傻帽” 博客,请务必保留此出处http://hotboy.blog.51cto.com/8826994/1650505

OData 协议 asp.net Mvc WebAPI分页笔记

标签:function   asp.net   success   window   count   

原文地址:http://hotboy.blog.51cto.com/8826994/1650505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!