list-search.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" :up="upOption" @up="upCallback">
  3. <view class="item">
  4. <text class="tip">热门搜索:</text>
  5. <text class="hot-word" @click="doSearch('奶粉')">奶粉</text>
  6. <text class="hot-word" @click="doSearch('面霜')">面霜</text>
  7. <text class="hot-word" @click="doSearch('图书')">图书</text>
  8. </view>
  9. <view class="item">
  10. <text class="tip">关键词:</text>
  11. <input class="word-input" placeholder="请输入搜索关键词" v-model="curWord" @input="inputWord"/>
  12. </view>
  13. <good-list :list="goods"></good-list>
  14. </mescroll-body>
  15. </template>
  16. <script>
  17. import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
  18. import {apiSearch} from "@/api/mock.js"
  19. export default {
  20. mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
  21. data() {
  22. return {
  23. upOption: {
  24. // auto: false, //是否在初始化后,自动执行上拉回调callback; 默认true
  25. // page: {
  26. // num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
  27. // size: 10 // 每页数据的数量
  28. // }
  29. noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
  30. empty: {
  31. tip: '~ 搜索无结果 ~' // 提示
  32. }
  33. },
  34. goods: [], // 数据列表
  35. curWord:"" //当前搜索关键词
  36. }
  37. },
  38. methods: {
  39. // 输入监听
  40. inputWord(e){
  41. // this.curWord = e.detail.value // 已使用v-model,无需再次赋值
  42. // 节流,避免输入过快多次请求
  43. this.searchTimer && clearTimeout(this.searchTimer)
  44. this.searchTimer = setTimeout(()=>{
  45. this.doSearch(this.curWord)
  46. },300)
  47. },
  48. // 搜索
  49. doSearch(word){
  50. this.curWord = word
  51. this.goods = []; // 先清空列表,显示加载进度
  52. this.mescroll.resetUpScroll();
  53. },
  54. /*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
  55. upCallback(page) {
  56. //联网加载数据
  57. apiSearch(page.num, page.size, this.curWord).then(curPageData=>{
  58. //联网成功的回调,隐藏下拉刷新和上拉加载的状态;
  59. this.mescroll.endSuccess(curPageData.length);
  60. //如果是第一页需手动制空列表
  61. if(page.num == 1) this.goods = [];
  62. //追加新数据
  63. this.goods=this.goods.concat(curPageData);
  64. }).catch(()=>{
  65. //联网失败, 结束加载
  66. this.mescroll.endErr();
  67. })
  68. }
  69. }
  70. }
  71. </script>
  72. <style>
  73. /*关键词搜索*/
  74. .item{
  75. padding: 20rpx;
  76. }
  77. .tip{
  78. font-size: 30rpx;
  79. vertical-align: middle;
  80. }
  81. .hot-word{
  82. font-size: 24rpx;
  83. margin-left: 30rpx;
  84. padding: 6rpx 40rpx;
  85. border: 2rpx solid #FF6990;
  86. border-radius: 100rpx;
  87. vertical-align: middle;
  88. color: #FF6990;
  89. }
  90. .word-input{
  91. display: inline-block;
  92. width: 60%;
  93. height: 50rpx;
  94. line-height: 50rpx;
  95. font-size: 24rpx;
  96. margin-left: 30rpx;
  97. border: 2rpx solid #18B4FE;
  98. border-radius: 60rpx;
  99. text-align: center;
  100. background-color: #fff;
  101. vertical-align: middle;
  102. }
  103. </style>