jQuery Chosen通用初始化
一直在用Chosen這個js插件,其目的就是美化下拉框。github地址:https://harvesthq.github.io/chosen/
no_results_text:"xxxxx"無搜索結果時顯示的文本
allow_single_deselect:true 是否允許取消選擇
disable_search: true 是否有搜索框出現(xiàn)
max_selected_options:當select為多選時,最多選擇個數(shù)
/* 功能: Chosen通用初始化
* 創(chuàng)建人:Brian 創(chuàng)建時間:2016-12-13
*/
(function ($j) {
var chosenTool = function (el, options) {
this.opts = options;
this.chosenInit();
this.chose_get_init();
this.chose_mult_set_init(this.opts.hidClassName);
this.clickEvent();
return this;
}
chosenTool.opts = {
selectId: '',//selectId
hidClassName: '',//隱藏域Class
placeholdertxt: '',//select中placeholder文字
noresulttxt: '',//輸入的名稱未查到時顯示的文字
dataJson: ''//數(shù)據源
};
$j.fn.myChosenTool = function (opt) {
var value,
args = Array.prototype.slice.call(arguments, 1);
var $jthis = $j(this),
data = $jthis.data('chosenTool'),
options = $j.extend({}, chosenTool.opts, $jthis.data(),
typeof option === 'object' && option);
if (typeof option === 'string') {
//判斷用戶調用的方法是否存在
//if ($j.inArray(option, allowedMethods) < 0) {
// throw new Error("Unknown method: " + option);
//}
if (!data) {
return;
}
value = data[option].apply(data, args);
if (option === 'destroy') {
$jthis.removeData('chosenTool');
}
}
/*插件外部調用插件內部的方法需要修改成下面形式*/
//if (typeof opt === 'string') {
// if (!data) {
// return;
// }
// value = data[opt].apply(data, args);
// if (opt === 'destroy') {
// $jthis.removeData('chosenTool');
// }
//}
if (!data) {
opt["selectId"] = $j(this).attr("id");
$jthis.data('chosenTool', (data = new chosenTool(this, opt)));
}
console.log(data);
return typeof value === 'undefined' ? this : value;
};
chosenTool.prototype.clickEvent = function () {
var _this = this;
$j("#" + this.opts.selectId).on("change", function () {
_this.chose_get_value();
});
};
/*下拉框初始化方法*/
chosenTool.prototype.selectInfoInit = function () {
var proOPts = "";
this.opts.dataJson = $j.parseJSON(this.opts.dataJson);
$j.each(this.opts.dataJson, function (index, item) {
proOPts += "<option value='" + item.ValueField + "'>" + item.TextField + "</option>";
});
$j("#" + this.opts.selectId).append(proOPts);
//初始化chosen
$j("#" + this.opts.selectId).chosen({
allow_single_deselect: true, //是否允許取消選擇
placeholder_text_multiple: this.opts.placeholdertxt, //多選框沒有選中任何值的時候 顯示的文字
no_results_text: this.opts.noresulttxt,//無搜索結果時顯示的文本
search_contains: true//是否支持模糊搜索
});
};
/*對象初始化方法*/
chosenTool.prototype.chosenInit = function () {
this.selectInfoInit();
};
//私有方法,檢測參數(shù)是否合法
chosenTool.prototype.isValid = function () {
return !this.options || (this.options && typeof this.options === "object") ? true : false;
};
//數(shù)據同步
chosenTool.prototype.chose_get_init = function () {
var selectId = this.opts.selectId;
$j("#" + this.opts.selectId).chosen().change(
function () {
$j("#" + selectId).trigger("liszt:updated");//更新下拉框
});
};
//單選select value獲取
chosenTool.prototype.chose_get_value = function () {
var selectVal = $j("#" + this.opts.selectId).val();
$j("." + this.opts.hidClassName).val(selectVal);
};
//單選select value獲取
chosenTool.prototype.chose_mult_set_init = function () {
var values = $j("." + this.opts.hidClassName).val();
if (values && values.indexOf(',') > 0) {
var arr = values.split(',');
var length = arr.length;
var value = '';
for (i = 0; i < length; i++) {
value = arr[i];
$j("#" + this.opts.selectId + " [value='" + value + "']").attr('selected', 'selected');
}
} else {
$j("#" + this.opts.selectId + " [value='" + values + "']").attr('selected', 'selected');
}
$j("#" + this.opts.selectId).trigger("liszt:updated");
};
//select text獲取,多選時請注意
chosenTool.prototype.chose_get_text = function () {
return $j("#" + this.opts.selectId + " option:selected").text();
};
})(jQuery);
以上所述是小編給大家介紹的jQuery Chosen通用初始化,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
jQuery去掉字符串起始和結尾的空格(多種方法實現(xiàn))
去掉字符串起始和結尾的空格在實際應用中時很常見的的功能,本教程以多種方法為大家介紹下去掉空格的方法,感興趣的朋友可以參考下哈2013-04-04
jQuery圖片旋轉插件jQueryRotate.js用法實例(附demo下載)
這篇文章主要介紹了jQuery圖片旋轉插件jQueryRotate.js用法,結合實例形式分析了圖片旋轉插件jQueryRotate.js用法,并附帶了demo示例代碼供讀者下載,需要的朋友可以參考下2016-01-01
jQuery中(function($){})(jQuery)詳解
本文通過具體示例向大家詳細介紹了jQuery中的(function($){})(jQuery)的用法和意義,對此有相同困惑的小伙伴可以參考下本文。2015-07-07
jQuery插件FusionWidgets實現(xiàn)的Cylinder圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件FusionWidgets實現(xiàn)的Cylinder圖效果,結合實例形式分析了jQuery使用FusionWidgets結合swf文件讀取xml數(shù)據并采用Cylinder圖展示的相關實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03

