cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$.fn.pagination = function(_opts){
 
    var opts = $.extend({
        curl : "",
        durl : "",
        mdiv : "",
        pdiv : "",
        perPage : 6,
        param : {}
    },_opts);
    
    var status = {
        recordCount : 0,
        totalPage : 0
    };
    
    return this.each(function(){
        var currPage,panel = $(this).css({'text-align':'center'});
        
        function _loadPage(cpage){
            panel.html("<span>加载中</span>").unbind('click');
            currPage = cpage;
            opts.param = $.extend(opts.param,{currPage:cpage,pageSize:opts.perPage});
            $.post(opts.durl,opts.param,function(data){
                if(opts.dataHandler) {
                    opts.dataHandler(data);
                } else {
                    if(opts.mdiv)$('#'+opts.mdiv).append(data);
                }
                if(opts.callback)opts.callback(currPage,status.totalPage,status.recordCount);
                _render();
            });
        }
        
        function _gotoPage(page){
            return function(){
                _loadPage(page);
            };
        }
        
        function _render(){
            if(currPage == status.totalPage){
                panel.html("<span>没有了</span>").unbind('click');
            }else{
                panel.html("<span>加载更多</span>").bind('click',_gotoPage(currPage+1));
            }
        }
        //开始执行
        $.post(opts.curl,opts.param,function(json){
            //每次新的查询,都移除panel的click事件
            panel.unbind('click');
            //设置翻页
            var total = json;
            if(total==0){
                panel.html("<span>暂无数据</span>").show();
                if(opts.callback)opts.callback(0,0,0);
            }else{
                status.recordCount = total;
                status.totalPage = Math.ceil(total/opts.perPage);
                //放置第一页
                _loadPage(1);
                //if(total>opts.perPage){
                //    $('#'+opts.pdiv).show();
                //}else{
                //    $('#'+opts.pdiv).hide();
                //}
                $('#'+opts.pdiv).show();
            }
        },'json');
    });
};