• ajax分页


      1 一、分页方法
      2 
      3 /**
      4 
      5  * 分页方法
      6 
      7  * @param page int 当前页码数
      8 
      9  * @param b    object 
     10 
     11  *             b.js js数据调用方法
     12 
     13  *             b.total 数据总条数
     14 
     15  *             b.pagesize 每页多少条
     16 
     17  *             b.pagelen 页码数量
     18 
     19  */ 
     20 
     21 function ajaxPage(page,b){
     22 
     23  var js=b.js;        //调用的方法
     24 
     25     var total=b.total;  //总条数
     26 
     27  var pagesize=b.pagesize; //每页多少条
     28 
     29     var pagelen=b.pagelen;   //页码数量
     30 
     31     if (page > 99){         
     32 
     33         pagelen = 5;
     34 
     35     }  
     36 
     37     pagecode = '';
     38 
     39     page = parseInt(page);
     40 
     41     total = parseInt(total);
     42 
     43     if (!total){
     44 
     45       return false; 
     46 
     47     }
     48 
     49     var  pages = Math.ceil(total / pagesize);//总页数
     50 
     51     if (page < 1)
     52 
     53         page = 1;
     54 
     55     if (page > pages)
     56 
     57         page = pages;
     58 
     59     // offset = pagesize * (page - 1);
     60 
     61    var init = 1;
     62 
     63    var max = pages;
     64 
     65    var pagelen = (pagelen % 2) ? pagelen : pagelen + 1;
     66 
     67    var pageoffset = (pagelen - 1) / 2;
     68 
     69      b=$.toJSON(b);
     70 
     71     // 生成html
     72 
     73     var pagecode = "<dl>";
     74 
     75     if (page != 1) {
     76 
     77         pagecode+="<dt><a href='javascript:void(0)' onclick='"+js+"(1,"+b+")'>首页</a></dt>";
     78 
     79         pagecode+="<dt><a href='javascript:void(0)' onclick='"+js+"("+(page-1)+","+b+")'>上一页</a></dt>";
     80 
     81     }
     82 
     83     if (pages > pagelen) {
     84 
     85         if (page <= pageoffset) {
     86 
     87             init = 1;
     88 
     89             max = pagelen;
     90 
     91         } else {
     92 
     93             if (page + pageoffset >= pages + 1) {
     94 
     95                 init = pages - pagelen + 1;
     96 
     97             } else {
     98 
     99                 init = page - pageoffset;
    100 
    101                 max = page + pageoffset;
    102 
    103             }
    104 
    105         }
    106 
    107     }
    108 
    109     // 生成html
    110 
    111     for ( var i = init; i <= max; i++) {
    112 
    113         if (i == page) {
    114 
    115             pagecode+="<dd><a href='javascript:void(0)' class='choose'>"+i+"</a></dd>";
    116 
    117         } else {
    118 
    119             pagecode+="<dd><a href='javascript:void(0)' onclick='"+js+"("+i+","+b+")'>"+i+"</a></dd>";
    120 
    121         }
    122 
    123     }
    124 
    125 
    126 
    127     if (page != pages) {
    128 
    129         pagecode+="<dt><a href='javascript:void(0)' onclick='"+js+"("+(page+1)+","+b+")'>下一页</a></dt>";
    130 
    131         pagecode+="<dt><a href='javascript:void(0)' onclick='"+js+"("+pages+","+b+")'>尾页</a></dt>";
    132 
    133     }
    134 
    135     pagecode += "<dt><span>共"+pages+"页</span></dt>";
    136 
    137     pagecode+="</dl>";
    138 
    139     return pagecode;
    140 
    141 }
    142 
    143 
    144 
    145 二、方法中用到$.toJSON,插件代码
    146 
    147 /**
    148 
    149 * jQuery JSON plugin 2.4.0
    150 
    151 *
    152 
    153 * @author Brantley Harris, 2009-2011
    154 
    155 * @author Timo Tijhof, 2011-2014
    156 
    157 * @source This plugin is heavily influenced by MochiKit's serializeJSON, which is
    158 
    159 * copyrighted 2005 by Bob Ippolito.
    160 
    161 * @source Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
    162 
    163 * website's http://www.json.org/json2.js, which proclaims:
    164 
    165 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
    166 
    167 * I uphold.
    168 
    169 * @license MIT License <http://opensource.org/licenses/MIT>
    170 
    171 */
    172 
    173 (function ($) {
    174 
    175 'use strict';
    176 
    177 
    178 
    179 var escape = /["\\\x00-\x1f\x7f-\x9f]/g,
    180 
    181 meta = {
    182 
    183 '\b': '\\b',
    184 
    185 '\t': '\\t',
    186 
    187 '\n': '\\n',
    188 
    189 '\f': '\\f',
    190 
    191 '\r': '\\r',
    192 
    193 '"': '\\"',
    194 
    195 '\\': '\\\\'
    196 
    197 },
    198 
    199 hasOwn = Object.prototype.hasOwnProperty;
    200 
    201 
    202 
    203 /**
    204 
    205 * jQuery.toJSON
    206 
    207 * Converts the given argument into a JSON representation.
    208 
    209 *
    210 
    211 * @param o {Mixed} The json-serializable *thing* to be converted
    212 
    213 *
    214 
    215 * If an object has a toJSON prototype, that will be used to get the representation.
    216 
    217 * Non-integer/string keys are skipped in the object, as are keys that point to a
    218 
    219 * function.
    220 
    221 *
    222 
    223 */
    224 
    225 $.toJSON = typeof JSON === 'object' && JSON.stringify ? JSON.stringify : function (o) {
    226 
    227 if (o === null) {
    228 
    229 return 'null';
    230 
    231 }
    232 
    233 
    234 
    235 var pairs, k, name, val,
    236 
    237 type = $.type(o);
    238 
    239 
    240 
    241 if (type === 'undefined') {
    242 
    243 return undefined;
    244 
    245 }
    246 
    247 
    248 
    249 // Also covers instantiated Number and Boolean objects,
    250 
    251 // which are typeof 'object' but thanks to $.type, we
    252 
    253 // catch them here. I don't know whether it is right
    254 
    255 // or wrong that instantiated primitives are not
    256 
    257 // exported to JSON as an {"object":..}.
    258 
    259 // We choose this path because that's what the browsers did.
    260 
    261 if (type === 'number' || type === 'boolean') {
    262 
    263 return String(o);
    264 
    265 }
    266 
    267 if (type === 'string') {
    268 
    269 return $.quoteString(o);
    270 
    271 }
    272 
    273 if (typeof o.toJSON === 'function') {
    274 
    275 return $.toJSON(o.toJSON());
    276 
    277 }
    278 
    279 if (type === 'date') {
    280 
    281 var month = o.getUTCMonth() + 1,
    282 
    283 day = o.getUTCDate(),
    284 
    285 year = o.getUTCFullYear(),
    286 
    287 hours = o.getUTCHours(),
    288 
    289 minutes = o.getUTCMinutes(),
    290 
    291 seconds = o.getUTCSeconds(),
    292 
    293 milli = o.getUTCMilliseconds();
    294 
    295 
    296 
    297 if (month < 10) {
    298 
    299 month = '0' + month;
    300 
    301 }
    302 
    303 if (day < 10) {
    304 
    305 day = '0' + day;
    306 
    307 }
    308 
    309 if (hours < 10) {
    310 
    311 hours = '0' + hours;
    312 
    313 }
    314 
    315 if (minutes < 10) {
    316 
    317 minutes = '0' + minutes;
    318 
    319 }
    320 
    321 if (seconds < 10) {
    322 
    323 seconds = '0' + seconds;
    324 
    325 }
    326 
    327 if (milli < 100) {
    328 
    329 milli = '0' + milli;
    330 
    331 }
    332 
    333 if (milli < 10) {
    334 
    335 milli = '0' + milli;
    336 
    337 }
    338 
    339 return '"' + year + '-' + month + '-' + day + 'T' +
    340 
    341 hours + ':' + minutes + ':' + seconds +
    342 
    343 '.' + milli + 'Z"';
    344 
    345 }
    346 
    347 
    348 
    349 pairs = [];
    350 
    351 
    352 
    353 if ($.isArray(o)) {
    354 
    355 for (k = 0; k < o.length; k++) {
    356 
    357 pairs.push($.toJSON(o[k]) || 'null');
    358 
    359 }
    360 
    361 return '[' + pairs.join(',') + ']';
    362 
    363 }
    364 
    365 
    366 
    367 // Any other object (plain object, RegExp, ..)
    368 
    369 // Need to do typeof instead of $.type, because we also
    370 
    371 // want to catch non-plain objects.
    372 
    373 if (typeof o === 'object') {
    374 
    375 for (k in o) {
    376 
    377 // Only include own properties,
    378 
    379 // Filter out inherited prototypes
    380 
    381 if (hasOwn.call(o, k)) {
    382 
    383 // Keys must be numerical or string. Skip others
    384 
    385 type = typeof k;
    386 
    387 if (type === 'number') {
    388 
    389 name = '"' + k + '"';
    390 
    391 } else if (type === 'string') {
    392 
    393 name = $.quoteString(k);
    394 
    395 } else {
    396 
    397 continue;
    398 
    399 }
    400 
    401 type = typeof o[k];
    402 
    403 
    404 
    405 // Invalid values like these return undefined
    406 
    407 // from toJSON, however those object members
    408 
    409 // shouldn't be included in the JSON string at all.
    410 
    411 if (type !== 'function' && type !== 'undefined') {
    412 
    413 val = $.toJSON(o[k]);
    414 
    415 pairs.push(name + ':' + val);
    416 
    417 }
    418 
    419 }
    420 
    421 }
    422 
    423 return '{' + pairs.join(',') + '}';
    424 
    425 }
    426 
    427 };
    428 
    429 
    430 
    431 /**
    432 
    433 * jQuery.evalJSON
    434 
    435 * Evaluates a given json string.
    436 
    437 *
    438 
    439 * @param str {String}
    440 
    441 */
    442 
    443 $.evalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) {
    444 
    445 /*jshint evil: true */
    446 
    447 return eval('(' + str + ')');
    448 
    449 };
    450 
    451 
    452 
    453 /**
    454 
    455 * jQuery.secureEvalJSON
    456 
    457 * Evals JSON in a way that is *more* secure.
    458 
    459 *
    460 
    461 * @param str {String}
    462 
    463 */
    464 
    465 $.secureEvalJSON = typeof JSON === 'object' && JSON.parse ? JSON.parse : function (str) {
    466 
    467 var filtered =
    468 
    469 str
    470 
    471 .replace(/\\["\\\/bfnrtu]/g, '@')
    472 
    473 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
    474 
    475 .replace(/(?:^|:|,)(?:\s*\[)+/g, '');
    476 
    477 
    478 
    479 if (/^[\],:{}\s]*$/.test(filtered)) {
    480 
    481 /*jshint evil: true */
    482 
    483 return eval('(' + str + ')');
    484 
    485 }
    486 
    487 throw new SyntaxError('Error parsing JSON, source is not valid.');
    488 
    489 };
    490 
    491 
    492 
    493 /**
    494 
    495 * jQuery.quoteString
    496 
    497 * Returns a string-repr of a string, escaping quotes intelligently.
    498 
    499 * Mostly a support function for toJSON.
    500 
    501 * Examples:
    502 
    503 * >>> jQuery.quoteString('apple')
    504 
    505 * "apple"
    506 
    507 *
    508 
    509 * >>> jQuery.quoteString('"Where are we going?", she asked.')
    510 
    511 * "\"Where are we going?\", she asked."
    512 
    513 */
    514 
    515 $.quoteString = function (str) {
    516 
    517 if (str.match(escape)) {
    518 
    519 return '"' + str.replace(escape, function (a) {
    520 
    521 var c = meta[a];
    522 
    523 if (typeof c === 'string') {
    524 
    525 return c;
    526 
    527 }
    528 
    529 c = a.charCodeAt();
    530 
    531 return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
    532 
    533 }) + '"';
    534 
    535 }
    536 
    537 return '"' + str + '"';
    538 
    539 };
    540 
    541 
    542 
    543 }(jQuery));
    做好记录,以后总会用到!
  • 相关阅读:
    裴蜀定理
    上下界网络流
    寻找符合子序列要求的区间个数
    小猪分配 , 最大流
    floyd + 最大流 (奶牛分配问题)
    抛硬币问题
    消消乐
    Entity Framework(1)
    冒泡排序
    二分法查找数据
  • 原文地址:https://www.cnblogs.com/faronl/p/4565060.html
Copyright © 2020-2023  润新知