SearchHeader.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. export default {
  38. props: {
  39. placeholder: {
  40. type: String,
  41. default: '请输入要查找的内容'
  42. },
  43. similarUrl: {
  44. type: String,
  45. default: '/search/similarKeywords'
  46. },
  47. type: {
  48. type: String,
  49. default: 'default'
  50. }
  51. },
  52. data () {
  53. return {
  54. keyword: '',
  55. similarList: {},
  56. showSimilarWord: false
  57. }
  58. },
  59. mounted () {
  60. this.$nextTick(() => {
  61. document.onclick = () => {
  62. this.showSimilarWord = false
  63. }
  64. })
  65. },
  66. methods: {
  67. onSearch: function (key, type, e) {
  68. if (e) {
  69. e.stopPropagation()
  70. }
  71. if (key) {
  72. this.keyword = key
  73. this.$emit('searchAction', {
  74. keyword: this.keyword,
  75. type: type
  76. })
  77. } else {
  78. this.$emit('searchAction', {
  79. keyword: this.keyword
  80. })
  81. }
  82. this.showSimilarWord = false
  83. },
  84. getSimilarList: function () {
  85. if (this.keyword && this.keyword !== '') {
  86. this.$http.get(this.similarUrl, {params: {keyword: this.keyword}}).then(
  87. res => {
  88. this.similarList = res.data
  89. this.showSimilarWord = true
  90. }
  91. )
  92. }
  93. }
  94. }
  95. }
  96. </script>
  97. <style lang="scss" scoped>
  98. .search-content {
  99. color: #333;
  100. input {
  101. margin: 0 0 0 .5rem;
  102. line-height: normal;
  103. }
  104. ul {
  105. width: 6.48rem;
  106. background: #fff;
  107. position: absolute;
  108. left: .6rem;
  109. top: .72rem;
  110. border: 1px solid #ccc;
  111. border-radius: .05rem;
  112. li {
  113. height: .6rem;
  114. line-height: .6rem;
  115. padding: 0 .1rem;
  116. font-size: .26rem;
  117. &.title {
  118. color: #666;
  119. border-bottom: 1px solid #ddd;
  120. font-weight: bold;
  121. background: #f6f5f5;
  122. }
  123. }
  124. }
  125. }
  126. </style>