SearchHeader.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div class="search-content com-mobile-header">
  3. <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
  4. <input type="text" v-model="keyword" @input="getSimilarList()" :placeholder="placeholder" @keyup.13="onSearch">
  5. <span @click="onSearch()"><i class="iconfont icon-sousuo"></i></span>
  6. <ul v-if="type == 'supplier' && keyword && keyword !== '' && showSimilarWord">
  7. <template v-if="similarList.pCmpCode && similarList.pCmpCode.length">
  8. <li class="title text-ellipse">型号</li>
  9. <li class="text-ellipse" v-for="code in similarList.pCmpCode.slice(0, 4)" @click="onSearch(code.pCmpCode, 'pCmpCode', $event)">{{code.pCmpCode}}</li>
  10. </template>
  11. <template v-if="similarList.pBrandEn && similarList.pBrandEn.length">
  12. <li class="title text-ellipse">品牌</li>
  13. <li class="text-ellipse" v-for="brand in similarList.pBrandEn.slice(0, 4)" @click="onSearch(brand.nameCn, 'pBrandEn', $event)">{{brand.nameCn}}</li>
  14. </template>
  15. <template v-if="similarList.kind && similarList.kind.length">
  16. <li class="title text-ellipse">类目</li>
  17. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.kind, 'kind', $event)">{{kind.kind}}</li>
  18. </template>
  19. </ul>
  20. <ul v-if="type == 'default' && keyword && keyword !== '' && showSimilarWord">
  21. <template v-if="similarList.component && similarList.component.length">
  22. <li class="title text-ellipse">型号</li>
  23. <li class="text-ellipse" v-for="code in similarList.component.slice(0, 4)" @click="onSearch(code.code, 'code', $event)">{{code.code}}</li>
  24. </template>
  25. <template v-if="similarList.brand && similarList.brand.length">
  26. <li class="title text-ellipse">品牌</li>
  27. <li class="text-ellipse" v-for="brand in similarList.brand.slice(0, 4)" @click="onSearch(brand.nameCn, 'brand', $event)">{{brand.nameCn}}</li>
  28. </template>
  29. <template v-if="similarList.kind && similarList.kind.length">
  30. <li class="title text-ellipse">类目</li>
  31. <li class="text-ellipse" v-for="kind in similarList.kind.slice(0, 4)" @click="onSearch(kind.nameCn, 'kind', $event)">{{kind.nameCn}}</li>
  32. </template>
  33. </ul>
  34. </div>
  35. </template>
  36. <script>
  37. import {scrollTo} from '~utils/scroll'
  38. export default {
  39. props: {
  40. placeholder: {
  41. type: String,
  42. default: '请输入要查找的内容'
  43. },
  44. similarUrl: { // 联想词url
  45. type: String,
  46. default: '/search/similarKeywords'
  47. },
  48. type: { // 搜索类型
  49. type: String,
  50. default: 'default'
  51. },
  52. showSimilar: { // 是否显示联想词
  53. type: Boolean,
  54. default: true
  55. }
  56. },
  57. data () {
  58. return {
  59. keyword: '',
  60. similarList: {},
  61. showSimilarWord: false
  62. }
  63. },
  64. mounted () {
  65. this.$nextTick(() => {
  66. document.onclick = () => {
  67. this.showSimilarWord = false
  68. }
  69. })
  70. },
  71. methods: {
  72. onSearch: function (key, type, e) {
  73. if (e) {
  74. e.stopPropagation()
  75. }
  76. if (key) {
  77. this.keyword = key
  78. this.$emit('searchAction', {
  79. keyword: this.keyword,
  80. type: type
  81. })
  82. } else {
  83. this.$emit('searchAction', {
  84. keyword: this.keyword
  85. })
  86. }
  87. scrollTo('body', 10)
  88. this.showSimilarWord = false
  89. },
  90. getSimilarList: function () {
  91. if (this.showSimilar && this.keyword && this.keyword !== '') {
  92. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  93. res => {
  94. this.similarList = res.data
  95. this.showSimilarWord = true
  96. }
  97. )
  98. }
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. .search-content {
  105. color: #333;
  106. input {
  107. margin: 0 0 0 .5rem;
  108. line-height: normal;
  109. }
  110. ul {
  111. width: 6.48rem;
  112. background: #fff;
  113. position: absolute;
  114. left: .6rem;
  115. top: .72rem;
  116. border: 1px solid #ccc;
  117. border-radius: .05rem;
  118. li {
  119. height: .6rem;
  120. line-height: .6rem;
  121. padding: 0 .1rem;
  122. font-size: .26rem;
  123. &.title {
  124. color: #666;
  125. border-bottom: 1px solid #ddd;
  126. font-weight: bold;
  127. background: #f6f5f5;
  128. }
  129. }
  130. }
  131. }
  132. </style>