• uniapp 之 tab栏 切换数据渲染不同状态


    前言

    前面讲解些 tab栏是如何切换的,那下面就给大家演示下

     它们的状态是怎么切换过来的,很简单

     上面就是最终的效果图

    最开始时,后端把全部订单的所有数据,包含一些状态都返给我了,

    我开始想的,是通过 先过滤数组,找到自己需要的状态对应的数组,然后把那个数组传递给tab栏切换到对应的页面,通俗一点就是,把已完成状态的数组,传递给已完成那个组件,进而渲染已完成的数据,但,最终发现,这样一些的话,要 C全部订单的 样式,写起来有些麻烦,

    后来我请教大哥,大哥说,他们那边会给我一个带有状态值的接口,当值为 1 时,就渲染全部订单,为三时就渲染已完成的数据

    这样一来,我就轻松多了,就不用把值传来传去的了

    具体代码,如下

    这里面的话 是因为后端反的状态值 是 1 , 2, 3 就对应是的 下面的index

    1. <template>
    2. <view>
    3. <view class="tab_nav">
    4. <view class="navTitle" v-for="(item,index) in navList" :key="index">
    5. <view :class="{'active':current === index}" @click="checked(index,item.index)">
    6. {{item.title}}
    7. </view>
    8. </view>
    9. </view>
    10. <view class="nav_item">
    11. <Indent :indentList="indentList"></Indent>
    12. </view>
    13. </view>
    14. </template>
    15. <script>
    16. export default {
    17. data() {
    18. return {
    19. current: 0,
    20. navList: [{
    21. index: '1',
    22. title: '全部订单'
    23. }, {
    24. index: '2',
    25. title: "进行中"
    26. }, {
    27. index: '3',
    28. title: "已完成"
    29. }],
    30. indentList: [],
    31. pageSize: 10,
    32. pageNum: 1,
    33. // 总条数
    34. total: 0,
    35. isLoading: false,
    36. orderStatus: '1'
    37. }
    38. },
    39. onLoad() {
    40. this.getIndent()
    41. },
    42. // 上拉刷新
    43. onPullDownRefresh() {
    44. this.pageNum = 1
    45. this.indentList = []
    46. this.getIndent(uni.stopPullDownRefresh)
    47. },
    48. // 触底事件
    49. onReachBottom() {
    50. // 判断当前数据是否已经是全部数据
    51. if (this.pageNum * this.pageSize >= this.total) {
    52. return uni.$showMsg('已经到底了')
    53. }
    54. if (this.isLoading) return
    55. //如果是在请求过程中,就不允许翻页 - 节流
    56. //让页数加1
    57. this.pageNum++
    58. this.getIndent()
    59. },
    60. methods: {
    61. checked(index, res) {
    62. this.current = index
    63. this.orderStatus = res
    64. this.pageNum = 1
    65. // 清空
    66. this.indentList = []
    67. this.getIndent()
    68. },
    69. // 发送查询订单请求
    70. async getIndent(orderFn) {
    71. this.isLoading = true
    72. const params = {
    73. pageSize: this.pageSize,
    74. pageNum: this.pageNum,
    75. orderStatus: this.orderStatus
    76. }
    77. const {
    78. data: {
    79. obj
    80. }
    81. } = await uni.$http.post('/uniapp/order/getOrderList',
    82. params
    83. )
    84. this.isLoading = false
    85. // 判断是不是在下拉刷新中调用,如果是,关闭
    86. orderFn && orderFn()
    87. console.log('查询订单信息', obj);
    88. // this.indentList = [...this.indentList, ...obj.list]
    89. this.indentList = obj.list
    90. this.total = obj.total
    91. console.log(this.indentList, this.total);
    92. },
    93. },
    94. }
    95. </script>
    96. <style lang="scss" scoped>
    97. .tab_nav {
    98. display: flex;
    99. justify-content: center;
    100. align-items: center;
    101. }
    102. .tab_nav .navTitle {
    103. height: 90rpx;
    104. line-height: 90rpx;
    105. width: 100%;
    106. text-align: center;
    107. font-size: 32rpx;
    108. font-family: Alibaba PuHuiTi;
    109. color: #333;
    110. }
    111. .active {
    112. position: relative;
    113. color: #1F75FE;
    114. }
    115. .active::after {
    116. content: "";
    117. position: absolute;
    118. width: 100rpx;
    119. height: 4rpx;
    120. background-color: #1F75FE;
    121. left: 0px;
    122. right: 0px;
    123. bottom: 0px;
    124. margin: auto;
    125. }
    126. </style>

    最主要的是这句代码 

    1. checked(index, res) {
    2. this.current = index
    3. this.orderStatus = res
    4. this.getIndent()
    5. },

    补充

    1. checked(index, res) {
    2. this.current = index
    3. this.orderStatus = res
    4. this.pageNum = 1
    5. // 清空
    6. this.indentList = []
    7. this.getIndent()
    8. },

      这里面添加了 this.pageNum = 1 以及 this.indentList = []
      
      是因为我在后续中,发现 若不加 this.pageNum = 1,它切换过去的时候,会展示pageNum 为2的数据,除非下拉刷新的时候才会展示 第一页的数据

  • 相关阅读:
    Microsoft 365跨平台协同办公功能,实现Mac、iOS、Windows用户的实时无缝协作
    核对不同文件夹所含内容的差异并提取缺失内容:Python代码
    2022.7.30 C++——final和override关键字
    经典面试题:为什么 ConcurrentHashMap 的读操作不需要加锁?
    【机器学习】鸢尾花数据的基本信息 || sklearn
    【面试题】近期学员被问最多的真实面试题记录(如何分配测试任务?)
    微信小程序---组件开发与使用
    慕测练习题NextDay
    C++并发之锁(std::lock_guard,std::unique_lock)
    算法解析:LeetCode——机器人碰撞和最低票价
  • 原文地址:https://blog.csdn.net/LJM51200/article/details/128064063