基于bootstrap按鈕式下拉菜單組件的搜索建議插件
本文實(shí)例為大家分享了Bootstrap Search Suggest搜索建議插件的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>Bootstrap 搜索建議插件</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="這是一個(gè)基于 bootstrap 按鈕式下拉菜單組件的搜索建議插件">
<meta name="Keywords" content="Bootstrap Search Suggest,bootstrap,搜索建議插件"/>
<meta name="author" content="lizhiwen@meizu.com">
<link rel="external nofollow" rel="stylesheet">
</head>
<body>
<div class="container">
<h2>bootstrap combox 搜索建議插件</h2>
<p>這是一個(gè)基于 bootstrap 按鈕式下拉菜單組件的搜索建議插件.</p>
<p>使用說明:<a href="README.html" rel="external nofollow" target="_blank">README.html</a> <a rel="external nofollow" target="_blank">Github</a></p>
<form action="index_submit" method="get" accept-charset="utf-8" role="form">
<h3>測試(URL 獲取)</h3>
<p>配置了 data-id,非下拉菜單選擇輸入則背景色警告。</p>
<div class="row">
<div class="col-lg-2">
<div class="input-group">
<input type="text" class="form-control" id="test">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
<!-- /btn-group -->
</div>
</div>
</div>
<h3>測試(URL 獲取)</h3>
<p>不展示下拉菜單按鈕。</p>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" id="testNoBtn">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
<!-- /btn-group -->
</div>
</div>
</div>
<h3>測試(json 數(shù)據(jù)中獲取)</h3>
<p>默認(rèn)啟用空關(guān)鍵字檢索。</p>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" id="test_data">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
<!-- /btn-group -->
</div>
</div>
</div>
<h3>百度搜索</h3>
<p>支持逗號分隔多關(guān)鍵字</p>
<div class="row">
<div class="col-lg-6">
<div class="input-group" style="width: 300px;">
<input type="text" class="form-control" id="baidu">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
<!-- /btn-group -->
</div>
</div>
</div>
<h3>淘寶搜索</h3>
<p>支持逗號分隔多關(guān)鍵字</p>
<div class="row">
<div class="col-lg-6">
<div class="input-group" style="width: 400px;">
<input type="text" class="form-control" id="taobao">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
</ul>
</div>
<!-- /btn-group -->
</div>
</div>
</div>
</form>
</div>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="bootstrap-suggest.js"></script>
<script type="text/javascript">
var testBsSuggest = $("#test").bsSuggest({
//url: "/rest/sys/getuserlist?keyword=",
url: "data.json",
/*effectiveFields: ["userName", "shortAccount"],
searchFields: [ "shortAccount"],
effectiveFieldsAlias:{userName: "姓名"},*/
idField: "userId",
keyField: "userName"
}).on('onDataRequestSuccess', function (e, result) {
console.log('onDataRequestSuccess: ', result);
}).on('onSetSelectValue', function (e, keyword) {
console.log('onSetSelectValue: ', keyword);
}).on('onUnsetSelectValue', function (e) {
console.log("onUnsetSelectValue");
});
/**
* 不顯示下拉按鈕
*/
var testBsSuggest = $("#testNoBtn").bsSuggest({
//url: "/rest/sys/getuserlist?keyword=",
url: "data.json",
/*effectiveFields: ["userName", "shortAccount"],
searchFields: [ "shortAccount"],
effectiveFieldsAlias:{userName: "姓名"},*/
showBtn: false,
idField: "userId",
keyField: "userName"
}).on('onDataRequestSuccess', function (e, result) {
console.log('onDataRequestSuccess: ', result);
}).on('onSetSelectValue', function (e, keyword) {
console.log('onSetSelectValue: ', keyword);
}).on('onUnsetSelectValue', function (e) {
console.log("onUnsetSelectValue");
});
/**
* 從 data參數(shù)中過濾數(shù)據(jù)
*/
var testdataBsSuggest = $("#test_data").bsSuggest({
indexId: 2, //data.value 的第幾個(gè)數(shù)據(jù),作為input輸入框的內(nèi)容
indexKey: 1, //data.value 的第幾個(gè)數(shù)據(jù),作為input輸入框的內(nèi)容
data: {
'value':[
{'id':'0','word':'lzw','description':'http://lzw.me'},
{'id':'1','word':'lzwme','description':'http://w.lzw.me'},
{'id':'2','word':'meizu','description':'http://www.meizu.com'},
{'id':'3','word':'flyme','description':'http://flyme.meizu.com'}
],
'defaults':'http://lzw.me'
}
});
/**
* 百度搜索 API 測試
*/
var baiduBsSuggest = $("#baidu").bsSuggest({
allowNoKeyword: false, //是否允許無關(guān)鍵字時(shí)請求數(shù)據(jù)。為 false 則無輸入時(shí)不執(zhí)行過濾請求
multiWord: true, //以分隔符號分割的多關(guān)鍵字支持
separator: ",", //多關(guān)鍵字支持時(shí)的分隔符,默認(rèn)為空格
getDataMethod: "url", //獲取數(shù)據(jù)的方式,總是從 URL 獲取
url: 'http://unionsug.baidu.com/su?p=3&t='+ (new Date()).getTime() +'&wd=', /*優(yōu)先從url ajax 請求 json 幫助數(shù)據(jù),注意最后一個(gè)參數(shù)為關(guān)鍵字請求參數(shù)*/
jsonp: 'cb', //如果從 url 獲取數(shù)據(jù),并且需要跨域,則該參數(shù)必須設(shè)置
processData: function (json) { // url 獲取數(shù)據(jù)時(shí),對數(shù)據(jù)的處理,作為 getData 的回調(diào)函數(shù)
var i, len, data = {value: []};
if (!json || !json.s || json.s.length === 0) {
return false;
}
console.log(json);
len = json.s.length;
jsonStr = "{'value':[";
for (i = 0; i < len; i++) {
data.value.push({
word: json.s[i]
});
}
data.defaults = 'baidu';
//字符串轉(zhuǎn)化為 js 對象
return data;
}
});
/**
* 淘寶搜索 API 測試
*/
var taobaoBsSuggest = $("#taobao").bsSuggest({
indexId: 2, //data.value 的第幾個(gè)數(shù)據(jù),作為input輸入框的內(nèi)容
indexKey: 1, //data.value 的第幾個(gè)數(shù)據(jù),作為input輸入框的內(nèi)容
allowNoKeyword: false, //是否允許無關(guān)鍵字時(shí)請求數(shù)據(jù)。為 false 則無輸入時(shí)不執(zhí)行過濾請求
multiWord: true, //以分隔符號分割的多關(guān)鍵字支持
separator: ",", //多關(guān)鍵字支持時(shí)的分隔符,默認(rèn)為空格
getDataMethod: "url", //獲取數(shù)據(jù)的方式,總是從 URL 獲取
showHeader: true, //顯示多個(gè)字段的表頭
effectiveFieldsAlias:{Id: "序號", Keyword: "關(guān)鍵字", Count: "數(shù)量"},
url: 'http://suggest.taobao.com/sug?code=utf-8&extras=1&q=', /*優(yōu)先從url ajax 請求 json 幫助數(shù)據(jù),注意最后一個(gè)參數(shù)為關(guān)鍵字請求參數(shù)*/
jsonp: 'callback', //如果從 url 獲取數(shù)據(jù),并且需要跨域,則該參數(shù)必須設(shè)置
processData: function(json){ // url 獲取數(shù)據(jù)時(shí),對數(shù)據(jù)的處理,作為 getData 的回調(diào)函數(shù)
var i, len, data = {value: []};
if(!json || !json.result || json.result.length == 0) {
return false;
}
console.log(json);
len = json.result.length;
for (i = 0; i < len; i++) {
data.value.push({
"Id": (i + 1),
"Keyword": json.result[i][0],
"Count": json.result[i][1]
});
}
console.log(data);
return data;
}
});
$("form").submit(function(e) {
return false;
});
</script>
</body>
</html>
配置參數(shù)
參數(shù)列表中的值均為插件默認(rèn)值
{
url: null, //請求數(shù)據(jù)的 URL 地址
jsonp: null, //設(shè)置此參數(shù)名,將開啟jsonp功能,否則使用json數(shù)據(jù)結(jié)構(gòu)
data: {}, //提示所用的數(shù)據(jù)
getDataMethod: "firstByUrl", //獲取數(shù)據(jù)的方式,url:一直從url請求;data:從 options.data 獲??;firstByUrl:第一次從Url獲取全部數(shù)據(jù),之后從options.data獲取
indexId: 0, //每組數(shù)據(jù)的第幾個(gè)數(shù)據(jù),作為input輸入框的 data-id,設(shè)為 -1 且 idField 為空則不設(shè)置此值
indexKey: 0, //每組數(shù)據(jù)的第幾個(gè)數(shù)據(jù),作為input輸入框的內(nèi)容
idField: "", //每組數(shù)據(jù)的哪個(gè)字段作為 data-id,優(yōu)先級高于 indexId 設(shè)置(推薦)
keyField: "", //每組數(shù)據(jù)的哪個(gè)字段作為輸入框內(nèi)容,優(yōu)先級高于 indexKey 設(shè)置(推薦)
effectiveFields: [], //有效顯示于列表中的字段,非有效字段都會(huì)過濾,默認(rèn)全部,對自定義getData方法無效
effectiveFieldsAlias: {}, //有效字段的別名對象,用于 header 的顯示
searchFields: [], //有效搜索字段,從前端搜索過濾數(shù)據(jù)時(shí)使用。effectiveFields 配置字段也會(huì)用于搜索過濾
showHeader: false, //是否顯示選擇列表的 header,默認(rèn)有效字段大于一列時(shí)顯示,否則不顯示
showBtn: true, //是否顯示下拉按鈕
allowNoKeyword: true, //是否允許無關(guān)鍵字時(shí)請求數(shù)據(jù)
multiWord: false, //以分隔符號分割的多關(guān)鍵字支持
separator: ",", //多關(guān)鍵字支持時(shí)的分隔符,默認(rèn)為半角逗號
processData: processData, //格式化數(shù)據(jù)的方法,返回?cái)?shù)據(jù)格式參考 data 參數(shù)
getData: getData, //獲取數(shù)據(jù)的方法
autoMinWidth: false, //是否自動(dòng)最小寬度,設(shè)為 false 則最小寬度不小于輸入框?qū)挾?
listAlign: "left", //提示列表對齊位置,left/right/auto
inputBgColor: '', //輸入框背景色,當(dāng)與容器背景色不同時(shí),可能需要該項(xiàng)的配置
inputWarnColor: "rgba(255,0,0,.1)", //輸入框內(nèi)容不是下拉列表選擇時(shí)的警告色
listStyle: {
"padding-top":0, "max-height": "375px", "max-width": "800px",
"overflow": "auto", "width": "auto",
"transition": "0.3s", "-webkit-transition": "0.3s", "-moz-transition": "0.3s", "-o-transition": "0.3s"
}, //列表的樣式控制
listHoverStyle: 'background: #07d; color:#fff', //提示框列表鼠標(biāo)懸浮的樣式
listHoverCSS: "jhover", //提示框列表鼠標(biāo)懸浮的樣式名稱
keyLeft: 37, //向左方向鍵
keyUp: 38, //向上方向鍵
keyRight: 39, //向右方向鍵
keyDown: 40, //向下方向鍵
keyEnter: 13 //回車鍵
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript實(shí)現(xiàn)自由編輯圖片代碼詳解
這篇文章主要介紹了javascript實(shí)現(xiàn)自由編輯圖片代碼詳解,在當(dāng)下的的前端項(xiàng)目中,圖片功能可以說是非常常見的,圖片的展示、圖片的裁剪編輯、圖片的上傳等,那么我們的項(xiàng)目便來了個(gè)需求。,需要的朋友可以參考下2019-06-06
layui前端時(shí)間戳轉(zhuǎn)化實(shí)例
今天小編就為大家分享一篇layui前端時(shí)間戳轉(zhuǎn)化實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
JS中利用localStorage防止頁面動(dòng)態(tài)添加數(shù)據(jù)刷新后數(shù)據(jù)丟失
本文給大家分享一段js代碼利用利用localStorage防止頁面動(dòng)態(tài)添加數(shù)據(jù)刷新后數(shù)據(jù)丟失問題,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
javascript 數(shù)組排序與對象排序的實(shí)例
這篇文章主要介紹了javascript 數(shù)組排序與對象排序的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07
基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

