微信小程序实现tab切换和数据列表

问题背景

上篇文章介绍了微信小程序实现tab切换的一种方案(参考 https://blog.51cto.com/baorant24/6188157 ),感觉代码不是很精简,本文再用一个demo介绍微信小程序如何实现tab切换和数据列表。

问题分析

微信小程序对应页面文件结构如下: image.png

问题解决

话不多说,直接上代码: (1)index.js文件,代码如下:

  1. // pages/healdata/healthydata.ts
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. currentIndex: 0, //默认是活动项
  8. home: "#979797",
  9. find: "#979797",
  10. services: "#00c2a5",
  11. mind: "#979797",
  12. homeChoose: false,
  13. findChoose: false,
  14. servicesChoose: true,
  15. mindChoose: false,
  16. msg: {},//消息数
  17. noNewMsg: true,//true表示新有新消息
  18. servicesAppiontData: [//服务预约数据列表
  19. {
  20. }],
  21. listItem: [
  22. {
  23. "deptId": 2098494533,
  24. "packageId": 1,
  25. "packageName": "院内服务",
  26. "services": [
  27. {
  28. "serviceId": 1,
  29. "deptId": 2098494533,
  30. "brandName": "PICC护理",
  31. "serviceType": "1"
  32. },
  33. {
  34. "serviceId": 2,
  35. "deptId": 2098494533,
  36. "brandName": "压疮护理",
  37. "serviceType": "1"
  38. },
  39. {
  40. "serviceId": 3,
  41. "deptId": 2098494533,
  42. "brandName": "导尿管护理",
  43. "serviceType": "1"
  44. },
  45. {
  46. "serviceId": 4,
  47. "deptId": 2098494533,
  48. "brandName": "胃管护理",
  49. "serviceType": "1",
  50. }
  51. ]
  52. },
  53. {
  54. "deptId": 2098494533,
  55. "packageId": 2,
  56. "packageName": "中医服务",
  57. "services": [
  58. {
  59. "serviceId": 7,
  60. "deptId": 2098494533,
  61. "brandName": "失眠",
  62. "serviceType": "2"
  63. },
  64. {
  65. "serviceId": 8,
  66. "deptId": 2098494533,
  67. "brandName": "便秘",
  68. "serviceType": "2"
  69. },
  70. {
  71. "serviceId": 9,
  72. "deptId": 2098494533,
  73. "brandName": "颈椎病",
  74. "serviceType": "2"
  75. }
  76. ]
  77. },
  78. {
  79. "deptId": 2098494533,
  80. "packageId": 2,
  81. "packageName": "中医服务",
  82. "services": [
  83. {
  84. "serviceId": 10,
  85. "deptId": 2098494533,
  86. "brandName": "便秘",
  87. "serviceType": "2"
  88. },
  89. {
  90. "serviceId": 11,
  91. "deptId": 2098494533,
  92. "brandName": "颈椎病",
  93. "serviceType": "2"
  94. }
  95. ]
  96. },
  97. ],//数据列表数据
  98. patintIdS: ''
  99. },
  100. /**
  101. * 生命周期函数--监听页面加载
  102. */
  103. onLoad() {
  104. },
  105. //点击tab时触发
  106. titleClick: function (e) {
  107. this.setData({
  108. //拿到当前索引并动态改变
  109. currentIndex: e.currentTarget.dataset.idx
  110. })
  111. // console.log("======拿到当前索引并动态改变=dddd======")
  112. },
  113. /**
  114. * 生命周期函数--监听页面初次渲染完成
  115. */
  116. onReady() {
  117. },
  118. /**
  119. * 生命周期函数--监听页面显示
  120. */
  121. onShow() {
  122. wx.hideHomeButton()
  123. },
  124. /**
  125. * 生命周期函数--监听页面隐藏
  126. */
  127. onHide() {
  128. },
  129. /**
  130. * 生命周期函数--监听页面卸载
  131. */
  132. onUnload() {
  133. },
  134. /**
  135. * 页面相关事件处理函数--监听用户下拉动作
  136. */
  137. onPullDownRefresh() {
  138. },
  139. /**
  140. * 页面上拉触底事件的处理函数
  141. */
  142. onReachBottom() {
  143. },
  144. /**
  145. * 用户点击右上角分享
  146. */
  147. onShareAppMessage() {
  148. }
  149. })

(2)index.json文件,代码如下:

  1. {
  2. "usingComponents": {},
  3. "navigationBarTitleText": "分类列表",
  4. "navigationStyle": "custom"
  5. }

(3)index.wxml文件,代码如下:

  1. <!-- 头部搜索 -->
  2. <view class="title_search">
  3. </view>
  4. <!-- 头部搜索 -->
  5. <!-- Tab布局 -->
  6. <view catchtouchmove='true'>
  7. <view class='navBox'>
  8. <view class='titleBox1' id="allaid" bindtap='titleClick' data-idx='0'>
  9. <text class="{{0 == currentIndex ? 'fontColorBox' : 'fontColorBox1'}}">服务预约</text>
  10. <hr class="{{0 == currentIndex ? 'lineBox' : 'notLineBox'}}" />
  11. </view>
  12. <view class='titleBox2' bindtap='titleClick' data-idx='1'>
  13. <text class="{{1 == currentIndex ? 'fontColorBox' : 'fontColorBox1'}}">订单消息</text>
  14. <hr class="{{1 == currentIndex ? 'lineBox' : 'notLineBox'}} " />
  15. </view>
  16. </view>
  17. <!-- 内容布局 -->
  18. <swiper class='swiperTtemBox' bindchange='pagechange' current='{{currentIndex}}'>
  19. <!--服务预约 -->
  20. <swiper-item class='swiperTtemBox' catchtouchmove='onTouchMove'>
  21. <!-- 服务预约列表 -->
  22. <scroll-view class="box-scroll" scroll-y='true'>
  23. <!-- 服务列表 -->
  24. <block wx:for="{{listItem}}" wx:key="id" wx:for-item="itemName" wx:for-index="id">
  25. <view class="up_service">
  26. <view class="up_service_content">
  27. <!-- 上门服务 -->
  28. <view class="up_service_title_txt">
  29. <text>
  30. {{itemName.packageName }}
  31. </text>
  32. </view>
  33. <!-- 服务内容 -->
  34. <view class="upitem_list">
  35. <block wx:for="{{itemName.services.length}}" wx:key="ids" wx:for-item="chilgrenItem" wx:for-index="ids">
  36. <view class="item_nursin" catchtap="nursin_servies" id="{{itemName.services[ids].serviceId}}">
  37. <text class="up_ser_it_txt" id="up_ser_it_txt_id" catchtap="nursin_servies" id="{{itemName.services[ids].serviceId}}">{{itemName.services[ids].brandName}}</text>
  38. </view>
  39. </block>
  40. </view>
  41. </view>
  42. </view>
  43. </block>
  44. </scroll-view>
  45. </swiper-item>
  46. <!-- 订单消息 -->
  47. <swiper-item class='swiperTtemBox' catchtouchmove='onTouchMove'>
  48. <view class="order_msg_md">暂无订单消息</view>
  49. </swiper-item>
  50. </swiper>
  51. </view>

(4)index.wxss文件,代码如下:

  1. Page {
  2. background: #f0f0f0;
  3. height: 100%;
  4. position: fixed;
  5. }
  6. /* 头部搜索 */
  7. /* 搜索标题 */
  8. .title_search{
  9. background: linear-gradient(to right, #0455a7, #62c8ec);
  10. height: 170rpx;
  11. width: 100%;
  12. display: flex;
  13. flex-direction: row;
  14. align-items: flex-end;
  15. justify-content: flex-start;
  16. }
  17. .seeck_md{
  18. display: flex;
  19. flex-direction: row;
  20. width: 100%;
  21. justify-content: space-between;
  22. align-items: flex-end;
  23. }
  24. .ahead_md{
  25. display: flex;
  26. flex-direction: row;
  27. width: 100%;
  28. margin-bottom: 20rpx;
  29. }
  30. .title_icon_ss{
  31. height: 70rpx;
  32. width: 70rpx;
  33. background-color: white;
  34. border-radius: 35rpx;
  35. margin-left: 20rpxrpx;
  36. }
  37. /* 搜索部分bg*/
  38. .search_md{
  39. background-color: #69ccf1;
  40. margin-left: 10rpx;
  41. height: 70rpx;
  42. width: 390rpx;
  43. border-radius: 35rpx;
  44. display: flex;
  45. flex-direction: row;
  46. align-items: center;
  47. justify-content: space-between;
  48. }
  49. /* 搜索 */
  50. .seeck_ic{
  51. height: 46rpx;
  52. width: 46rpx;
  53. margin-left: 20rpx;
  54. border-radius: 23rpx;
  55. }
  56. /* 语音 */
  57. .vioce_icon{
  58. height: 46rpx;
  59. width: 46rpx;
  60. border-radius: 23rpx;
  61. margin-right: 20rpx;
  62. }
  63. /* 消息 */
  64. .msg{
  65. width: 100rpx;
  66. height: 90rpx;
  67. display: flex;
  68. flex-direction: column;
  69. justify-content: center;
  70. align-items: center;
  71. margin-right: 0rpx;
  72. margin-left: 30rpx;
  73. }
  74. /* 消息提示 */
  75. .msg_tag{
  76. position: absolute;
  77. background-color: #ff6162;
  78. width: 14rpx;
  79. height: 14rpx;
  80. border-radius: 7rpx;
  81. margin-bottom: 72rpx;
  82. margin-left: 24rpx;
  83. }
  84. .msg_ic{
  85. height: 44rpx;
  86. width: 48rpx;
  87. margin-right: 2rpx;
  88. margin-right: 10rpx;
  89. }
  90. .msg_txt{
  91. font-size: 24rpx;
  92. height: 40rpx;
  93. width: 60rpx;
  94. margin-bottom: 20rpx;
  95. align-items: flex-start;
  96. color: #fff;
  97. display: flex;
  98. justify-content: flex-start;
  99. }
  100. /* 退出 */
  101. .logout{
  102. width: 100rpx;
  103. height: 90rpx;
  104. display: flex;
  105. flex-direction: column;
  106. justify-content: center;
  107. align-items: center;
  108. margin-right: 30rpx;
  109. }
  110. .logout_ic{
  111. height: 44rpx;
  112. width: 48rpx;
  113. margin-right: 2rpx;
  114. }
  115. .logout_txt{
  116. font-size: 24rpx;
  117. height: 40rpx;
  118. width: 60rpx;
  119. margin-bottom: 20rpx;
  120. align-items: flex-start;
  121. color: #fff;
  122. display: flex;
  123. justify-content: flex-start;
  124. }
  125. /* 搜索标题 */
  126. /* 头部搜索 */
  127. .box-scroll{
  128. background-color: #EFEFEF;
  129. width: 100%;
  130. flex: 1 auto;
  131. height: calc(100vh - 260rpx);
  132. left: 0rpx;
  133. right: 0rpx;
  134. top: 0rpx;
  135. padding-bottom: 0rpx;
  136. }
  137. /* .box-scroll {
  138. flex: 1 auto;
  139. height: calc(100vh - 390rpx);
  140. left: 0rpx;
  141. right: 0rpx;
  142. top: 0rpx;
  143. padding-bottom: 20rpx;
  144. } */
  145. /* 顶部切换 */
  146. .navBox {
  147. /* 顶部tab盒子样式 */
  148. width: 100%;
  149. height: 80rpx;
  150. background: white;
  151. display: flex;
  152. flex-direction: row;
  153. align-items: center;
  154. justify-content: center;
  155. }
  156. /* 文字默认颜色 */
  157. .fontColorBox {
  158. color: #07c8ae;
  159. }
  160. .fontColorBox1 {
  161. color: black;
  162. }
  163. /* 文字默认颜色 */
  164. .titleBox1,
  165. .titleBox2 {
  166. /* 未选中文字的样式 */
  167. width: 50%;
  168. font-size: 30rpx;
  169. height: 100%;
  170. display: flex;
  171. flex-direction: column;
  172. align-items: center;
  173. justify-content: flex-end;
  174. padding-top: 10rpx;
  175. }
  176. .lineBox,
  177. .notLineBox {
  178. /* 选中及未选中底线共同样式 */
  179. width: 100%;
  180. height: 4rpx;
  181. margin-top: 16rpx;
  182. border-radius: 4rpx;
  183. }
  184. .lineBox {
  185. /* 选中底线样式 */
  186. background: #06c7ae;
  187. width: 100%;
  188. height: 4rpx;
  189. margin-top: 16rpx;
  190. border-radius: 4rpx;
  191. }
  192. .notLineBox {
  193. /* 未选中底线样式 */
  194. background: transparent;
  195. width: 100%;
  196. height: 4rpx;
  197. }
  198. /* 底部内容样式 */
  199. .swiperTtemBox {
  200. height: 100vh;
  201. overflow: scroll;
  202. margin: 0rpx 0rpx;
  203. background: #f0f0f0;
  204. border-radius: 30rpx;
  205. font-size: 28rpx;
  206. margin-top: 5rpx;
  207. }
  208. /* 顶部切换 */
  209. .up_service {
  210. justify-content: left;
  211. padding-left: 40rpx;
  212. padding-right: 40rpx;
  213. }
  214. .up_service_content {
  215. justify-content: space-between;
  216. }
  217. .up_service_title_txt {
  218. color: #666666;
  219. }
  220. .upitem_list {
  221. display: flex;
  222. flex-direction: row;
  223. justify-content: space-between;
  224. flex-wrap: wrap;
  225. }
  226. .item_nursin {
  227. width: 320rpx;
  228. height: 200rpx;
  229. display: flex;
  230. flex-direction: column;
  231. justify-content: center;
  232. align-items: center;
  233. flex-shrink: 0;
  234. flex-grow: 0;
  235. margin-top: 30rpx;
  236. background-color: white;
  237. border-radius: 20rpx;
  238. }
  239. .up_service_icon {
  240. width: 130rpx;
  241. height: 110rpx;
  242. border-radius: 10rpx;
  243. margin-top: 10rpx;
  244. }
  245. #up_ser_it_txt_id {
  246. color: #1b1b1b;
  247. font-size: 32rpx;
  248. margin-top: 10rpx;
  249. }
  250. .cn_medicine {
  251. margin-top: 30rpx;
  252. display: flex;
  253. flex-direction: row;
  254. justify-content: space-between;
  255. }
  256. /* 院内服务 */
  257. .hospital_service {
  258. justify-content: left;
  259. padding-left: 40rpx;
  260. padding-right: 40rpx;
  261. margin-top: 10rpx;
  262. }
  263. .order_msg_md{
  264. display: flex;
  265. margin-top: 30rpx;
  266. align-items: center;
  267. justify-content: center;
  268. color: #919199;
  269. }
  270. .scroll-view_H {
  271. overflow: scroll;
  272. white-space: nowrap;
  273. background-color: white;
  274. margin-right: 30rpx;
  275. }
  276. .head_list {
  277. display: flex;
  278. /* display: inline-block; */
  279. flex-direction: row;
  280. background-color: white;
  281. height: 160rpx;
  282. width: 100%;
  283. justify-content: flex-start;
  284. align-items: center;
  285. }
  286. /* 家属选择背景 */
  287. .head_list_item_one {
  288. flex-shrink: 0;
  289. flex-grow: 0;
  290. display: flex;
  291. flex-direction: column;
  292. background-color: #f0f0f0;
  293. width: 140rpx;
  294. height: 140rpx;
  295. align-items: center;
  296. justify-content: center;
  297. margin-left: 30rpx;
  298. border-radius: 10rpx;
  299. }
  300. /* 家属选背景 */
  301. .head_list_item_one_ed {
  302. display: flex;
  303. flex-direction: column;
  304. background-color: #06c7ae;
  305. flex-shrink: 0;
  306. flex-grow: 0;
  307. width: 140rpx;
  308. height: 140rpx;
  309. align-items: center;
  310. justify-content: center;
  311. margin-left: 30rpx;
  312. border-radius: 10rpx;
  313. }
  314. .head_list_item_one_im_parent {
  315. background-color: white;
  316. width: 80rpx;
  317. height: 80rpx;
  318. border-radius: 40rpx;
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. }
  323. .head_list_item_one_im {
  324. width: 72rpx;
  325. height: 72rpx;
  326. border-radius: 36rpx;
  327. background-color: white;
  328. }
  329. .head_list_item_one_txt {
  330. margin-top: 6rpx;
  331. font-size: 26rpx;
  332. color: #252525;
  333. }
  334. .head_list_item_one_txt_ed {
  335. margin-top: 6rpx;
  336. font-size: 26rpx;
  337. color: #ffffff;
  338. }
  339. .first {
  340. margin-top: 20rpx;
  341. }

运行结果如下: 1681378325094.gif

问题总结

本文介绍了微信小程序如何实现tab切换和数据列表,有兴趣的同学可以进一步深入研究。