yaopinsousuo.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. mui.plusReady(function(){
  2. var $searchbarInput = $('#searchPut'),
  3. $searchSuggest = $('#search_suggest_text'),// 搜索框下面悬浮的搜索提示
  4. search_keyword = "",//记录搜索框的内容
  5. $noResultWrap = $('#no_result_wrap'),
  6. $del = $('.u-icon-delete'),
  7. $ul = $('.lc-list'),
  8. $searchResult = $('#search_result');
  9. $searchbarInput.focus();
  10. //监视输入
  11. $searchbarInput.on('input', function() {
  12. var text = $(this).val().trim();
  13. if(text){
  14. $del.show()
  15. }else{
  16. $del.hide()
  17. }
  18. $searchResult.hide();
  19. $noResultWrap.hide();
  20. showSearchSuggest(text);
  21. }).on('keydown',function(e) {
  22. if (e.which === 13) {
  23. searchByPaging()
  24. }
  25. })
  26. //查询
  27. function searchByPaging(){
  28. $searchbarInput.blur();
  29. search_keyword = $searchbarInput.val();
  30. if(!$.trim(search_keyword)) {
  31. return ;
  32. }
  33. $searchSuggest.hide();
  34. var params = {
  35. name:search_keyword
  36. }
  37. plus.nativeUI.showWaiting();
  38. sendPost("doctor/prescriptionInfo/findDictByName",params, function(){
  39. plus.nativeUI.closeWaiting();
  40. mui.toast("获取数据失败");
  41. }, function(res){
  42. plus.nativeUI.closeWaiting();
  43. if(res.status == 200){
  44. if(res.data.length>0){
  45. var html = template('result_tmp',{data:$.map(res.data,function(o,index){
  46. o.isRefrigerate = o.storageConditions;
  47. o.physicAmountUnit = o.packUnit;
  48. o.physicAmountUnitName = o.packUnitName;
  49. o.jsonStr = JSON.stringify(o);
  50. return o })
  51. })
  52. $ul.empty().html(html)
  53. ellipsisText($searchResult.find('.yp-one'),search_keyword)
  54. $noResultWrap.hide();
  55. $searchResult.show();
  56. }else{
  57. $noResultWrap.show()
  58. $ul.empty()
  59. $searchResult.hide()
  60. }
  61. }else{
  62. mui.toast("获取数据失败");
  63. }
  64. },'get')
  65. }
  66. $searchSuggest.on('click',function() {
  67. searchByPaging()
  68. })
  69. mui('.mui-scroll-wrapper').scroll({
  70. bounce: true
  71. })
  72. // 控制搜索关键字悬浮提示的显示
  73. function showSearchSuggest(text) {
  74. var suggestText = '搜索“'+text+'”';
  75. if(text&&text.trim().length) {
  76. $searchSuggest.text(suggestText);
  77. $searchSuggest.show();
  78. } else {
  79. $searchSuggest.text('');
  80. $searchSuggest.hide();
  81. }
  82. }
  83. //关键字变色
  84. function ellipsisText($elements, searchText) {
  85. _.each($elements,function(el) {
  86. var $textEl = $(el).find(".j-text-ellipsis");
  87. _.each($textEl, function(t){
  88. highlineKeyword($(t),searchText)
  89. })
  90. })
  91. }
  92. function highlineKeyword($el,searchText) {
  93. var props = getRowProps($el),
  94. chartWidth = props.chartWidth,
  95. rowHeight = props.rowHeight,
  96. rowWidth = props.rowWidth,
  97. // 每行显示字符数(以中文字符为标准计算)
  98. chartNum = Math.floor(rowWidth / chartWidth.zh),
  99. // 排除指定数量字符所占宽度
  100. exceptNum = 0,
  101. // 行数
  102. rowNum = 1,
  103. // 预计显示总字符数
  104. expectedNum = chartNum * rowNum - exceptNum,
  105. $target = $el,
  106. // 目标文本
  107. text = $.trim($target.attr('data-text')),
  108. length = text.length,
  109. // 关键字数组
  110. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  111. var fidx = 0,preFidx,diff = 0;
  112. $target.html(replaceAll(text, kws));
  113. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  114. return ;
  115. }
  116. }
  117. function getRowProps($el) {
  118. var $textEllipsis = $el,
  119. $text = $textEllipsis.eq(0),
  120. $chart = $text.text('a'),
  121. enWidth = $chart.width(),
  122. $chart = $text.text('中'),
  123. zhWidth = $chart.width(),
  124. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  125. rowHeight = $chart.height();
  126. $chart.text('');
  127. return {
  128. chartWidth: {
  129. zh: zhWidth,
  130. en: enWidth
  131. },
  132. rowHeight: Math.max(rowHeight, lineHeight),
  133. rowWidth: $el.width()
  134. };
  135. }
  136. function replaceAll(text, arr) {
  137. var html = text;
  138. _.each(arr,function(kw) {
  139. var reg = new RegExp(kw+"(?!>)","gi");
  140. html = html.replace(reg,'<em>'+kw+'</em>');
  141. });
  142. return html;
  143. }
  144. //清空
  145. $del.click(function(){
  146. $searchbarInput.val('')
  147. $del.hide()
  148. $noResultWrap.hide()
  149. $searchResult.hide()
  150. $searchSuggest.empty().hide()
  151. $searchbarInput.focus()
  152. })
  153. //选取用药
  154. $ul.on('click','.yp-one',function(){
  155. var $val = $(this).attr('data-json');
  156. plus.storage.setItem("chooseMedicineList",$val)
  157. //新增
  158. var page = plus.webview.getWebviewById("change-chufang2");
  159. if(page){
  160. mui.fire(page, "addMedicine");
  161. }
  162. //返回添加页
  163. var page1 = plus.webview.getWebviewById("yaopinsousuo");
  164. if(page1){
  165. page1.close()
  166. }
  167. mui.back()
  168. })
  169. })