autocomplete.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <th:block th:include="include :: header('搜索自动补全')" />
  5. </head>
  6. <body class="gray-bg">
  7. <div class="wrapper wrapper-content animated fadeInRight">
  8. <div class="row">
  9. <div class="col-sm-6">
  10. <div class="ibox float-e-margins">
  11. <div class="ibox-title">
  12. <h5>搜索自动补全<small>https://github.com/lzwme/bootstrap-suggest-plugin</small></h5>
  13. </div>
  14. <div class="ibox-content">
  15. <p>展示下拉菜单按钮。</p>
  16. <div class="row">
  17. <div class="col-lg-6">
  18. <div class="input-group">
  19. <input type="text" class="form-control" id="suggest-demo-1">
  20. <div class="input-group-btn">
  21. <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
  22. <span class="caret"></span>
  23. </button>
  24. <ul class="dropdown-menu dropdown-menu-right" role="menu">
  25. </ul>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <p>不展示下拉菜单按钮。</p>
  31. <div class="row">
  32. <div class="col-lg-6">
  33. <div class="input-group">
  34. <input type="text" class="form-control" id="suggest-demo-2">
  35. <div class="input-group-btn">
  36. <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
  37. <span class="caret"></span>
  38. </button>
  39. <ul class="dropdown-menu dropdown-menu-right" role="menu">
  40. </ul>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <p>前端json中获取数据</p>
  46. <div class="row">
  47. <div class="col-lg-6">
  48. <div class="input-group">
  49. <input type="text" class="form-control" id="suggest-demo-3">
  50. <div class="input-group-btn">
  51. <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
  52. <span class="caret"></span>
  53. </button>
  54. <ul class="dropdown-menu dropdown-menu-right" role="menu">
  55. </ul>
  56. </div>
  57. <!-- /btn-group -->
  58. </div>
  59. </div>
  60. </div>
  61. <h3>百度搜索</h3>
  62. <p>支持逗号分隔多关键字</p>
  63. <div class="row">
  64. <div class="col-lg-6">
  65. <div class="input-group" style="width: 300px;">
  66. <input type="text" class="form-control" id="baidu">
  67. <div class="input-group-btn">
  68. <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
  69. <span class="caret"></span>
  70. </button>
  71. <ul class="dropdown-menu dropdown-menu-right" role="menu">
  72. </ul>
  73. </div>
  74. <!-- /btn-group -->
  75. </div>
  76. </div>
  77. </div>
  78. <h3>淘宝搜索</h3>
  79. <p>支持逗号分隔多关键字</p>
  80. <div class="row">
  81. <div class="col-lg-6">
  82. <div class="input-group" style="width: 400px;">
  83. <input type="text" class="form-control" id="taobao">
  84. <div class="input-group-btn">
  85. <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
  86. <span class="caret"></span>
  87. </button>
  88. <ul class="dropdown-menu dropdown-menu-right" role="menu">
  89. </ul>
  90. </div>
  91. <!-- /btn-group -->
  92. </div>
  93. </div>
  94. </div>
  95. <hr>
  96. <div class="form-group">
  97. <label class="font-noraml">相关参数详细信息</label>
  98. <div><a href="http://doc.ruoyi.vip/ruoyi/document/zjwd.html#bootstrap-suggest" target="_blank">http://doc.ruoyi.vip/ruoyi/document/zjwd.html#bootstrap-suggest</a></div>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. <div class="col-sm-6">
  104. <div class="ibox float-e-margins">
  105. <div class="ibox-title">
  106. <h5>搜索自动补全<small>https://github.com/bassjobsen/Bootstrap-3-Typeahead</small></h5>
  107. </div>
  108. <div class="ibox-content">
  109. <p>通过数据属性的基本示例。</p>
  110. <div class="row">
  111. <div class="col-lg-6">
  112. <input type="text" placeholder="ruoyi..." data-provide="typeahead" data-source='["ruoyi 1","ruoyi 2","ruoyi 3"]' class="form-control" />
  113. </div>
  114. </div>
  115. <hr>
  116. <p>通过javascript的基本示例。</p>
  117. <div class="row">
  118. <div class="col-lg-6">
  119. <input type="text" placeholder="ruoyi..." class="form-control" id="typeahead-demo-1"/>
  120. </div>
  121. </div>
  122. <hr>
  123. <p>通过javascript的复杂示例。</p>
  124. <div class="row">
  125. <div class="col-lg-6">
  126. <input type="text" placeholder="ruoyi..." class="form-control" id="typeahead-demo-2"/>
  127. </div>
  128. </div>
  129. <hr>
  130. <p>后台url中获取简单数据</p>
  131. <div class="row">
  132. <div class="col-lg-6">
  133. <input type="text" placeholder="ruoyi..." class="form-control" id="typeahead-demo-3"/>
  134. </div>
  135. </div>
  136. <hr>
  137. <div class="form-group">
  138. <label class="font-noraml">相关参数详细信息</label>
  139. <div><a href="http://doc.ruoyi.vip/ruoyi/document/zjwd.html#bootstrap-typeahead" target="_blank">http://doc.ruoyi.vip/ruoyi/document/zjwd.html#bootstrap-typeahead</a></div>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. <th:block th:include="include :: footer" />
  147. <th:block th:include="include :: bootstrap-suggest-js" />
  148. <th:block th:include="include :: bootstrap-typeahead-js" />
  149. <script type="text/javascript">
  150. var testBsSuggest = $("#suggest-demo-1").bsSuggest({
  151. url: ctx + "demo/form/userModel",
  152. idField: "userId",
  153. keyField: "userName"
  154. }).on('onDataRequestSuccess', function (e, result) {
  155. console.log('onDataRequestSuccess: ', result);
  156. }).on('onSetSelectValue', function (e, keyword) {
  157. console.log('onSetSelectValue: ', keyword);
  158. }).on('onUnsetSelectValue', function (e) {
  159. console.log("onUnsetSelectValue");
  160. });
  161. var testBsSuggest = $("#suggest-demo-2").bsSuggest({
  162. url: ctx + "demo/form/userModel",
  163. showBtn: false,
  164. idField: "userId",
  165. keyField: "userName"
  166. }).on('onDataRequestSuccess', function (e, result) {
  167. console.log('onDataRequestSuccess: ', result);
  168. }).on('onSetSelectValue', function (e, keyword) {
  169. console.log('onSetSelectValue: ', keyword);
  170. }).on('onUnsetSelectValue', function (e) {
  171. console.log("onUnsetSelectValue");
  172. });
  173. //data 数据中获取
  174. var testdataBsSuggest = $("#suggest-demo-3").bsSuggest({
  175. indexId: 1,
  176. indexKey: 2,
  177. data: {
  178. 'value': [
  179. {
  180. 'userId': '1',
  181. 'userCode': '1000001',
  182. 'userName': '测试1',
  183. 'userPhone': '15888888888'
  184. },
  185. {
  186. 'userId': '2',
  187. 'userCode': '1000002',
  188. 'userName': '测试2',
  189. 'userPhone': '15888888888'
  190. },
  191. {
  192. 'userId': '3',
  193. 'userCode': '1000003',
  194. 'userName': '测试3',
  195. 'userPhone': '15888888888'
  196. },
  197. {
  198. 'userId': '4',
  199. 'userCode': '1000004',
  200. 'userName': '测试4',
  201. 'userPhone': '15888888888'
  202. },
  203. {
  204. 'userId': '5',
  205. 'userCode': '1000005',
  206. 'userName': '测试5',
  207. 'userPhone': '15888888888'
  208. }
  209. ],
  210. 'defaults': 'http://ruoyi.vip'
  211. }
  212. });
  213. //百度搜索测试
  214. var baiduBsSuggest = $("#baidu").bsSuggest({
  215. allowNoKeyword: false, //是否允许无关键字时请求数据
  216. multiWord: true, //以分隔符号分割的多关键字支持
  217. separator: ",", //多关键字支持时的分隔符,默认为空格
  218. getDataMethod: "url", //获取数据的方式,总是从 URL 获取
  219. url: 'http://unionsug.baidu.com/su?p=3&t=' + (new Date()).getTime() + '&wd=',
  220. /*优先从url ajax 请求 json 帮助数据,注意最后一个参数为关键字请求参数*/
  221. jsonp: 'cb',
  222. /*如果从 url 获取数据,并且需要跨域,则该参数必须设置*/
  223. processData: function (json) { // url 获取数据时,对数据的处理,作为 getData 的回调函数
  224. var i, len, data = {
  225. value: []
  226. };
  227. if (!json || !json.s || json.s.length === 0) {
  228. return false;
  229. }
  230. console.log(json);
  231. len = json.s.length;
  232. jsonStr = "{'value':[";
  233. for (i = 0; i < len; i++) {
  234. data.value.push({
  235. word: json.s[i]
  236. });
  237. }
  238. data.defaults = 'baidu';
  239. //字符串转化为 js 对象
  240. return data;
  241. }
  242. });
  243. //淘宝搜索建议测试
  244. var taobaoBsSuggest = $("#taobao").bsSuggest({
  245. indexId: 2, // data.value 的第几个数据,作为input输入框的内容
  246. indexKey: 1, // data.value 的第几个数据,作为input输入框的内容
  247. allowNoKeyword: false, // 是否允许无关键字时请求数据
  248. hideOnSelect: true, // 鼠标从列表单击选择了值时,是否隐藏选择列表
  249. multiWord: true, // 以分隔符号分割的多关键字支持
  250. separator: ",", // 多关键字支持时的分隔符,默认为空格
  251. getDataMethod: "url", // 获取数据的方式,总是从 URL 获取
  252. effectiveFieldsAlias: {
  253. Id: "序号",
  254. Keyword: "关键字",
  255. Count: "数量"
  256. },
  257. showHeader: true,
  258. url: 'http://suggest.taobao.com/sug?code=utf-8&extras=1&q=',
  259. /*优先从url ajax 请求 json 帮助数据,注意最后一个参数为关键字请求参数*/
  260. jsonp: 'callback',
  261. /*如果从 url 获取数据,并且需要跨域,则该参数必须设置*/
  262. processData: function (json) { // url 获取数据时,对数据的处理,作为 getData 的回调函数
  263. var i, len, data = {
  264. value: []
  265. };
  266. if (!json || !json.result || json.result.length == 0) {
  267. return false;
  268. }
  269. console.log(json);
  270. len = json.result.length;
  271. for (i = 0; i < len; i++) {
  272. data.value.push({
  273. "Id": (i + 1),
  274. "Keyword": json.result[i][0],
  275. "Count": json.result[i][1]
  276. });
  277. }
  278. console.log(data);
  279. return data;
  280. }
  281. });
  282. $('#typeahead-demo-1').typeahead({
  283. source: ["ruoyi 1","ruoyi 2","ruoyi 3"]
  284. });
  285. $('#typeahead-demo-2').typeahead({
  286. source: [
  287. {"name": "Afghanistan", "code": "AF", "ccn0": "040"},
  288. {"name": "Land Islands", "code": "AX", "ccn0": "050"},
  289. {"name": "Albania", "code": "AL","ccn0": "060"},
  290. {"name": "Algeria", "code": "DZ","ccn0": "070"}
  291. ]
  292. });
  293. $.get(ctx + "demo/form/collection", function(data){
  294. $("#typeahead-demo-3").typeahead({
  295. source: data.value
  296. });
  297. },'json');
  298. </script>
  299. </body>
  300. </html>