search.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="u-margin-left-20 u-margin-right-20 u-margin-top-20">
  3. <u-search v-model="keyword" :focus="true" action-text="取消"
  4. placeholder="请输入搜索关键字" @custom="cancelSearch" @search="clickSearch(value)"></u-search>
  5. <!-- 搜索记录 -->
  6. <template v-if="historyList.length > 0">
  7. <view style="position: relative;">
  8. <u-card :border="false" :head-border-bottom="false" padding="0" title="搜索记录" title-size="28">
  9. <view slot="body">
  10. <u-cell-group :border="false">
  11. <u-cell-item v-for="(item,index) in historyList" :key="index" :arrow="false" :border-bottom="false"
  12. :title="item" @click="clickSearchTag(item)">
  13. <!-- <u-icon slot="icon" size="32" name="search"></u-icon> -->
  14. </u-cell-item>
  15. </u-cell-group>
  16. </view>
  17. </u-card>
  18. <view class="arrow-right" @click="clearHistory">
  19. <u-icon name="trash"></u-icon>
  20. 清除
  21. </view>
  22. </view>
  23. </template>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. historyList: [],
  31. keyword: "",
  32. }
  33. },
  34. onLoad() {
  35. // 加载历史记录
  36. let history = uni.getStorageSync('searchHistory')
  37. this.historyList = history ? JSON.parse(history) : []
  38. },
  39. onReady() {
  40. },
  41. methods: {
  42. clickSearch() {
  43. if (this.keyword === '') {
  44. return uni.showToast({
  45. title: '关键词不能为空',
  46. icon: 'none'
  47. });
  48. } else {
  49. uni.hideKeyboard()
  50. this.addHistory()
  51. this.$u.route('/pages/search/searchList')
  52. }
  53. },
  54. // 自定义取消搜索事件
  55. cancelSearch() {
  56. uni.navigateBack({
  57. delta: 1
  58. })
  59. },
  60. clickSearchTag(item) {
  61. this.keyword = item
  62. this.clickSearch()
  63. },
  64. // 加入搜索记录
  65. addHistory() {
  66. let index = this.historyList.indexOf(this.keyword)
  67. let history = this.historyList;
  68. if (index === -1) {
  69. history.unshift(this.keyword)
  70. } else {
  71. history.unshift(history.splice(index, 1)[0])
  72. }
  73. uni.setStorageSync('searchHistory', JSON.stringify(history))
  74. },
  75. clearHistory() {
  76. // 清除搜索记录
  77. uni.showModal({
  78. title: '提示',
  79. content: '是否清除搜索历史?',
  80. cancelText: '取消',
  81. confirmText: '确认',
  82. success: res => {
  83. if (res.confirm) {
  84. uni.clearStorageSync();
  85. this.historyList = []
  86. this.keyword = ''
  87. this.$mytip.toast('清除成功')
  88. }
  89. }
  90. });
  91. }
  92. }
  93. }
  94. </script>
  95. <style>
  96. page {
  97. background: #FFFFFF;
  98. }
  99. </style>
  100. <style lang="scss" scoped>
  101. .item-title {
  102. font-size: 28 rpx;
  103. color: $u-main-color;
  104. font-weight: bold;
  105. }
  106. .item-price {
  107. font-weight: normal;
  108. font-size: 35 rpx;
  109. color: $u-type-warning;
  110. margin-top: 3px;
  111. }
  112. .item-desc {
  113. font-weight: normal;
  114. font-size: 26 rpx;
  115. color: $u-tips-color;
  116. }
  117. .item-tag {
  118. font-size: 24 rpx;
  119. color: $u-tips-color;
  120. margin-top: 3px;
  121. }
  122. .arrow-right {
  123. position: absolute;
  124. top: 0 rpx;
  125. right: 28 rpx;
  126. font-weight: normal;
  127. font-size: 28 rpx;
  128. color: $u-tips-color;
  129. }
  130. </style>