Authored by 郭成尧

搜索联动

@@ -19,6 +19,6 @@ if (qs) { @@ -19,6 +19,6 @@ if (qs) {
19 19
20 new ProductListWithFilter(initParams); 20 new ProductListWithFilter(initParams);
21 21
22 -// require('./search/list'); 22 +require('./search/search-list');
23 require('common/footer'); 23 require('common/footer');
24 require('./shop/coupon'); 24 require('./shop/coupon');
  1 +import Page from 'yoho-page';
  2 +import Tip from 'plugin/tip';
  3 +
  4 +class SearchList extends Page {
  5 + constructor() {
  6 + super();
  7 +
  8 + this.selector = {
  9 + input: $('#search-input').find('input[name="query"]'),
  10 + clear: $('#search-input .clear-input'),
  11 + buriedpoint: $('.buriedpoint'),
  12 + search: $('#search')
  13 + };
  14 +
  15 + this.init();
  16 + }
  17 +
  18 + init() {
  19 + this.inputAction();
  20 + }
  21 +
  22 + /**
  23 + * 搜索输入联动
  24 + */
  25 + inputAction() {
  26 + let $searchAssociate = $('.search-associate');
  27 + let $icon = $('.search-icon');
  28 + let $searchItems = $('.search-items');
  29 +
  30 + this.selector.input.on('input', () => {
  31 + if (this.selector.input.val() === '') {
  32 + $icon.css('color', '#b2b2b2');
  33 + this.selector.clear.addClass('hide');
  34 + $searchAssociate.html('');
  35 + $searchItems.show();
  36 + $searchAssociate.hide();
  37 + } else {
  38 + $icon.css('color', '#666');
  39 + this.selector.clear.removeClass('hide');
  40 + $searchAssociate.show();
  41 + }
  42 +
  43 + // 联动搜索
  44 + $.ajax({
  45 + url: '/product/search/fuzzyDatas',
  46 + data: {
  47 + keyword: this.selector.input.val()
  48 + },
  49 + dataType: 'json',
  50 + success: (data) => {
  51 + let ajaxHtml = '';
  52 + let i;
  53 +
  54 + if (data.length > 0) {
  55 + for (i = 0; i < data.length; i++) {
  56 + ajaxHtml += '<li><span class="keyword">' + data[i].keyword + '</span><span class="count">' +
  57 + data[i].count + ' items<i class="iconfont">&#xe614;</i></span></li>';
  58 + }
  59 +
  60 + $searchAssociate.html(ajaxHtml);
  61 + $searchItems.hide();
  62 + } else {
  63 + $searchAssociate.html('');
  64 + }
  65 +
  66 + $searchAssociate.find('li').on('touchend', function() {
  67 + this.selector.buriedpoint.val($(this).find('.keyword').html());
  68 + this.selector.search.closest('form').submit();
  69 + });
  70 + },
  71 + error: () => {
  72 + Tip.show('网络断开连接了~');
  73 + }
  74 + });
  75 + });
  76 + }
  77 +}
  78 +
  79 +export default new SearchList();