autoInput.htm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <#def title = "输入框样式"/>
  2. <#def keyword = "ZhiqimUI"/>
  3. <#def desc = "输入框样式是指对输入框增加样式规则,如指定输入框长度大小、边框颜色、和输入控制等。"/>
  4. <#def prevUrl = "../js/query.htm"/>
  5. <#def nextUrl = "button.htm"/>
  6. ${zhiqim_com_head()}
  7. ${zhiqim_com_head_main()}
  8. <script>
  9. function doChange(tabnav)
  10. {
  11. doUpdateCode(Z(tabnav));
  12. }
  13. function doUpdateCode($this)
  14. {//更新代码
  15. var html = $this.find("section > div").htmlt();
  16. //去除多余的cover代码
  17. html = Z.S.replaceAll(html, /&lt;span\sclass="z-cover((?!&gt;).)+&gt;/g, '');
  18. html = Z.S.replaceAll(html, /&lt;div\sclass="z-absolute((?!&gt;).)+&gt;.+span&gt;/g, '');
  19. $this.find("section > div").next("div > span").html(html);
  20. }
  21. </script>
  22. ${zhiqim_com_head_end()}
  23. ${zhiqim_com_mobileUI_scrollJs()}
  24. ${zhiqim_com_body()}
  25. ${zhiqim_com_topnav("ui")}
  26. <!--容器开始 -->
  27. <div id="container" class="container">
  28. ${zhiqim_com_container_ui("ZmUI", "form", "input")}
  29. <!--主体-->
  30. <div id="mainbody" class="mainbody">
  31. ${zhiqim_com_breadcrumb("UI", "ZmUI", "表单", "输入框")}
  32. <!-- content开始 -->
  33. <div id="mobileUI_content" class="content">
  34. <!-- 左侧详情 -->
  35. <div class="mobileUI-left">
  36. <#-- 标题 -->
  37. <div class="z-relative-left z-w100p z-pd16 z-bg-blue z-px18 z-lh200p z-bold" style="border-left:5px solid #4bacc6">
  38. <p class="z-text-blue">自动提示关键字:</p>
  39. <p class="z-color-333" style="text-indent:38px;">
  40. 输入内容,匹配数据库显示可选项;支持键盘“上”、“下”键选择,鼠标、“回车”选中
  41. </p>
  42. </div>
  43. <#-- 一、自动提示关键字 -->
  44. <div class="tutorial title">一、纯文本框,不显示历史记录、显示高度为5条选框高度</div>
  45. <div class="z-tabnav z-bordered" data-role="z-tabnav" onchange="doChange(this);">
  46. <nav>
  47. <ul class="zi-float-left">
  48. <li class="z-active">示例</li>
  49. <li>代码</li>
  50. <li>说明</li>
  51. </ul>
  52. </nav>
  53. <section style="min-height:150px;">
  54. <div class="z-active z-pd10 z-px16" style="min-height:200px;"><script>
  55. Z.onload(function()
  56. {
  57. var searchAutoList = new Z.SearchAutoList({
  58. id: "auto_input", //输入框 id
  59. ajaxClassName: "com.zhiqim.zhiqim.ui.SearchInputAjaxList", //ajax调用的类名
  60. ajaxMethodName: "getKeywordsList", //ajax调用的方法名
  61. history: 0, //可选项;是否显示历史记录,默认为 “1” 显示
  62. listMax: 5, //可选项;结果列表的显示高度,默认为 “10”
  63. splitType: ";", //可选项;返回值中的分隔符,默认为 “;”
  64. callback: null //可选项;选中后的回调函数,默认为 null,this 为定义的 Z.SearchAutoList 对象
  65. });
  66. });
  67. </script>
  68. <input id="auto_input" class="z-input z-large z-w300" type="text">
  69. </div>
  70. <div class="z-relative-left z-w100p z-pd10">
  71. <span class="z-text-prewrap z-pre"></span>
  72. </div>
  73. <div class="z-pd10 z-px16 z-lh200p">
  74. 1、前三个参数为必填项,缺一不可<br>
  75. 2、Ajax的回调结果为字符串型,字符的分隔符号由类方法决定<br>
  76. 3、splitType 为可选项,默认值为 “;”,字符的分隔符号不为 “;” 时需要定义<br>
  77. </div>
  78. </section>
  79. <div class="tutorial title">二、带按钮的搜索框,显示历史记录,显示高度为10条选项高度</div>
  80. <div class="z-tabnav z-bordered" data-role="z-tabnav" onchange="doChange(this);">
  81. <nav>
  82. <ul class="zi-float-left">
  83. <li class="z-active">示例</li>
  84. <li>代码</li>
  85. <li>说明</li>
  86. </ul>
  87. </nav>
  88. <section style="min-height:150px;">
  89. <div class="z-active z-pd10 z-px16" style="min-height:200px;"><script>
  90. Z.onload(function()
  91. {
  92. var searchAutoList2 = new Z.SearchAutoList({
  93. id: "auto_wrap", //盒子 id
  94. ajaxClassName: "com.zhiqim.zhiqim.ui.SearchInputAjaxList", //ajax调用的类名
  95. ajaxMethodName: "getKeywordsList", //ajax调用的方法名
  96. callback: function(){ //可选项;选中后的回调函数,默认为 null,this 为定义的 Z.SearchAutoList 对象
  97. alert("输入结果: " + this.value);
  98. }
  99. });
  100. });
  101. </script>
  102. <span id="auto_wrap" class="z-show-ib">
  103. <input id="auto_input2" class="z-input z-large z-w300" type="text"><button class="z-button z-mg-l-1" style="height:34px" type="submit">搜索</button>
  104. </span>
  105. </div>
  106. <div class="z-relative-left z-w100p z-pd10">
  107. <span class="z-text-prewrap z-pre"></span>
  108. </div>
  109. <div class="z-pd10 z-px16 z-lh200p">
  110. 1、前三个参数为必填项,缺一不可<br>
  111. 2、Ajax的回调结果为字符串型,字符的分隔符号由类方法决定<br>
  112. 3、splitType 为可选项,默认值为 “;”,字符的分隔符号不为 “;” 时需要定义<br>
  113. 4、callback 为可选项,表示选中后需要做的操作,默认值为 “null”,this 为定义的 Z.SearchAutoList 本身<br>
  114. 5、callback 函数中可用到的值:this.value 为输入框内容,this.responseText 为返回的搜索结果字符串
  115. </div>
  116. </section>
  117. </div>
  118. <!-- 右侧demo ZmUI代码 -->
  119. ${zhiqim_turorial_ZmUI_demo_start()}
  120. <div class="z-bar-top">
  121. <a href="#" class="z-left"><i class="z-font z-rem18 z-arrowhead-left"></i></a>
  122. <div class="z-title">ZhiqimUI.mobile</div>
  123. </div>
  124. <div class="z-bar-bottom">
  125. <a href="#" class="z-direction-column z-active"><i class="z-font z-apps"></i><span class="z-mg-t3">文案</span></a>
  126. <a href="#" class="z-direction-column"><i class="z-font z-apps"></i><span class="z-mg-t3">文案</span></a>
  127. <a href="#" class="z-direction-column"><i class="z-font z-apps"></i><span class="z-mg-t3">文案</span></a>
  128. <a href="#" class="z-direction-column"><i class="z-font z-apps"></i><span class="z-mg-t3">文案</span></a>
  129. </div>
  130. <div class="z-container">
  131. <div class="z-flexBox-list z-h100p"><div class="z-rem30 z-text-center">这里是内容</div></div>
  132. </div>
  133. ${zhiqim_turorial_ZmUI_demo_end()}
  134. <!-- content结束 -->
  135. </div>
  136. ${zhiqim_com_chapter()}
  137. <!-- 主体结束 -->
  138. </div>
  139. <!-- 容器结束 -->
  140. </div>
  141. ${zhiqim_com_footer()}
  142. <script>
  143. +(function(F)
  144. {
  145. Z.SearchAutoList = Z.Class.newInstance();
  146. Z.SearchAutoList.prototype =
  147. {
  148. defaults:
  149. {
  150. id: null, //输入框/输入框盒子ID
  151. ajaxClassName: null, //Ajax调用的类名
  152. ajaxMethodName: null, //Ajax调用的类方法
  153. history: 1, //历史记录显示
  154. listMax: 10, //结果列表显示高度
  155. splitType: ";", //类方法返回的字符串结果中的分隔符
  156. callback: null //选中后的回调函数
  157. },
  158. init: function()
  159. {//构造函数
  160. if (!this.id || !Z.D.has(this.id)){
  161. Z.alert("指定[id]出错");
  162. }
  163. this.responseText = "";
  164. this.$wrap = Z("#"+this.id);
  165. this.$input = this.$wrap;
  166. if (this.$wrap[0].tagName.toUpperCase() != "INPUT")
  167. this.$input = this.$wrap.find("input[type=text]");
  168. var width = this.$wrap.offsetWidth();
  169. var height = this.$wrap.offsetHeight();
  170. var fontSize = this.$input.css("fontSize");
  171. this.pdLeft = this.$input.css("padding-left");
  172. this.pdRight = this.$input.css("padding-right");
  173. this.lHeight = this.$input.css("line-height");
  174. this.mHeight = this.listMax * parseInt(this.lHeight) + "px";
  175. this.$cover = Z.$cover(this.$wrap).css({display:"inline-block",position:"relative",width:width,height:height,fontSize:fontSize});
  176. this.$list = Z('<div class="z-absolute z-w100p z-mg-t-1 z-bg-white z-text-left z-bd z-hide" style="left:0;top:100%;font-size:inherit;z-index:99;"></div>')
  177. .appendTo(this.$cover);
  178. //事件绑定
  179. this.$input.on("focus",this.focus,this);
  180. this.$input.on("input",this.input,this);
  181. this.$input.keydown(this.doKeyDown,this);
  182. Z(document).click(this.docClick,this);
  183. },
  184. execute: function()
  185. {//执行为空即可,统一在初始化中处理
  186. },
  187. //获取焦点事件
  188. focus: function(e)
  189. {
  190. this.value = this.$input.val();
  191. if (this.value == "")
  192. return this.doHistoryList();
  193. if (this.$list.find('li').length == 0)
  194. return;
  195. this.$list.show();
  196. if (this.beforeValue != this.value)
  197. {//无修改时,获取焦点,不调用Ajax
  198. this.doAjax();
  199. this.beforeValue = this.value;
  200. }
  201. },
  202. //input事件
  203. input: function(e)
  204. {
  205. this.value = this.$input.val();
  206. if (Z.V.isEmptyBlank(this.value))
  207. return this.doHistoryList();
  208. this.$list.show();
  209. this.doAjax();
  210. },
  211. doAjax: function(e)
  212. {
  213. this.ajax = new Z.Ajax();
  214. this.ajax.OBJ = this;
  215. this.ajax.setClassName(this.ajaxClassName);
  216. this.ajax.setMethodName(this.ajaxMethodName);
  217. this.ajax.addParam(this.value);
  218. this.ajax.setCallback(function(){
  219. this.OBJ.doFilter.call(this.OBJ,this.responseText);
  220. });
  221. this.ajax.execute();
  222. },
  223. //筛选对象,生成列表
  224. doFilter: function(e)
  225. {
  226. var responseText = e;
  227. this.$list.html("");
  228. var keyWordsArr = responseText.split(this.splitType);
  229. var htmlStr = '<ul class="z-overflow-auto" style="max-height:' + this.mHeight + '">';
  230. for (var i = 0;i < keyWordsArr.length;i++)
  231. {
  232. if (keyWordsArr[i].indexOf(this.value) > -1)
  233. htmlStr += '<li style="padding: 0 ' + this.pdRight + ' 0 ' + this.pdLeft +'; line-height:'+ this.lHeight +'; cursor: default;">'
  234. + keyWordsArr[i] + "</li>";
  235. }
  236. htmlStr += "</ul>";
  237. Z(htmlStr).appendTo(this.$list);
  238. if (Z(htmlStr).html() == "")
  239. return this.$list.hide();
  240. //绑定事件
  241. this.liSet = this.$list.find("li");
  242. this.liSet.click(this.doSelect,this);
  243. this.liSet.mouseenter(this.liMouseEnter,this);
  244. this.liSet.mouseleave(this.liMouseLeave,this);
  245. },
  246. //鼠标选中
  247. doSelect: function(e)
  248. {
  249. Z.E.forbidden(e);
  250. var liVal = Z(Z.E.target(e)).html();
  251. this.beforeValue = this.value;
  252. this.value = liVal;
  253. this.$input.val(liVal);
  254. this.$list.hide();
  255. this.doAjax();
  256. //历史记录存储
  257. this.saveHistory(e);
  258. //选中后的回调
  259. this.doCallback(e);
  260. },
  261. //空白处点击,隐藏列表
  262. docClick: function(e)
  263. {
  264. var target = Z.E.target(e);
  265. if (target == this.$input[0])
  266. return;
  267. this.$list.hide();
  268. if (Z.V.isEmptyBlank(this.value))
  269. return;
  270. if (this.beforeValue != this.value)
  271. {//解决多次调用Ajax的问题
  272. this.doAjax();
  273. this.beforeValue = this.value;
  274. //历史记录存储
  275. this.saveHistory(e);
  276. }
  277. },
  278. //键盘 “上” “下” “回车”按键监听
  279. doKeyDown: function(e)
  280. {
  281. var keyVal = Z.E.key(e);
  282. if (keyVal == 38 || keyVal == 40 || keyVal == 13)
  283. {//上:38,下:40,回车:13
  284. Z.E.forbidden(e);
  285. if (this.liSet)
  286. {//有选择列表的搜索框
  287. var liSetLength = this.liSet.length;
  288. var liActive = this.$list.find(".z-bg-gray");
  289. var liActiveIndex = -1;
  290. var toIndex = null;
  291. if (liActive.length != 0)
  292. liActiveIndex = Z.EL.parentIndex(liActive[0]);
  293. switch (keyVal)
  294. {
  295. case 38://上
  296. toIndex = liActiveIndex - 1;
  297. if (toIndex <= -1)
  298. toIndex = liSetLength - 1;
  299. break;
  300. case 40://下
  301. toIndex = liActiveIndex + 1;
  302. if (toIndex == liSetLength)
  303. toIndex = 0;
  304. break;
  305. case 13://回车选中
  306. this.$input[0].blur();
  307. this.$list.hide();
  308. this.value = this.$input.val();
  309. this.doAjax();
  310. //历史记录存储
  311. this.saveHistory(e);
  312. //选中后的回调
  313. this.doCallback(e);
  314. return;
  315. }
  316. toIndex++;
  317. this.liSet.removeClass("z-bg-gray");
  318. var toLi = this.$list.find("li:nth-child(" + toIndex +")").addClass("z-bg-gray");
  319. this.value = toLi.html();
  320. this.$input.val(toLi.html());
  321. }
  322. else if (keyVal == 13)
  323. {//直接在输入框回车搜索
  324. this.value = this.$input.val();
  325. this.doAjax();
  326. //历史记录存储
  327. this.saveHistory(e);
  328. //选中后的回调
  329. this.doCallback(e);
  330. }
  331. }
  332. },
  333. //选中后的回掉函数
  334. doCallback: function(e)
  335. {
  336. if(!this.callback)
  337. return;
  338. this.callback(e);
  339. },
  340. //历史记录列表展示
  341. doHistoryList: function(e)
  342. {
  343. if (!this.history) return;
  344. this.$list.html("").hide();
  345. if (localStorage.getItem(this.id))
  346. {
  347. var htmlStr = '<ul class="z-text-gray">';
  348. var hisArr = localStorage.getItem(this.id).split(";");
  349. for (var i = 0;i < hisArr.length;i++)
  350. {
  351. htmlStr += '<li style="padding:0 ' + this.pdRight + ' 0 ' + this.pdLeft +';line-height:'+ this.lHeight +';cursor:default;">'
  352. + hisArr[i] + "</li>";
  353. }
  354. htmlStr += "</ul>";
  355. Z(htmlStr).appendTo(this.$list.show());
  356. var hisBar = '<div class="z-bg-blue z-clearfix" style="padding:0 ' + this.pdRight + ' 0 ' + this.pdLeft +';line-height:'+ this.lHeight +';cursor:default;">'
  357. + '<span class="z-float-right z-pointer z-deleteBtn">清空历史</span>'
  358. + '</div>';
  359. Z(hisBar).appendTo(this.$list.show());
  360. //绑定事件
  361. this.liSet = this.$list.find("li");
  362. this.liSet.click(this.doSelect,this);
  363. this.liSet.mouseenter(this.liMouseEnter,this);
  364. this.liSet.mouseleave(this.liMouseLeave,this);
  365. //绑定删除事件
  366. var $delBtn = this.$list.find(".z-deleteBtn");
  367. $delBtn.click(this.emptyHistory,this);
  368. }
  369. },
  370. emptyHistory: function(e)
  371. {
  372. localStorage.removeItem(this.id);
  373. },
  374. //输入完成,存储历史记录
  375. saveHistory: function(e)
  376. {
  377. if (!this.history) return;
  378. if (!localStorage.getItem(this.id))
  379. localStorage.setItem(this.id,"");
  380. var hisStr = localStorage.getItem(this.id);
  381. if (hisStr == "")
  382. return localStorage.setItem(this.id,this.value);
  383. //去重
  384. var hisArr = hisStr.split(";");
  385. hisArr.unshift(this.value);
  386. var hisObj = {};
  387. var resultArr = [];
  388. for(var i = 0; i < hisArr.length; i++)
  389. {
  390. if(!hisObj[hisArr[i]])
  391. {
  392. resultArr.push(hisArr[i]);
  393. hisObj[hisArr[i]] = 1;
  394. }
  395. }
  396. //限制个数
  397. if (resultArr.length > 10)
  398. resultArr.length = 10;
  399. hisStr = resultArr.join(";");
  400. localStorage.setItem(this.id,hisStr);
  401. },
  402. //鼠标悬浮,背景颜色变化
  403. liMouseEnter: function(e)
  404. {
  405. var $thisLi = Z(Z.E.target(e));
  406. $thisLi.addClass("z-bg-gray");
  407. },
  408. liMouseLeave: function(e)
  409. {
  410. var $thisLi = Z(Z.E.target(e));
  411. $thisLi.removeClass("z-bg-gray");
  412. },
  413. }
  414. }
  415. )(zhiqim)
  416. </script>