jq ajax分頁插件:pagination -> 支持首頁尾頁跳轉頁、列表搜索分頁正常

最終示例圖:javascript

html代碼(搜索塊)

............................
<button type="submit" class="btn btn-success" id="btn_cz" name="" onclick="search()">
<i class="Hui-iconfont">&#xe665;</i> 搜&nbsp;索</button>

html代碼(分頁代碼:)

<div class="pagination" id="Pagination" style=""></div>css

css樣式代碼:

.pagination{float:right;height:40px;padding:20px 0;margin-top: 20px;}
.pagination a{display:block;float:left;margin-right:10px;padding:2px 12px;height:24px;border:1px #ccc solid;background:#fff;text-decoration:none;color:#808080;font-size:12px;line-height:24px}
.pagination a,.pagination span{float:left;border:0;background:#5a98de;color:#fff;}
.pagination i{float:left;border:0;background: #de7504;color:#fff;margin-right: 8px;padding: 3px 6px;}
.pagination .current{border:0;background:#dd514c;color:#fff;float:left;padding:2px 12px;font-size:12px;height:24px;line-height:24px;margin-right:8px}
.pagination .current.prev,.pagination .current.next{float:left;padding:2px 12px;font-size:12px;height:24px;line-height:24px;color:#bbb;border:1px #ccc solid;background:#fcfcfc;margin-right:8px}
.pagination p{float:left;font-size:13px;height:24px;line-height:24px;color:#333;}
.pagination p b{color:red}
#pagevalue{float:left;padding:2px 7px;font-size:12px;margin-right:8px;border:1px solid #ccc; text-align: center;height: 24px;margin-left: 8px;}
#dosubtiao{text-align: center;float:left}

重點來了->javascript代碼:

頁面中的javascript代碼:html

var pageIndex = 0; //初始頁索引
            var pageSize = '{$showrow}'; //每頁顯示的條數
            var totalSize; //設置默認總數據條數
            $(function() {
                totalSize = '{$count}'; //綁定後臺字段
                //InitTable(0);
                window.flagp = 0;
                pagecallback(totalSize);
            });
            //定義回調函數
            function PageCallback(index, jq) {
                InitTable(index);
            };
            //定義初始函數
            function InitTable(pageIndex) {
                if(window.flagp) {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: '/user/user_list_json',
                        data: {
                            "doajax": "ajaxpost",
                            "pageIndex": pageIndex,
                            "pageSize": pageSize,
                            "workstate": '{$workstate}',
                            "user_name": $("#user_name").val(),
                            "s_state": $("#stateDdl").val(),
                        },
                        success: function(data) {
                            ins(data);
                        }
                    });
                } else {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: '/user/user_list_json',
                        data: {
                            "doajax": "ajaxpost",
                            "pageIndex": pageIndex,
                            "pageSize": pageSize,
                            "workstate": '{$workstate}',
                 
                        },
                        success: function(data) {
                            ins(data);
                        }
                    });
                }
            };
            function ins(data){
                count = data.sum;
                $("#count").html(count);
                data = data.data;
                if(data != "") {
                    var str = '';
                    $.each(data, function(index, v) {
                        str += ("<tr class=\"text-c\"> <td><input type=\"checkbox\" value='" + v.pf_id + "'   id='" + v.pf_id + "' name=\"userCheck\" onclick=\"swapCheck(this);\"></td><td class=\"idss\">" + v.pf_fileNumber + "</td><td class=\"idss\">" + v.pf_name + "</td><td class=\"idss\">" + v.pf_sex + "</td><td class=\"idss\">" + v.m_id_post + "</td><td class=\"idss\">" + v.m_id_diploma + "</td><td class=\"idss\">" + v.pf_phone + "</td><td class=\"idss\">" + v.pf_birthday + "</td><td class=\"idss\">" + v.pf_entrytime + "</td>{if $workstate==0}<td class=\"idss\">" + v.pf_departuretime + "</td>{/if}<td class=\"idss\">" + v.worktime + "</td><td class=\"td-status\"><span class=\"label radius " + v.pf_workingState.state_style + "\">" + v.pf_workingState.state_name + "</span></td><td class=\"idss\">" + v.pf_addtime + "</td><td class=\"td-manage\">\n   <a title=\"編輯\" href=\"javascript:;\" onclick=\"admin_edit('管理員編輯','/user/user_edit/pf_id/" + v.pf_id + "','" + v.pf_id + "','850','650')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6df;</i></a>\n   <a title=\"刪除\" href=\"javascript:;\" onclick=\"del_obj(this," + v.pf_id + ",'/user/user_del')\" class=\"ml-5\" style=\"text-decoration:none\"><i class=\"Hui-iconfont\">&#xe6e2;</i></a>\n</td></tr>");
                    });
                    $("#tblData").html(str);
                } else {
                    $("#tblData").html("沒有數據");
                }
            }
            //查詢
            function search() {
                $.post("/user/user_list_json", {
                    "doajax": "ajaxpost",
                    "pageIndex": pageIndex,
                    "pageSize": pageSize,
                    "workstate": '{$workstate}',
                    "user_name": $("#user_name").val(),
                    "s_state": $("#stateDdl").val(),
                }, function(arg1) {
                    window.flagp = 1;
                    window.resdata = arg1;
                    pagecallback(arg1.sum);
                });
            };
            function pagecallback(datasum, pageIndex) {
                $("#Pagination").pagination(datasum, { //顯示底端的頁碼
                    callback: PageCallback,
                    first_text: '首頁',
                    pre_text: '上一頁',
                    net_text: '下一頁',
                    last_text: '尾頁',
                    items_per_page: pageSize, //顯示條數
                    num_display_entries: 3, //連續分頁主體部分分頁條目數
                    current_page: pageIndex, //當前頁索引
                    num_edge_entries: 2
                });
            }

 

jquery.pagination.js  文件:java

/**
 * This jQuery plugin displays pagination links inside the selected elements.
 *
 * @author Gabriel Birke (birke *at* d-scribe *dot* de)
 * @version 1.2
 * @param {int} maxentries Number of entries to paginate
 * @param {Object} opts Several options (see README for documentation)
 * @return {Object} jQuery Object
 */
jQuery.fn.pagination = function(maxentries, opts){
	opts = jQuery.extend({
		items_per_page:10,
		num_display_entries:10,
		current_page:0,
		num_edge_entries:0,
		link_to:"#",
        first_text: "首頁",
        last_text: "尾頁",
		prev_text:"上一頁",
		next_text:"下一頁",
		ellipse_text:"...",
		prev_show_always:true,
		next_show_always:true,
		callback:function(){return false;}
	},opts||{});
	
	return this.each(function() {
		/**
		 * 計算最大分頁顯示數目
		 */
		function numPages() {
			return Math.ceil(maxentries/opts.items_per_page);
		}	
		/**
		 * 極端分頁的起始和結束點,這取決於current_page 和 num_display_entries.
		 * @返回 {數組(Array)}
		 */
		function getInterval()  {
			var ne_half = Math.ceil(opts.num_display_entries/2);
			var np = numPages();
			var upper_limit = np-opts.num_display_entries;
			var start = current_page>ne_half?Math.max(Math.min(current_page-ne_half, upper_limit), 0):0;
			var end = current_page>ne_half?Math.min(current_page+ne_half, np):Math.min(opts.num_display_entries, np);
			return [start,end];
		}
		
		/**
		 * 分頁連接事件處理函數
		 * @參數 {int} page_id 爲新頁碼
		 */
		function pageSelected(page_id, evt){
			current_page = page_id;
			drawLinks();
			var continuePropagation = opts.callback(page_id, panel);
			if (!continuePropagation) {
				if (evt.stopPropagation) {
					evt.stopPropagation();
				}
				else {
					evt.cancelBubble = true;
				}
			}
			return continuePropagation;
		}
		
		/**
		 * 此函數將分頁連接插入到容器元素中
		 */
		function drawLinks() {
			panel.empty();
			var interval = getInterval();
			var np = numPages();
			// 這個輔助函數返回一個處理函數調用有着正確page_id的pageSelected。
			var getClickHandler = function(page_id) {
				return function(evt){ return pageSelected(page_id,evt); }
			}
			//輔助函數用來產生一個單連接(若是不是當前頁則產生span標籤)
			var appendItem = function(page_id, appendopts){
				page_id = page_id<0?0:(page_id<np?page_id:np-1); // 規範page id值
				appendopts = jQuery.extend({text:page_id+1, classes:""}, appendopts||{});
				if(page_id == current_page){
					var lnk = jQuery("<span class='current'>"+(appendopts.text)+"</span>");
				}else{
					var lnk = jQuery("<p><a>"+(appendopts.text)+"</a></p>")
						.bind("click", getClickHandler(page_id))
						.attr('href', opts.link_to.replace(/__id__/,page_id));		
				}
				if(appendopts.classes){lnk.addClass(appendopts.classes);}
				panel.append(lnk);
			}
            // firstPage 跳轉到首頁
            if (opts.first_text && (current_page > 0 || opts.prev_show_always)) {
                appendItem(0, { text: opts.first_text, classes: "prev disabled" });
            }
			// 產生"Previous"-連接
			if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){
				appendItem(current_page-1,{text:opts.prev_text, classes:"prev"});
			}
			// 產生起始點
			if (interval[0] > 0 && opts.num_edge_entries > 0)
			{
				var end = Math.min(opts.num_edge_entries, interval[0]);
				for(var i=0; i<end; i++) {
					appendItem(i);
				}
				if(opts.num_edge_entries < interval[0] && opts.ellipse_text)
				{
					jQuery("<i>"+opts.ellipse_text+"</i>").appendTo(panel);
				}
			}
			// 產生內部的些連接
			for(var i=interval[0]; i<interval[1]; i++) {
				appendItem(i);
			}
			// 產生結束點
			if (interval[1] < np && opts.num_edge_entries > 0)
			{
				if(np-opts.num_edge_entries > interval[1]&& opts.ellipse_text)
				{
					jQuery("<i>"+opts.ellipse_text+"</i>").appendTo(panel);
				}
				var begin = Math.max(np-opts.num_edge_entries, interval[1]);
				for(var i=begin; i<np; i++) {
					appendItem(i);
				}
				
			}

            // 產生 "Next"-連接
			if(opts.next_text && (current_page < np-1 || opts.next_show_always)){
				appendItem(current_page+1,{text:opts.next_text, classes:"next"});
			}

            // lastPage 跳轉到尾頁
            if (opts.last_text && (current_page < np - 1 || opts.next_show_always)) {
                appendItem(np, { text: opts.last_text, classes: "prev disabled" });
            }
            /****************** Added ***************/
            /*插入一個文本框,用戶輸入並回車後進行跳轉*/
            var pagetext = '<input id="pagevalue" size="1" value="'+(current_page+1)+'"type="text">';
            var tosumPage='<p>共 <b>'+np+'</b> 頁,跳轉至</p>';
            var toPage='<div id="dosubtiao"><a href="#">肯定</a></div>';
            $(tosumPage).appendTo(panel);
            $(pagetext).appendTo(panel);
            $(toPage).appendTo(panel);
            $("#dosubtiao").bind("click",function(evt){
                var iPageNum = $.trim($("#pagevalue").val()) -1;
                if(iPageNum<0){
                    alert("超過最小頁數");
                }else if(iPageNum<np){
                    pageSelected(iPageNum,evt);
                }else{
                    alert("超過最大頁數");
                }
            });
            /****************** Added End ******************/


		}
		
		//從選項中提取current_page
		var current_page = opts.current_page;
		//建立一個顯示條數和每頁顯示條數值
		maxentries = (!maxentries || maxentries < 0)?1:maxentries;
		opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page;
		//存儲DOM元素,以方便從全部的內部結構中獲取
		var panel = jQuery(this);
		// 得到附加功能的元素
		this.selectPage = function(page_id){ pageSelected(page_id);}
		this.prevPage = function(){ 
			if (current_page > 0) {
				pageSelected(current_page - 1);
				return true;
			}
			else {
				return false;
			}
		}
		this.nextPage = function(){ 
			if(current_page < numPages()-1) {
				pageSelected(current_page+1);
				return true;
			}
			else {
				return false;
			}
		}
		// 全部初始化完成,繪製連接
		drawLinks();
        // 回調函數
        opts.callback(current_page, this);
	});
}
相關文章
相關標籤/搜索