search-bar.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;$(function () {
  2. var $searchInput = $('#searchInput'),
  3. $searchCancel = $('#searchCancel'),
  4. $categoryIndex = $('#categoryIndex'),
  5. $searchResultList = $('#searchResultList'),
  6. $searchResult = $('#searchResult'),
  7. $maskCont = $('.js-mask-cont'),
  8. data = {},
  9. timer = null;
  10. // 初始化数据加载
  11. function dataInit() {
  12. sendPost("common/health/food/list?pid=0",data, "json", "post", operateFailed, operateSuccesss);
  13. function operateSuccesss(res) {
  14. var categoryData = res,
  15. dataStr = JSON.stringify(res.list);
  16. localStorage.setItem("categoryList", dataStr);
  17. html = template('categoryList', categoryData);
  18. $categoryIndex.html(html);
  19. };
  20. function operateFailed(res) {
  21. };
  22. }
  23. dataInit();
  24. // 视图逻辑层处理
  25. var viewLogic = {
  26. showFoodDeital:function () {
  27. $searchResult.show();
  28. $categoryIndex.hide();
  29. $maskCont.hide();
  30. },
  31. hideFoodDetial:function () {
  32. $searchResult.hide();
  33. $categoryIndex.show();
  34. $maskCont.hide();
  35. }
  36. }
  37. var timer = null;
  38. $searchInput.on('input',controlCancelBtn)
  39. function controlCancelBtn() {
  40. clearTimeout(timer);
  41. var val = $(this).val();
  42. // 查询
  43. function searchAjaxFn(){
  44. var postData = {},
  45. singleData = null;
  46. resData = null;
  47. timer = setTimeout(function () {
  48. postData.name = val;
  49. sendPost("common/health/food/search", postData, "json", "post", operateFailed, operateSuccesss);},700)
  50. function operateSuccesss(res) {
  51. resData = res.list;
  52. var length = resData.length,
  53. delNum;
  54. $maskCont.show();
  55. // 排除大类别
  56. console.log(resData);
  57. for (var i = 0; i < length; i++) {
  58. if (resData[i].pid == 0) {
  59. delNum = i;
  60. }
  61. }
  62. if (typeof delNum !== 'undefined') {
  63. resData.splice(delNum,1)
  64. }
  65. if (resData.length > 0) {
  66. var html = template('ResultTemplate', res);
  67. $searchResultList.html(html);
  68. $searchResultList.on('click','.searchbar-result-item',function () {
  69. var id = $(this).data('id'),
  70. length = resData.length;
  71. for (var i = 0; i < length; i++) {
  72. if (resData[i].id === id) {
  73. singleData = resData[i];
  74. }
  75. }
  76. if (typeof singleData.component === 'string') {
  77. singleData.component = JSON.parse(singleData.component);
  78. }
  79. html = template('singleResult', singleData);
  80. $searchResult.html(html)
  81. $searchInput.val(singleData.name);
  82. viewLogic.showFoodDeital();
  83. })
  84. }else {
  85. var
  86. data = {
  87. list: [{name:'查无结果'}],
  88. },
  89. html = template('ResultTemplate', data);
  90. $searchResultList.html(html);
  91. }
  92. }
  93. function operateFailed(res) {
  94. }
  95. }
  96. if (val !== '') {
  97. $searchCancel.show();
  98. searchAjaxFn();
  99. } else {
  100. $searchCancel.hide();
  101. $maskCont.hide();
  102. viewLogic.hideFoodDetial();
  103. }
  104. }
  105. $searchCancel.click(hideCancelBtn)
  106. function hideCancelBtn() {
  107. $searchInput.val('');
  108. $(this).hide();
  109. $maskCont.hide();
  110. viewLogic.hideFoodDetial();
  111. }
  112. })