index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="table-layout">
  3. <!-- 表头 -->
  4. <ul class="t-header" :class="{'bottom-line':showLine}">
  5. <!-- 复选框 -->
  6. <li v-if="selection" class="check-cls"><span @click="lock=false,selectAll=!selectAll" class="fdcheck" :class="{check_active:selectAll}"></span></li>
  7. <!-- -->
  8. <li v-for="(item,i) in header" :key="i" :style="{textAlign: item.textAlign, width:item.width ?item.width+'px':(100/header.length)+'%'}">
  9. <slot :data='item' name='header'></slot>
  10. </li>
  11. </ul>
  12. <!-- 表格内容区域 -->
  13. <div class="t-con"
  14. v-infinite-scroll="requestMoreData"
  15. :infinite-scroll-disabled="!canRequestMoreData"
  16. ref="t-con"
  17. :style="`margin-right: ${hasScrollBar ? -6 : 0}px`"
  18. >
  19. <ul class="t-item" active-txt :class="{'bottom-line': showLine}" v-for="(item, i) in fixdata" :key="i">
  20. <!-- 复选框 -->
  21. <li v-if="selection" class="check-cls" ><span @click="selectItem(item,i)" class="fdcheck" :class="{check_active:item.hasAuth}"></span></li>
  22. <!-- -->
  23. <li
  24. v-for="(sub,j) in header"
  25. :key='j'
  26. :style="{textAlign:item.textAlign, width:sub.width ? sub.width+'px':(100/header.length)+'%'}"
  27. :class="{ themetxt: sub.fontweight, showWhenHover: sub.showWhenHover }"
  28. :title="sub.key === 'name'? item[sub.key]: ''"
  29. >
  30. <slot :data='item[sub.key]' :item="item" :sub='sub' name='item'></slot>
  31. </li>
  32. </ul>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. import { mapState } from 'vuex'
  38. export default {
  39. props: {
  40. canRequestMoreData: {
  41. type: Boolean,
  42. default: true
  43. },
  44. data: {
  45. type: Array,
  46. default: Array
  47. },
  48. /**
  49. * 表头数据:各列名称和配置
  50. * fontweight: Boolean,某一列是否是关键列,会加粗显示
  51. * showWhenHover: Boolean,是否只在hover到这一行时才显示这一列在该行的数据
  52. * canclick: Booean, 是否可点击进行各种操作
  53. * width: Number,该列宽度
  54. * textAlign: css属性值
  55. */
  56. header: {
  57. type: Array,
  58. default: Array
  59. },
  60. // 是否显示复选框
  61. selection: {
  62. type: Boolean,
  63. defaut: false
  64. },
  65. // 是否显示每行之间的分隔线
  66. showLine: {
  67. type: Boolean,
  68. default: false
  69. }
  70. },
  71. data () {
  72. return {
  73. selectAll: false,
  74. lock: true,
  75. hasScrollBar: false,
  76. }
  77. },
  78. computed: {
  79. ...mapState({
  80. // props中data中hasAuth一律写成false,即未选中。
  81. fixdata () {
  82. let data = this.data && this.data.map(item => {
  83. item.hasAuth = false
  84. return item
  85. })
  86. return data
  87. }
  88. })
  89. },
  90. watch: {
  91. data () {
  92. this.selectAll = false
  93. },
  94. selectAll: function (newVal) {
  95. if (!this.lock) {
  96. this.fixdata.forEach(item => {
  97. item.hasAuth = newVal
  98. })
  99. this.handleSelect()
  100. }
  101. }
  102. },
  103. methods: {
  104. requestMoreData() {
  105. this.$emit('request-more-data')
  106. },
  107. handleSelect () {
  108. let arr = this.fixdata.filter(item => {
  109. return item.hasAuth
  110. })
  111. this.$emit('selection-change', arr)
  112. },
  113. selectItem (item, i) {
  114. item.hasAuth = !item.hasAuth
  115. this.$set(this.fixdata, i, item)
  116. this.lock = true
  117. this.selectAll = true
  118. this.fixdata.forEach(sub => {
  119. if (!sub.hasAuth) {
  120. this.selectAll = false
  121. }
  122. })
  123. this.handleSelect()
  124. }
  125. },
  126. mounted () {
  127. const resizeObserver = new ResizeObserver((entries) => {
  128. console.log('asdf');
  129. if (entries[0].target.clientHeight < entries[0].target.scrollHeight) {
  130. this.hasScrollBar = true
  131. } else {
  132. this.hasScrollBar = false
  133. }
  134. })
  135. resizeObserver.observe(this.$refs['t-con'])
  136. },
  137. }
  138. </script>
  139. <style lang="less" scoped>
  140. @import './style.less';
  141. </style>