xietongfuwu-search.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. mui.plusReady(function() {
  2. var page = 1,
  3. pagesize = 15;
  4. var self = plus.webview.currentWebview();
  5. var teamCode = self.teamCode;
  6. var docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  7. var $searchbarInput = $('#searchPut'),
  8. $searchSuggest = $('#search_suggest_text'),// 搜索框下面悬浮的搜索提示
  9. search_keyword = "",//记录搜索框的内容
  10. $noResultWrap = $('#no_result_wrap'),
  11. $searchResult = $('#search_result');
  12. mui.init({
  13. pullRefresh : {
  14. container:'.mui-scroll-wrapper',
  15. up : {
  16. height:50,
  17. contentdown:"",
  18. contentrefresh : "正在加载...",
  19. contentnomore:'没有更多数据了',
  20. callback: function() {
  21. var self = this;
  22. page++;
  23. queryListData(function(value){
  24. setTimeout(function(){
  25. self.endPullupToRefresh(value);
  26. },500)
  27. },'up')
  28. }
  29. }
  30. }
  31. })
  32. //监视输入
  33. $searchbarInput.on('input', function() {
  34. var text = $(this).val().trim();
  35. $searchResult.hide();
  36. $noResultWrap.hide();
  37. showSearchSuggest(text);
  38. }).on('keydown',function(e) {
  39. if (e.which === 13) {
  40. searchByPaging()
  41. }
  42. })
  43. //查询
  44. function searchByPaging(){
  45. $searchbarInput.blur();
  46. search_keyword = $searchbarInput.val();
  47. if(!$.trim(search_keyword)) {
  48. return ;
  49. }
  50. page = 1;
  51. $('#dest_list').empty();
  52. mui('.mui-scroll-wrapper').pullRefresh().refresh(true);
  53. plus.nativeUI.showWaiting();
  54. queryListData();
  55. $searchSuggest.hide();
  56. $noResultWrap.hide();
  57. }
  58. $searchSuggest.on('click',function() {
  59. searchByPaging()
  60. })
  61. // 控制搜索关键字悬浮提示的显示
  62. function showSearchSuggest(text) {
  63. var suggestText = '搜索“'+text+'”';
  64. // 如果text不为空,则显示;否则隐藏
  65. if(text&&text.trim().length) {
  66. $searchSuggest.text(suggestText);
  67. $searchSuggest.show();
  68. } else {
  69. $searchSuggest.text('');
  70. $searchSuggest.hide();
  71. }
  72. }
  73. //关键字变色
  74. function ellipsisText($elements, searchText) {
  75. _.each($elements,function(el) {
  76. var $textEl = $(el).find(".j-text-ellipsis");
  77. _.each($textEl, function(t){
  78. highlineKeyword($(t),searchText)
  79. })
  80. })
  81. }
  82. function highlineKeyword($el,searchText) {
  83. var props = getRowProps($el),
  84. chartWidth = props.chartWidth,
  85. rowHeight = props.rowHeight,
  86. rowWidth = props.rowWidth,
  87. // 每行显示字符数(以中文字符为标准计算)
  88. chartNum = Math.floor(rowWidth / chartWidth.zh),
  89. // 排除指定数量字符所占宽度
  90. exceptNum = 0,
  91. // 行数
  92. rowNum = 1,
  93. // 预计显示总字符数
  94. expectedNum = chartNum * rowNum - exceptNum,
  95. $target = $el,
  96. // 目标文本
  97. text = $.trim($target.attr('data-text')),
  98. length = text.length,
  99. // 关键字数组
  100. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  101. var fidx = 0,preFidx,diff = 0;
  102. $target.html(replaceAll(text, kws));
  103. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  104. return ;
  105. }
  106. }
  107. function getRowProps($el) {
  108. var $textEllipsis = $el,
  109. $text = $textEllipsis.eq(0),
  110. $chart = $text.text('a'),
  111. enWidth = $chart.width(),
  112. $chart = $text.text('中'),
  113. zhWidth = $chart.width(),
  114. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  115. rowHeight = $chart.height();
  116. $chart.text('');
  117. return {
  118. chartWidth: {
  119. zh: zhWidth,
  120. en: enWidth
  121. },
  122. rowHeight: Math.max(rowHeight, lineHeight),
  123. rowWidth: $el.width()
  124. };
  125. }
  126. function replaceAll(text, arr) {
  127. var html = text;
  128. _.each(arr,function(kw) {
  129. var reg = new RegExp(kw+"(?!>)","gi");
  130. html = html.replace(reg,'<em>'+kw+'</em>');
  131. });
  132. return html;
  133. }
  134. //取消
  135. $('.searchbar-cancel').on('click',function() {
  136. mui.back();
  137. })
  138. template.helper('setSex',function(item){
  139. if(item == 1){
  140. return '男'
  141. }else{
  142. return '女'
  143. }
  144. })
  145. //点击跳转
  146. $searchResult.on('tap','.j-detail',function(){
  147. mui.openWindow({
  148. id: "xietongfuwu-detail",
  149. url: "xietongfuwu-detail.html",
  150. extras: {
  151. code:$(this).attr('data-code')
  152. }
  153. })
  154. })
  155. function queryListData(fun){
  156. var params ={
  157. adminTeamCode:teamCode,
  158. doctor:docInfo.code,
  159. page:page,
  160. size:pagesize,
  161. patientName:search_keyword
  162. }
  163. sendGet("doctor/pcCustomer/getCallServices",params, function(){
  164. plus.nativeUI.closeWaiting();
  165. fun&&fun.call(this,false)
  166. mui.toast("获取数据失败!");
  167. }, function(res){
  168. plus.nativeUI.closeWaiting();
  169. if(res.status == 200){
  170. var html = template('li_tmpl',{list:_.map(res.data,function(o){
  171. o.jsonStr = JSON.stringify(o);
  172. return o;})
  173. })
  174. if(page == 1 && res.data.length==0){
  175. $noResultWrap.show();
  176. $searchResult.hide();
  177. }else{
  178. $noResultWrap.hide();
  179. $searchResult.show();
  180. if(res.data.length < pagesize){
  181. fun&&fun.call(this,true)
  182. }else{
  183. fun&&fun.call(this,false)
  184. }
  185. $('#dest_list').append(html);
  186. ellipsisText($searchResult.find('.j-detail'),search_keyword)//变色
  187. }
  188. }else{
  189. fun&&fun.call(this,false)
  190. mui.toast("获取数据失败!");
  191. }
  192. })
  193. }
  194. })
  195. template.helper("setPhoto", function(p) {
  196. return getImgUrl(p);
  197. })
  198. template.helper('setType',function(item){
  199. if(item == 1){return '预约挂号'}
  200. if(item == 0){return '医生咨询'}
  201. })