123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <!--360浏览器优先以webkit内核解析-->
- <title>H+ 后台主题UI框架 - 搜索建议</title>
- <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.7" rel="stylesheet">
- <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
- <link href="css/animate.css" rel="stylesheet">
- <link href="css/style.css?v=4.1.0" rel="stylesheet">
- </head>
- <body class="gray-bg">
- <div class="wrapper wrapper-content">
- <div class="row">
- <div class="col-sm-6">
- <div class="ibox float-e-margins">
- <div class="ibox-title">
- <h5>combox 搜索建议插件</h5>
- <div class="ibox-tools">
- <a class="collapse-link">
- <i class="fa fa-chevron-up"></i>
- </a>
- <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
- <i class="fa fa-wrench"></i>
- </a>
- <ul class="dropdown-menu dropdown-user">
- <li><a href="buttons.html#">选项1</a>
- </li>
- <li><a href="buttons.html#">选项2</a>
- </li>
- </ul>
- <a class="close-link">
- <i class="fa fa-times"></i>
- </a>
- </div>
- </div>
- <div class="ibox-content">
- <p>这是一个基于 bootstrap 按钮式下拉菜单组件的搜索建议插件.</p>
- <p>使用说明:<a href="https://github.com/lzwme/bootstrap-suggest-plugin" target="_blank">https://github.com/lzwme/bootstrap-suggest-plugin</a>
- </p>
- <form action="index_submit" method="get" accept-charset="utf-8" role="form">
- <hr>
- <h3>测试(URL 获取)</h3>
- <p>配置了 data-id,非下拉菜单选择输入则背景色警告。</p>
- <div class="row">
- <div class="col-lg-6">
- <div class="input-group">
- <input type="text" class="form-control" id="test">
- <div class="input-group-btn">
- <button type="button" class="btn btn-white 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>
- <hr>
- <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-white 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>
- <hr>
- <h3>测试(json 数据中获取)</h3>
- <p>默认启用空关键字检索。</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-white 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>
- <hr>
- <h3>百度搜索</h3>
- <p>支持逗号分隔多关键字</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-white 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>
- <hr>
- <h3>淘宝搜索</h3>
- <p>支持逗号分隔多关键字</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-white 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>
- </div>
- </div>
- <div class="col-sm-6">
- <div class="ibox float-e-margins">
- <div class="ibox-title">
- <h5>使用说明</h5>
- <div class="ibox-tools">
- <a class="collapse-link">
- <i class="fa fa-chevron-up"></i>
- </a>
- <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
- <i class="fa fa-wrench"></i>
- </a>
- <ul class="dropdown-menu dropdown-user">
- <li><a href="buttons.html#">选项1</a>
- </li>
- <li><a href="buttons.html#">选项2</a>
- </li>
- </ul>
- <a class="close-link">
- <i class="fa fa-times"></i>
- </a>
- </div>
- </div>
- <div class="ibox-content">
- <article class="markdown-body entry-content" itemprop="mainContentOfPage">
- <h1><a id="user-content-bootstrap-search-suggest" class="anchor" href="#bootstrap-search-suggest" aria-hidden="true"><span class="octicon octicon-link"></span></a>Bootstrap Search Suggest</h1>
- <p>这是一个基于 bootstrap 按钮式下拉菜单组件的搜索建议插件,必须使用于按钮式下拉菜单组件上。</p>
- <h2>功能说明</h2>
- <ul>
- <li>搜索方式:从 data.value 的有效字段数据中查询 keyword 的出现,或字段数据包含于 keyword 中</li>
- <li>支持单关键字、多关键字的输入搜索建议,多关键字可自定义分隔符</li>
- <li>支持按 data 数组数据搜索、按 URL 请求搜索和按首次请求URL数据并缓存搜索三种方式【getDataMethod】</li>
- <li>单关键字会设置 data-id 和输入框内容两个值,以 indexId/idField 和 indexKey/idFiled 取值 data 的数据为准;多关键字只设置输入框值</li>
- </ul>
- <h2>快速上手</h2>
- <ol>
- <li>引入 jQuery、bootstrap.min.css、bootstrap.min.js</li>
- <li>引入插件js: <code>bootstrap-suggest.min.js</code>
- </li>
- <li>初始化插件</li>
- </ol>
- <pre>var bsSuggest = $("input#test").bsSuggest({
- url: "/rest/sys/getuserlist?keyword="
- })</pre>
- <p>具体使用请参考参数配置说明及demo 示例页面源码(<a href="http://lzw.me/pages/demo/bootstrap-suggest-plugin">Demo</a>)</p>
- <h4>方法调用</h4>
- <ol>
- <li>禁用提示: <code>$("input#test").bsSuggest("disable");</code>
- </li>
- <li>启用提示: <code>$("input#test").bsSuggest("enable");</code>
- </li>
- <li>销毁插件: <code>$("input#test").bsSuggest("destroy");</code>
- </li>
- </ol>
- <h4><a class="anchor"><span class="octicon octicon-link"></span></a>事件监听</h4>
- <p>dataRequestSuccess: 当 AJAX 请求数据成功时触发,并传回结果到第二个参数</p>
- <pre>$("input#test").on("dataRequestSuccess", function (event, result) {
- console.log(result);
- });</pre>
- <p>onSetSelectValue:当从下拉菜单选取值时触发,并传回设置的数据到第二个参数 onUnsetSelectValue:当设置了 idField,且自由输入内容时触发(与背景警告色显示同步)</p>
- <h2>配置参数</h2>
- <p>参数列表中的值均为插件默认值</p>
- <pre>{
- url: null, //请求数据的 URL 地址
- jsonp: null, //设置此参数名,将开启jsonp功能,否则使用json数据结构
- data: {}, //提示所用的数据
- getDataMethod: "firstByUrl", //获取数据的方式,url:一直从url请求;data:从 options.data 获取;firstByUrl:第一次从Url获取全部数据,之后从options.data获取
- indexId: 0, //每组数据的第几个数据,作为input输入框的 data-id,设为 -1 且 idField 为空则不设置此值
- indexKey: 0, //每组数据的第几个数据,作为input输入框的内容
- idField: "", //每组数据的哪个字段作为 data-id,优先级高于 indexId 设置(推荐)
- keyField: "", //每组数据的哪个字段作为输入框内容,优先级高于 indexKey 设置(推荐)
- effectiveFields: [], //有效显示于列表中的字段,非有效字段都会过滤,默认全部,对自定义getData方法无效
- effectiveFieldsAlias: {}, //有效字段的别名对象,用于 header 的显示
- searchFields: [], //有效搜索字段,从前端搜索过滤数据时使用。effectiveFields 配置字段也会用于搜索过滤
- showHeader: false, //是否显示选择列表的 header,默认有效字段大于一列时显示,否则不显示
- showBtn: true, //是否显示下拉按钮
- allowNoKeyword: true, //是否允许无关键字时请求数据
- multiWord: false, //以分隔符号分割的多关键字支持
- separator: ",", //多关键字支持时的分隔符,默认为半角逗号
- processData: processData, //格式化数据的方法,返回数据格式参考 data 参数
- getData: getData, //获取数据的方法
- autoMinWidth: false, //是否自动最小宽度,设为 false 则最小宽度不小于输入框宽度
- listAlign: "left", //提示列表对齐位置,left/right/auto
- inputWarnColor: "rgba(255,0,0,.1)", //输入框内容不是下拉列表选择时的警告色
- listStyle: {
- "padding-top":0, "max-height": "375px", "max-width": "800px",
- "overflow": "auto", "width": "auto",
- "transition": "0.5s", "-webkit-transition": "0.5s", "-moz-transition": "0.5s", "-o-transition": "0.5s"
- }, //列表的样式控制
- listHoverStyle: 'background: #07d; color:#fff', //提示框列表鼠标悬浮的样式
- listHoverCSS: "jhover", //提示框列表鼠标悬浮的样式名称
- keyLeft: 37, //向左方向键
- keyUp: 38, //向上方向键
- keyRight: 39, //向右方向键
- keyDown: 40, //向下方向键
- keyEnter: 13 //回车键
- }</pre>
- <h2>使用示例</h2>
- <p>请参考 index.html 文件源代码,提供了自定义数据、URL 请求数据、百度搜索API、淘宝搜索API的接口演示。</p>
- <h2>二次开发</h2>
- <ol>
- <li><code>npm install grunt-cli -g</code>
- </li>
- <li><code>npm install</code>
- </li>
- <li><code>grunt watch</code>
- </li>
- </ol>
- <h2><a id="user-content-note-on-patches--pull-requests" class="anchor" href="#note-on-patches--pull-requests" aria-hidden="true"><span class="octicon octicon-link"></span></a>Note on Patches / Pull Requests</h2>
- <ul>
- <li>Fork the project.</li>
- <li>Make your feature addition or bug fix.</li>
- <li>Send me a pull request. Bonus points for topic branches.</li>
- </ul>
- <h2><a id="user-content-设计制作" class="anchor" href="#设计制作" aria-hidden="true"><span class="octicon octicon-link"></span></a>设计制作</h2>
- <p>该插件由<a href="http://lzw.me" target="_blank">志文工作室</a>开发和维护.</p>
- </article>
- </div>
- </div>
- </div>
- </div>
- </div>
- <!-- 全局js -->
- <script src="js/jquery.min.js?v=2.1.4"></script>
- <script src="js/bootstrap.min.js?v=3.3.7"></script>
- <!-- 自定义js -->
- <script src="js/content.js?v=1.0.0"></script>
- <script src="js/plugins/suggest/bootstrap-suggest.min.js"></script>
- <script type="text/javascript">
- var testBsSuggest = $("#test").bsSuggest({
- url: "js/plugins/suggest/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: "js/plugins/suggest/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 数据中获取
- var testdataBsSuggest = $("#test_data").bsSuggest({
- indexId: 2, //data.value 的第几个数据,作为input输入框的内容
- indexKey: 1, //data.value 的第几个数据,作为input输入框的内容
- 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'
- }
- });
- //百度搜索测试
- var baiduBsSuggest = $("#baidu").bsSuggest({
- allowNoKeyword: false, //是否允许无关键字时请求数据
- multiWord: true, //以分隔符号分割的多关键字支持
- separator: ",", //多关键字支持时的分隔符,默认为空格
- getDataMethod: "url", //获取数据的方式,总是从 URL 获取
- url: 'http://unionsug.baidu.com/su?p=3&t=' + (new Date()).getTime() + '&wd=',
- /*优先从url ajax 请求 json 帮助数据,注意最后一个参数为关键字请求参数*/
- jsonp: 'cb',
- /*如果从 url 获取数据,并且需要跨域,则该参数必须设置*/
- processData: function (json) { // url 获取数据时,对数据的处理,作为 getData 的回调函数
- 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';
- //字符串转化为 js 对象
- return data;
- }
- });
- //淘宝搜索建议测试
- var taobaoBsSuggest = $("#taobao").bsSuggest({
- indexId: 2, //data.value 的第几个数据,作为input输入框的内容
- indexKey: 1, //data.value 的第几个数据,作为input输入框的内容
- allowNoKeyword: false, //是否允许无关键字时请求数据
- multiWord: true, //以分隔符号分割的多关键字支持
- separator: ",", //多关键字支持时的分隔符,默认为空格
- getDataMethod: "url", //获取数据的方式,总是从 URL 获取
- effectiveFieldsAlias: {
- Id: "序号",
- Keyword: "关键字",
- Count: "数量"
- },
- showHeader: true,
- url: 'http://suggest.taobao.com/sug?code=utf-8&extras=1&q=',
- /*优先从url ajax 请求 json 帮助数据,注意最后一个参数为关键字请求参数*/
- jsonp: 'callback',
- /*如果从 url 获取数据,并且需要跨域,则该参数必须设置*/
- processData: function (json) { // url 获取数据时,对数据的处理,作为 getData 的回调函数
- 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>
|