使用jQuery.fn自訂jQuery翻頁外掛程式_jquery

來源:互聯網
上載者:User
第一次寫jQuery外掛程式。自己感覺寫的也不怎麼樣。寫jQuery外掛程式利用的就是這個東東 jQuery.fn,例如

複製代碼 代碼如下:

jQuery.fn.pluginName=function(){};

這個是我寫的分頁外掛程式的樣子
外掛程式通過一個外放的函數來進行翻頁操作,無論是點擊前進、後退、還是改變頁面大小,都會調用該函數。
先看看外掛程式的代碼結構
複製代碼 代碼如下:

(function ($) {
//存放外掛程式所需的屬性欄位
var PagerFields = {
};
//外掛程式的私人函數
function setImageButtonSate() {
}
//外掛程式的公用函數
var methods = {
_pagerFields: null,
ini: function (options) {
},
destory: function (options) {
return $(this).each(function () {
var $this = $(this); $this.removeData('HGPager2');
});
}
};
//定義外掛程式
$.fn.HGPager2 = function () {
var method = arguments[0];
if (methods[method]) {
method = methods[method];
arguments = Array.prototype.slice.call(arguments, 1);
}
else if (typeof (method) == 'object' || !method) {
method = methods.ini;
}
else {
$.error('Method ' + method + ' does not exist on jQuery.pluginName');
return this;
}
return method.apply(this, arguments);
};
})(jQuery);

這個結構是按照《深入理解jQuery外掛程式開發》文中的模式(這篇文章個人感覺不錯的,一開始學習寫外掛程式的朋友建議先看看這篇文章)。整個外掛程式的定義、以及它的私人變數函數都被包在$(function(){});裡面了。用這種方式即可以保護外掛程式私人變數的安全性,從另一個角度看也可以避免了變數名重複而導致的麻煩。如果把私人變數放在$.fn.HGPager2 = function () {}這個函數裡面的話,會有弊端,要是外掛程式外放了一些函數來擷取外掛程式的一些參數資訊(如當前的頁碼,當前頁大小諸如此類)就不能擷取到確切的參數資訊。因為根據js的範圍理論之前構造外掛程式時的參數資訊與後來調用函數進入的範圍是不一樣的。上面的這種模式,就確保了構造外掛程式與調用外掛程式函數時進入的範圍是一樣的。
下面再列舉外掛程式的各個部分
這是外掛程式的私人變數,用一個PagerFields類來存放。
複製代碼 代碼如下:

var PagerFields = {
pageSize: 10, //頁面大小
pageCount: 0,//頁面數量
recordCount: 0,//記錄總數
currentPage: 0,//當前頁碼
pagerFunction: null //翻頁時觸發的調用的函數,用於讀取資料。
};

這個是外掛程式的私人函數,用於設定翻頁按鈕的狀態(按鈕的圖片沒有上傳,若是把代碼直接複製粘貼啟動並執行話沒有圖片的)
複製代碼 代碼如下:

function setImageButtonSate() {
if (PagerFields.currentPage <= 1)
$("#HG_pagerPre").css("backgroundPosition", "-3px -3px"); //grey pre
else
$("#HG_pagerPre").css("backgroundPosition", "-3px -19px"); // black pre
if (PagerFields.currentPage == PagerFields.pageCount)
$("#HG_pagerNext").css("backgroundPosition", "-20px -3px"); //grey next
else
$("#HG_pagerNext").css("backgroundPosition", "-20px -19px"); // black next
}

這個是構造外掛程式的函數
複製代碼 代碼如下:

ini: function (options) {
_pagerFields = PagerFields;
return this.each(function () {
var $this = $(this);
$this.text("");
$out_div = $("<div id='hg_pager_outter_div' style='margin-bottom:-10px'></div>");
$this.append($out_div);
$out_div.append("<div><span id='HG_pagerPre' style='background-position:-3px -19px'></span></div>");
$out_div.append("<div style='float:left;'><input id='HG_pageNum' type='text' value='0' readonly='readonly'/></div>");
$out_div.append("<div><span id='HG_pagerNext' style='background-position:-20px -19px' ></span></div>");
$out_div.append("頁  ");
$out_div.append("共<span id='HG_pageCount'>0</span>頁");
$out_div.append("  ");
$out_div.append("每頁<select id='HG_pagerSize'></select>個記錄");
$out_div.append("  ");
$out_div.append("共有 <span id='HG_recordCount'>0</span> 個記錄");
$out_div.append("  ");
$this.append("<br/>");
$this.find("#HG_pagerPre").css("backgroundPosition", "-3px -3px");
$this.find("#HG_pagerNext").css("backgroundPosition", "-20px -3px");
if (options.pageSizes) {
for (var i = 0; i < options.pageSizes.length; i++) {
$("#HG_pagerSize").append("<option>" + options.pageSizes[i] + "</option>");
}
_pagerFields.pageSize = options.pageSizes[0];
}
else {
var default_page_size = [10, 30, 50];
for (var i = 0; i < default_page_size.length; i++) {
$("#HG_pagerSize").append("<option>" + default_page_size[i] + "</option>");
}
_pagerFields.pageSize = default_page_size[0];
}
if (options.selRecord != undefined && !options.selRecord) {
$("#selRecord_div").css("display", 'none');
}
if (options.pagerFuncton) {
_pagerFields.pagerFunction = options.pagerFuncton;
}
if (options.recordCount) {
_pagerFields.recordCount = options.recordCount;
$("#HG_recordCount").text(_pagerFields.recordCount);
_pagerFields.pageCount = _pagerFields.recordCount % _pagerFields.pageSize == 0 ? _pagerFields.recordCount / _pagerFields.pageSize : Math.ceil(_pagerFields.recordCount / _pagerFields.pageSize);
$("#HG_pageCount").text(_pagerFields.pageCount);
_pagerFields.currentPage = 1;
$("#HG_pageNum").val(_pagerFields.currentPage);
setImageButtonSate();
_pagerFields.pagerFunction(_pagerFields.pageSize, _pagerFields.currentPage);
}
// setting element
// bingding event
$("#HG_pagerPre").click(function () {
if (_pagerFields.currentPage <= 1) return;
else _pagerFields.currentPage--;
setImageButtonSate();
$("#HG_pageNum").val(_pagerFields.currentPage);
_pagerFields.pagerFunction(_pagerFields.pageSize, _pagerFields.currentPage);
});
$("#HG_pagerNext").click(function () {
if (_pagerFields.currentPage == _pagerFields.pageCount) return;
else _pagerFields.currentPage++;
setImageButtonSate();
$("#HG_pageNum").val(_pagerFields.currentPage);
_pagerFields.pagerFunction(_pagerFields.pageSize, _pagerFields.currentPage);
});
$("#HG_pagerSize").change(function () {
_pagerFields.pageSize = $this.find("option:selected").text() * 1;
_pagerFields.pageCount = _pagerFields.recordCount % _pagerFields.pageSize == 0 ? _pagerFields.recordCount / _pagerFields.pageSize : Math.ceil(_pagerFields.recordCount / _pagerFields.pageSize);
$("#HG_pageCount").text(_pagerFields.pageCount);
_pagerFields.currentPage = 1;
$("#HG_pageNum").val(1);
setImageButtonSate();
_pagerFields.pagerFunction(_pagerFields.pageSize, _pagerFields.currentPage);
});
});
}

下面是外掛程式的公用函數
複製代碼 代碼如下:

//擷取當前的頁碼
getCurrentPageIndex: function (options) {
return _pagerFields.currentPage;
},
//擷取記錄的總數
getRecordCount: function (options) {
return _pagerFields.recordCount;
},
//擷取當前頁面數量
getCurrentPageCount: function (options) {
return _pagerFields.pageCount;
},
//擷取頁面的大小
getCurrentPageSize: function (options) {
return _pagerFields.pageSize;
}

使用樣本:
複製代碼 代碼如下:

$(function () {
$("#testPager").HGPager2({
pageSizes: [10, 20, 30],
recordCount: 123,
pagerFuncton: function (size, index) {
alert("size: " + size + " index: " + index);
}
});
});
function test_Click() {
alert(
$("#testPager").HGPager2("getCurrentPageIndex")+" "+
$("#testPager").HGPager2("getRecordCount")+" "+
$("#testPager").HGPager2("getCurrentPageCount")+" "+
$("#testPager").HGPager2("getCurrentPageSize")
);
}

由於本人對js範圍的理解不夠透徹,此個外掛程式採用的模式也不知道是否最適合,上述內容如有說錯的,請批評指正。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.