brandSearch.js 2.29 KB

import { getYHStorageSync } from '../../utils/util';

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    all_list: {
      type: Object,
      value: {},
    },
    hotKeyword: {
      type: Object,
      value: {},
    }
  },

  /**
   * 私有数据,组件的初始数据
   * 可用于模版渲染
   */
  data: {
     result: [],
     showResult: false,
     hasResult: false,

     searchHistory: [],
  },

  ready: function () {
    
    let searchHistory = getYHStorageSync('searchHistory', 'brandSearch') ? getYHStorageSync('searchHistory','brandSearch') : []; 
    this.setData({
      searchHistory,
    });
  },

  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    //搜索框文字改变
    inputChanged: function (event) {
      let text = event.detail.value;
      text = text.toLowerCase();
      if (text) {
        let all_list = this.properties.all_list;
        let hasResult = false;
        let result = [];
        for (let item of all_list) {
          let list = item.list;
          list && list.map((value, key) => {
            let name = value.name.toLowerCase();
            if (name.includes(text)) { 
              result.push(value);
              hasResult = true;
            }
          })
        }
        this.setData({
          hasResult,
          showResult: true,
          result,
        }) 
      }else {
        this.setData({
          hasResult: false,
          showResult: false,
          result: {},
        }) 
      }
    },

    brandItemTapped: function(event) {
      let that = this;
      let brandItem = event.currentTarget.dataset.brandItem;
      wx.navigateTo({
        url: './brandDetail?brandId=' + brandItem.id + '&brandName=' + brandItem.name,
        success: function(res) {
          that.triggerEvent('dismissSearchView', {}); 
        }
      })
    },

    //搜索框获取焦点焦点
    focusChanged: function (event) {
      // console.log('focusChanged')
    },

    //搜索框键盘点击确定
    inputConfirmed: function (event) {
      // console.log('inputConfirmed')

    },


    cancelSearch: function (event) {
    },
  }
})