uni-tr.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view class="uni-table-tr">
  3. <checkbox-group v-if="selection === 'selection'" class="checkbox" :class="{'tr-table--border':border}" @change="change">
  4. <label>
  5. <checkbox value="check" :checked="value"/>
  6. </label>
  7. </checkbox-group>
  8. <slot></slot>
  9. </view>
  10. </template>
  11. <script>
  12. /**
  13. * Tr 表格行组件
  14. * @description 表格行组件 仅包含 th,td 组件
  15. * @tutorial https://ext.dcloud.net.cn/plugin?id=
  16. */
  17. export default {
  18. name: 'uniTr',
  19. options: {
  20. virtualHost: true
  21. },
  22. data() {
  23. return {
  24. value: false,
  25. border: false,
  26. selection:false,
  27. widthThArr:[]
  28. };
  29. },
  30. created() {
  31. this.root = this.getTable()
  32. this.border = this.root.border
  33. this.selection = this.root.type
  34. this.root.trChildren.push(this)
  35. this.root.isNodata()
  36. },
  37. mounted() {
  38. if(this.widthThArr.length > 0){
  39. const selectionWidth = this.selection === 'selection'? 50:0
  40. this.root.minWidth = this.widthThArr.reduce((a,b)=> Number(a) + Number(b)) + selectionWidth
  41. }
  42. },
  43. destroyed() {
  44. const index = this.root.trChildren.findIndex(i=>i===this)
  45. this.root.trChildren.splice(index,1)
  46. this.root.isNodata()
  47. },
  48. methods: {
  49. minWidthUpdate(width){
  50. this.widthThArr.push(width)
  51. },
  52. change(e) {
  53. this.root.trChildren.forEach((item) => {
  54. if (item === this) {
  55. this.root.check(this,e.detail.value.length > 0 ? true : false)
  56. }
  57. })
  58. },
  59. /**
  60. * 获取父元素实例
  61. */
  62. getTable() {
  63. let parent = this.$parent;
  64. let parentName = parent.$options.name;
  65. while (parentName !== 'uniTable') {
  66. parent = parent.$parent;
  67. if (!parent) return false;
  68. parentName = parent.$options.name;
  69. }
  70. return parent;
  71. },
  72. }
  73. }
  74. </script>
  75. <style lang="scss">
  76. .uni-table-tr {
  77. display: table-row;
  78. transition: all .3s;
  79. box-sizing: border-box;
  80. }
  81. .checkbox {
  82. padding: 12px 8px;
  83. width: 26px;
  84. padding-left: 12px;
  85. display: table-cell;
  86. // text-align: center;
  87. vertical-align: middle;
  88. color: #333;
  89. font-weight: 500;
  90. border-bottom: 1px #ddd solid;
  91. font-size: 14px;
  92. }
  93. .tr-table--border {
  94. border-right: 1px #ddd solid;
  95. }
  96. .uni-table-tr {
  97. /deep/ .uni-table-th {
  98. &.table--border:last-child {
  99. border-right: none;
  100. }
  101. }
  102. /deep/ .uni-table-td {
  103. &.table--border:last-child {
  104. border-right: none;
  105. }
  106. }
  107. }
  108. </style>