// 针对 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); //});
时间: 2024-09-27 07:13:14