search.vue 3.4 KB

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