当前位置:   article > 正文

uni-app封装表格组件_uniapp table组件

uniapp table组件

组件代码:

  1. <template>
  2. <view>
  3. <uni-table class="tableBox" border stripe emptyText="">
  4. <!-- 表头行 -->
  5. <uni-tr class="tableTr">
  6. <uni-th :sortable="item.sortable" :class="item.sortable ? 'active':''" @click.native="cloumClick(item)" align="center" v-for="item in tableDataCloums">{{item.title}}</uni-th>
  7. </uni-tr>
  8. <!-- 表格数据行 -->
  9. <uni-tr v-for="(el,index) in tableDataList">
  10. <template v-for="item in tableDataCloums">
  11. <uni-td align="center" v-if="item.prop !== 'operation'">
  12. <view v-if="!Boolean(item.slot)">{{getData(item,el,index)}}</view>
  13. <component v-else :is="item.slot" :imgSrc="getData(item,el,index)" :rank="el.id" :text="getData(item,el,index)" :type="filterType(getData(item,el,index))"></component>
  14. </uni-td>
  15. <uni-td align="center" v-else>
  16. <view class="btnBox">
  17. <u-button v-for="operationItem in getData(item,el,index)" @click="operationItemClick(operationItem,el)" :type="operationItem.type" :text="operationItem.text"></u-button>
  18. </view>
  19. </uni-td>
  20. </template>
  21. </uni-tr>
  22. </uni-table>
  23. <u-empty v-if="tableDataList.length === 0" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"/>
  24. <!--分页-->
  25. <view class="pages" v-if="showPages && tableDataList.length !== 0">
  26. <u-button type="primary" @click="pageChange('pre')">{{'<'+'上一页'}}</u-button>
  27. <u-button type="primary" @click="pageChange('next')">{{'下一页'+'>'}}</u-button>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import tags from './components/tags.vue'
  33. import imgBox from './components/imgBox.vue'
  34. import rank from './components/rank.vue'
  35. export default {
  36. components:{tags,imgBox,rank},
  37. props:{
  38. tableDataCloums:{
  39. type:Array,
  40. default:[]
  41. },
  42. tableDataList:{
  43. type:Array,
  44. default:[]
  45. },
  46. showPages:{
  47. type:Boolean,
  48. default:true
  49. }
  50. },
  51. data(){
  52. return{
  53. warningArr:['退款中'],
  54. errorArr:['未支付','拒绝退款','已结账'],
  55. successArr:['已退款','已支付']
  56. }
  57. },
  58. methods: {
  59. cloumClick(item){
  60. if(item.fun){
  61. this.$emit(item.fun)
  62. }
  63. },
  64. filterType(text){
  65. if(this.warningArr.includes(text)){
  66. return 'warning'
  67. }else if(this.errorArr.includes(text)){
  68. return 'error'
  69. }else{
  70. return 'success'
  71. }
  72. },
  73. pageChange(type){
  74. this.$emit('pageNumChange',type)
  75. },
  76. operationItemClick(operationItem,el){
  77. this.$emit(operationItem.fun,el)
  78. },
  79. getData(item,el,index){
  80. if(item.prop == 'operation'){
  81. return item.operation
  82. }
  83. return el[item.prop]
  84. }
  85. },
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .active{
  90. color: #1354E0 !important;
  91. }
  92. .pages{
  93. display: flex;
  94. justify-content: center;
  95. .u-button{
  96. margin: torpx(30);
  97. width: 30%;
  98. }
  99. }
  100. .table--border{
  101. border: none;
  102. }
  103. .tableTr{
  104. background: #DAE3F5;
  105. }
  106. .uni-table-th{
  107. font-size: torpx(24);
  108. color: #212121;
  109. }
  110. .uni-table-td{
  111. font-size: torpx(20);
  112. }
  113. .btnBox{
  114. display: flex;
  115. .u-button{
  116. margin: 0 torpx(10);
  117. }
  118. }
  119. </style>

组件使用代码:

  1. const tableDataCloums = [
  2. {title:'会员卡编号',prop:'id'},
  3. {title:'会员电话',prop:'phone'},
  4. {title:'会员等级',prop:'vipGrade'},
  5. {title:'会员昵称',prop:'nickName'},
  6. {title:'当前积分',prop:'pointsCurrent'},
  7. {title:'等级权益',prop:'pointsCurrent'},
  8. {title:'余额',prop:'pointsCurrent'},
  9. {title:'开卡时间',prop:'createTime'},
  10. {title:'最近下单',prop:'updateTime'},
  11. {title:'操作',
  12. prop:'operation',
  13. operation:[
  14. {text:'充值',fun:'recharge',type:'primary'},
  15. {text:'充值记录',fun:'record',type:'warning'}
  16. ]
  17. },
  18. ]
  19. <comsTable :tableDataCloums="balanceTableDataCloums" :tableDataList="balanceTableDataList"></comsTable>

页面样子:

加入了动态组件,需要在tableDataCloums传值中添加slot属性,我在项目中使用到了三个动态组件,页面展示

1.tags动态组件是对应的支付状态

2.imgBox对应的是图片

3.rank组件对应的排名

在报表和这个页面也可以看出来表格是可以排序的只要在tableDataCloums中传sorttable:true,即有排序样式,当然排序逻辑需要自己写,也可以看我之前的博客

 
 

                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/264055
推荐阅读