標籤: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分頁筆記