JQuery WEUI Select 組件增加搜索欄示例demo
JQuery WEUI Select 組件增加搜索欄
1.Demo

2.修改源碼
/* global $:true */
+ function($) {
"use strict";
var defaults;
var selects = [];
var Select = function(input, config) {
var self = this;
this.config = config;
//init empty data
this.data = {
values: '',
titles: '',
origins: [],
length: 0
};
this.$input = $(input);
if(config.readOnly != false ){
this.$input.prop("readOnly", true);
}
this.initConfig();
config = this.config;
this.$input.click($.proxy(this.open, this));
selects.push(this)
}
Select.prototype.initConfig = function() {
this.config = $.extend({}, defaults, this.config);
var config = this.config;
if(!config.items || !config.items.length) return;
config.items = config.items.map(function(d, i) {
if(typeof d == typeof "a") {
return {
title: d,
value: d
};
}
return d;
});
this.tpl = $.t7.compile("<div app_gn='select' style='min-height:300px !important;' class='weui-picker-modal weui-select-modal'>" + config.toolbarTemplate + (config.search?config.searchTemplate:"") + (config.multi ? config.checkboxTemplate : config.radioTemplate) + "</div>");
if(config.input !== undefined) this.$input.val(config.input);
this.parseInitValue();
this._init = true;
}
Select.prototype.updateInputValue = function(values, titles) {
var v, t;
if(this.config.multi) {
v = values.join(this.config.split);
t = titles.join(this.config.split);
} else {
v = values[0];
t = titles[0];
}
//caculate origin data
var origins = [];
this.config.items.forEach(function(d) {
values.each(function(i, dd) {
if(d.value == dd) origins.push(d);
});
});
this.$input.val(t).data("values", v);
this.$input.attr("value", t).attr("data-values", v);
var data = {
values: v,
titles: t,
valuesArray: values,
titlesArray: titles,
origins: origins,
length: origins.length
};
this.data = data;
this.$input.trigger("change", data);
this.config.onChange && this.config.onChange.call(this, data);
}
Select.prototype.parseInitValue = function() {
var value = this.$input.val();
var items = this.config.items;
//如果input為空,只有在第一次初始化的時(shí)候才保留默認(rèn)選擇。因?yàn)楹髞砭褪怯脩糇约喝∠巳窟x擇,不能再為他選中默認(rèn)值。
if( !this._init && (value === undefined || value == null || value === "")) return;
var titles = this.config.multi ? value.split(this.config.split) : [value];
for(var i=0;i<items.length;i++) {
items[i].checked = false;
for(var j=0;j<titles.length;j++) {
if(items[i].title === titles[j]) {
items[i].checked = true;
}
}
}
}
Select.prototype._bind = function(dialog) {
var self = this,
config = this.config;
dialog.on("change", function(e) {
var checked = dialog.find("input:checked");
var values = checked.map(function() {
return $(this).val();
});
var titles = checked.map(function() {
return $(this).data("title");
});
self.updateInputValue(values, titles);
if(config.autoClose && !config.multi) self.close();
})
.trigger('change')
.on("click", ".close-select", function() {
self.close();
});
}
//更新數(shù)據(jù)
Select.prototype.update = function(config) {
this.config = $.extend({}, this.config, config);
this.initConfig();
if(this._open) {
this._bind($.updatePicker(this.getHTML()));
}
}
Select.prototype.open = function(values, titles) {
if(this._open) return;
// open picker 會(huì)默認(rèn)關(guān)掉其他的,但是 onClose 不會(huì)被調(diào)用,所以這里先關(guān)掉其他select
for (var i = 0; i < selects.length; i++ ) {
var s = selects[i];
if (s === this) continue;
if (s._open) {
if(!s.close()) return false; // 其他的select由于某些條件限制關(guān)閉失敗。
}
}
this.parseInitValue();
var config = this.config;
var dialog = this.dialog = $.openPicker(this.getHTML());
this._bind(dialog);
this._open = true;
if(config.onOpen) config.onOpen(this);
}
Select.prototype.close = function(callback, force) {
if (!this._open) return false;
var self = this,
beforeClose = this.config.beforeClose;
if(typeof callback === typeof true) {
force === callback;
}
if(!force) {
if(beforeClose && typeof beforeClose === 'function' && beforeClose.call(this, this.data.values, this.data.titles) === false) {
return false
}
if(this.config.multi) {
if(this.config.min !== undefined && this.data.length < this.config.min) {
$.toast("請(qǐng)至少選擇"+this.config.min+"個(gè)", "text");
return false
}
if(this.config.max !== undefined && this.data.length > this.config.max) {
$.toast("最多只能選擇"+this.config.max+"個(gè)", "text");
return false
}
}
}
$.closePicker(function() {
self.onClose();
callback && callback();
});
return true
}
Select.prototype.onClose = function() {
this._open = false;
if(this.config.onClose) this.config.onClose(this);
}
Select.prototype.getHTML = function(callback) {
var config = this.config;
return this.tpl({
items: config.items,
title: config.title,
closeText: config.closeText
})
}
$.fn.select = function(params, args) {
return this.each(function() {
var $this = $(this);
if(!$this.data("weui-select")) $this.data("weui-select", new Select(this, params));
var select = $this.data("weui-select");
if(typeof params === typeof "a") select[params].call(select, args);
return select;
});
}
defaults = $.fn.select.prototype.defaults = {
items: [],
input: undefined, //輸入框的初始值
title: "請(qǐng)選擇",
multi: false,
closeText: "確定",
autoClose: true, //是否選擇完成后自動(dòng)關(guān)閉,只有單選模式下才有效
onChange: undefined, //function
beforeClose: undefined, // function 關(guān)閉之前,如果返回false則阻止關(guān)閉
onClose: undefined, //function
onOpen: undefined, //function
split: ",", //多選模式下的分隔符
min: undefined, //多選模式下可用,最少選擇數(shù)
max: undefined, //單選模式下可用,最多選擇數(shù)
search: false, // 搜索,默認(rèn) 不開啟
readOnly:true, // 只讀
// 標(biāo)題
toolbarTemplate: '<div class="toolbar">\
<div class="toolbar-inner">\
<a href="javascript:;" rel="external nofollow" class="picker-button close-select">{{closeText}}</a>\
<h1 class="title">{{title}}</h1>\
</div>\
</div>',
searchTemplate: '<div class="weui-search-bar">\
<form class="weui-search-bar__form">\
<div class="weui-search-bar__box">\
<i class="weui-icon-search"></i>\
<input type="search" class="weui-search-bar__input" id="select_search_input" placeholder="搜索" required="">\
<a href="javascript:" rel="external nofollow" rel="external nofollow" class="weui-icon-clear" id="select_search_clear"></a>\
</div>\
<label class="weui-search-bar__label">\
<i class="weui-icon-search"></i>\
<span>搜索</span>\
</label>\
</form>\
<a href="javascript:" rel="external nofollow" rel="external nofollow" class="weui-search-bar__cancel-btn" id="select_search_cancel">取消</a>\
</div>',
radioTemplate:
'<div app_gn="cells" class="weui-cells weui-cells_radio">\
{{#items}}\
<label app_gn="cell" class="weui-cell weui-check_label" for="weui-select-id-{{this.title}}">\
<div class="weui-cell__bd weui-cell_primary">\
<p>{{this.title}}</p>\
</div>\
<div class="weui-cell__ft">\
<input type="radio" class="weui-check" name="weui-select" id="weui-select-id-{{this.title}}" value="{{this.value}}" {{#if this.checked}}checked="checked"{{/if}} data-title="{{this.title}}">\
<span class="weui-icon-checked"></span>\
</div>\
</label>\
{{/items}}\
</div>',
checkboxTemplate:
'<div app_gn="cells" class="weui-cells weui-cells_checkbox">\
{{#items}}\
<label app_gn="cell" class="weui-cell weui-check_label" for="weui-select-id-{{this.title}}">\
<div class="weui-cell__bd weui-cell_primary">\
<p>{{this.title}}</p>\
</div>\
<div class="weui-cell__ft">\
<input type="checkbox" class="weui-check" name="weui-select" id="weui-select-id-{{this.title}}" value="{{this.value}}" {{#if this.checked}}checked="checked"{{/if}} data-title="{{this.title}}" >\
<span class="weui-icon-checked"></span>\
</div>\
</label>\
{{/items}}\
</div>',
}
}($);
// 給 搜索框添加輸監(jiān)聽事件
$(document).on('input',"#select_search_input", function(){
select_search_weui(this) ;
}).on('click','#select_search_clear,#select_search_cancel',function(){
select_search_weui(this) ;
}) ;
function select_search_weui(obj){
var body = $(obj).closest("div[app_gn='select']") ;
var search_val = $(obj).val().trim() ;
var cells = $(body).find("label[app_gn='cell']") ;
for(var i=0,len=cells.length;i<len;i++){
var text = $(cells[i]).text().trim() ;
if(text.indexOf(search_val) > -1 ){
$(cells[i]).css("display","") ;
}else{
$(cells[i]).css("display","none") ;
}
}
}到此這篇關(guān)于JQuery WEUI Select 組件增加搜索欄的文章就介紹到這了,更多相關(guān)JQuery WEUI 搜索欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery驗(yàn)證手機(jī)號(hào)碼、郵箱格式是否正確示例代碼
本文為大家介紹下使用jquery驗(yàn)證郵箱、驗(yàn)證手機(jī)號(hào)碼,具體實(shí)現(xiàn)思路及代碼如下,感興趣的朋友可以學(xué)習(xí)下2013-07-07
jQuery新的事件綁定機(jī)制on()示例應(yīng)用
從jQuery1.7開始,jQuery引入了全新的事件綁定機(jī)制,on()和off()兩個(gè)函數(shù)統(tǒng)一處理事件綁定,下面通過示例為大家介紹下2014-07-07
jquery插件方式實(shí)現(xiàn)table查詢功能的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨query插件方式實(shí)現(xiàn)table查詢功能的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
jquery select操作的日期聯(lián)動(dòng)實(shí)現(xiàn)代碼
是很簡(jiǎn)單的代碼 不過我自己操作的時(shí)候才發(fā)現(xiàn)我自己還有很多不懂,要多實(shí)際應(yīng)用才發(fā)現(xiàn)問題,哎~~2009-12-12
jQuery Animation實(shí)現(xiàn)CSS3動(dòng)畫示例介紹
jQuery Animation的工作原理是通過將元素的CSS樣式從一個(gè)狀態(tài)改變?yōu)榱硪粋€(gè)狀態(tài),下面以一個(gè)實(shí)例為大家詳細(xì)介紹下具體的實(shí)現(xiàn),感興趣的朋友可以參考下2013-08-08
淺談jQuery this和$(this)的區(qū)別及獲取$(this)子元素對(duì)象的方法
下面小編就為大家?guī)硪黄獪\談jQuery this和$(this)的區(qū)別及獲取$(this)子元素對(duì)象的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
jQuery解析與處理服務(wù)器端返回xml格式數(shù)據(jù)的方法詳解
這篇文章主要介紹了jQuery解析與處理服務(wù)器端返回xml格式數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了jQuery基于ajax的get方法與后臺(tái)交互操作xml格式數(shù)據(jù)的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-07-07
JQuery防止退格鍵網(wǎng)頁后退的實(shí)現(xiàn)代碼
有時(shí)我們需要防止退格鍵的網(wǎng)頁后退,一般情況下最好不要這么用,因?yàn)閷?duì)UX體驗(yàn)不好2012-03-03

