瑕佹兂緙栧啓涓涓狫query鎻掍歡錛岄鍏堝繀欏誨Jquery鏈夋墍浜?jiǎn)瑙eQ屽挨鍏舵槸浠ヤ笅鍑犱釜鐭ヨ瘑鐐癸細(xì)
鏈変簡(jiǎn)浠ヤ笂鐭ヨ瘑錛屾垜浠潵瀹炵幇涓涓猄elect鐨凧query鎻掍歡錛屽彲浠ユ柟渚跨殑鍒涘緩銆佹坊鍔犻夐」銆佸垹闄ら夐」絳夌瓑銆備唬鐮佸涓嬶細(xì)
/**
* Created by gavinli on 17-3-30.
*/
;(function ($) {
'use strict';
var pluginName = 'myList';
var _default = {};
_default.settings = {};
var MyList = function (element, options) {
this.$element = $(element);
this.init(options);
return {
init: $.proxy(this.init, this),
add: $.proxy(this.add, this),
remove: $.proxy(this.remove, this),
list: $.proxy(this.list, this),
clear: $.proxy(this.clear, this),
getSelected: $.proxy(this.getSelected, this)
}
};
MyList.prototype.init = function (options) {
this.items = [];
if (options.data) {
if (typeof options.data === 'string') {
options.data = $.parseJSON(options.data);
}
this.items = $.extend(true, [], options.data);
delete options.data;
}
this.options = $.extend({}, _default.settings, options);
this.render();
this.subscribeEvents();
};
MyList.prototype.subscribeEvents = function () {
//TODO:
};
MyList.prototype.add = function (items) {
if (!(items instanceof Array)) {
items = [items];
}
var _this = this;
$.each(items, function (i, value) {
_this.items.push(value);
});
this.filterDup();
this.render();
}
//Remove all duplicated items
MyList.prototype.filterDup = function () {
var _this = this;
var values = {}
$.each(_this.items, function (i, value) {
if (values[value]) {
_this.items[i] = null;
} else {
values[value] = true;
}
});
}
MyList.prototype.remove = function (items) {
var _this = this;
var toBeRemoved = {};
$.each(items, function (i, value) {
toBeRemoved[value] = true;
});
$.each(_this.items, function (i, value) {
if (toBeRemoved[value] == true) {
_this.items[i] = null;
}
});
this.render();
}
MyList.prototype.getSelected = function () {
return this.$wrapper.val();
}
MyList.prototype.list = function (item) {
var result = [];
$.each(this.items, function (i, value) {
if (value) {
result.push(value);
}
});
return result;
}
//Clear all items
MyList.prototype.clear = function () {
delete this.items;
this.items = [];
this.render();
}
MyList.prototype.render = function () {
if (!this.initialized) {
this.$wrapper = $(this.template.list);
this.initialized = true;
}
//Append select element to $element
this.$element.empty().append(this.$wrapper.empty());
//Build select options
this.buildList(this.items);
}
MyList.prototype.buildList = function (items) {
var _this = this;
$.each(items, function (i, value) {
if (value) {
var option = $(_this.template.item);
option.append(value);
_this.$wrapper.append(option);
}
});
}
MyList.prototype.template = {
list: '<select multiple class="form-control"></select>',
item: '<option></option>'
};
$.fn[pluginName] = function (options, args) {
var result;
this.each(function () {
var _this = $.data(this, pluginName);
if (typeof options === 'string') {
if (!_this) {
//logError('Not initialized, can not call method : ' + options);
}
else if (!$.isFunction(_this[options]) || options.charAt(0) === '_') {
//logError('No such method : ' + options);
}
else {
if (!(args instanceof Array)) {
args = [args];
}
result = _this[options].apply(_this, args);
}
}
else if (typeof options === 'boolean') {
result = _this;
}
else {
$.data(this, pluginName, new MyList(this, $.extend(true, {}, options)));
}
});
return result || this;
};
})(jQuery);
涓嬮潰閽堝鍏朵腑鐨勫叧閿柟娉曡繘琛屽垎鏋愯瑙o細(xì)
MyList鍑芥暟錛歁yList瀵硅薄鐨刢onstrunctor鏂規(guī)硶錛屾帴鍙梠ptions鍙傛暟錛坥ptions鍙傛暟鍖呭惈鎵鏈塷ptions鐨勬暟緇勶級(jí)
MyList.prototype.init錛氭牴鎹畂ptions鐨刣ata鏋勫緩騫舵覆鏌揝elect鎺т歡
MyList.prototype.add錛氭坊鍔爋ption鍒癝elect涓茍娓叉煋錛屽叾瀹價(jià)emove錛宭ist錛実etSelected鏂規(guī)硶澶у鑷鐮旂┒
涓婇潰浠g爜涓紝鏈鏍稿績(jī)鐨勯儴鍒嗗湪浜庡浣曞皢MyList瀵硅薄鍜孌om鍏冪礌緇撳悎銆佸茍涓旀墿灞曞埌Jquery涓紝鍏蜂綋鍙傝冨涓嬫敞閲婁唬鐮侊細(xì)
////鎵╁睍jQuery鐨刾rototype瀵硅薄錛岃繖閲岀殑plugName絳変簬myList錛岀浉褰撲簬緇檍Query瀵硅薄娣誨姞浜?jiǎn)涓涓?myList"鏂規(guī)硶
$.fn[pluginName] = function (options, args) {
var result;
////榪欓噷鐨則his鏄竴涓猨Query瀵硅薄
this.each(function () {
//涓嬮潰鐨則his涓嶆槸jQuery瀵硅薄錛岃屾槸jQuery瀵硅薄涓殑Dom瀵硅薄
//浠嶥om瀵硅薄涓幏鍙?data-myList"灞炴х粦瀹氱殑瀵硅薄
var _this = $.data(this, pluginName);
//options鏄柟娉曞悕錛屼緥濡?('#list1').MyList('add'錛孾])錛岃繖閲岀殑options絳変簬'add'
if (typeof options === 'string') {
if (!_this) {
//logError('Not initialized, can not call method : ' + options);
}
else if (!$.isFunction(_this[options]) || options.charAt(0) === '_') {
//logError('No such method : ' + options);
}
else {
if (!(args instanceof Array)) {
args = [args];
}
//璋冪敤MyList瀵硅薄鐨勬柟娉?/span>
result = _this[options].apply(_this, args);
}
}
else if (typeof options === 'boolean') {
result = _this;
}
else {
//鍒涘緩MyList瀵硅薄騫剁粦瀹氬埌Dom瀵硅薄鐨刣ata-myList灞炴?/span>
$.data(this, pluginName, new MyList(this, $.extend(true, {}, options)));
}
});
return result || this;
};
濡備綍浣跨敤璇ユ彃浠剁殑鏂規(guī)硶濡備笅鎵紺猴細(xì)
棣栧厛鍦╤tml涓畾涔変竴涓?lt;div id="list1"></div>錛岀劧鍚庤繖鏍蜂嬌鐢ㄥ畠錛?br />
//鍒涘緩涓涓猄elect鍖呭惈涓変釜options
$('#list1').MyList(['Tom','Mary','Alice']);
//娣誨姞鏂扮殑option
$('#list1').MyList('add', [['James','Richard']]);
//鍒犻櫎option
$('#list1').MyList('remove', [['Alice']]);
鏈鍚庢垜浠彲浠ュ湪浠ヤ笂渚嬪瓙涓彂鐜板垱寤簀Query鎻掍歡鐨勬諱綋鎬濊礬錛?br />
- 鑷畾涔夊璞★紝瀵硅薄涓寘鍚暟鎹拰jQuery瀵硅薄鏈韓
- 瀹氫箟瀵硅薄鐨勬柟娉曪紝騫朵笖鏍規(guī)嵁瀵硅薄涓暟鎹殑鍙樺寲娓叉煋Dom瀵硅薄錛堥氳繃jQuery瀵硅薄鑾峰緱Dom瀵硅薄錛?/li>
- 灝嗚鑷畾涔夊璞℃柟娉曟墿灞曞埌jQuery鍘熷瀷瀵硅薄涓?/li>
- 鍒涘緩鑷畾涔夊璞★紝騫剁粦瀹氬埌jQuery涓璂om瀵硅薄鐨刣ata灞炴?br />
閫氳繃浠ヤ笂瀹炵幇錛屾垜浠究鍙互鍍忎嬌鐢╦Query瀵硅薄涓鏍風(fēng)殑鏂瑰紡浣跨敤鎺т歡錛屽睆钄藉Dom鍏冪礌鐨勬搷浣滐紝綆鍗曞張鏂逛究
]]>