TableView.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <script>
  2. /*
  3. * @Author: yolo
  4. * @Date: 2020-05-13 10:36:04
  5. * @Last Modified by: Yolo
  6. * @Last Modified time: 2020-12-24 15:18:03
  7. * 应用于表格视图
  8. */
  9. export default {
  10. name: 'TableView',
  11. data () {
  12. return {
  13. tableViewBodyDom: null,
  14. interval: null,
  15. bodyHeight: 100,
  16. isShowHeader: false
  17. }
  18. },
  19. computed: {
  20. isShow () {
  21. return this.$slots.header
  22. }
  23. },
  24. methods: {
  25. setHeight () {
  26. if (this.tableViewBodyDom) {
  27. setTimeout(() => {
  28. const height =
  29. document.body.clientHeight -
  30. (this.$refs.header ? this.$refs.header.offsetHeight : 0) -
  31. this.getTopHeight() -
  32. (this.$refs.footer ? this.$refs.footer.offsetHeight : 0)
  33. height && this.bodyHeight !== height && this.bodyHeightChange(height)
  34. }, 300)
  35. }
  36. },
  37. /**
  38. 兼容页面多个头不能动态计算的问题
  39. */
  40. getTopHeight () {
  41. let height =
  42. (document.getElementsByClassName('navBar').length > 0
  43. ? document.getElementsByClassName('navBar')[0].offsetHeight
  44. : 0) +
  45. (document.getElementsByClassName('title-line-height').length > 0
  46. ? document.getElementsByClassName('title-line-height')[0].offsetHeight
  47. : 0) + 40
  48. return height
  49. },
  50. bodyHeightChange (newHeight) {
  51. this.bodyHeight = newHeight
  52. this.$emit('body-height-change', newHeight)
  53. },
  54. /**
  55. * 安装 table body height 监视器
  56. */
  57. installBodyHeightMonitor () {
  58. // console.info(this.$el)
  59. this.bodyHeight = 90
  60. this.beforeDestroyed()
  61. this.interval = setTimeout(() => {
  62. this.setHeight()
  63. this.installBodyHeightMonitor()
  64. }, 800)
  65. },
  66. /**
  67. * 安装 table body height 监视器
  68. */
  69. uninstallBodyHeightMonitor () {
  70. clearTimeout(this.interval)
  71. },
  72. beforeDestroyed () {
  73. this.uninstallBodyHeightMonitor()
  74. }
  75. },
  76. mounted () {
  77. this.tableViewBodyDom = this.$refs.tableViewBody
  78. this.setHeight()
  79. this.installBodyHeightMonitor()
  80. },
  81. destroyed () {
  82. this.beforeDestroyed()
  83. }
  84. }
  85. </script>
  86. <template>
  87. <section ref="tableView"
  88. class="table-view">
  89. <header ref="header">
  90. <slot name="header"></slot>
  91. </header>
  92. <div class="table-view__body"
  93. ref="tableViewBody">
  94. <slot v-if="bodyHeight"
  95. name="body"></slot>
  96. <footer ref="footer">
  97. <slot name="footer"></slot>
  98. </footer>
  99. </div>
  100. <slot></slot>
  101. </section>
  102. </template>
  103. <style lang="scss" scope>
  104. .table-view {
  105. // display: flex;
  106. flex-direction: column;
  107. height: 100%;
  108. min-height: 150px;
  109. // margin-top: 1px;
  110. max-height: 100%;
  111. header {
  112. }
  113. &__body {
  114. flex: 1;
  115. overflow-y: auto;
  116. padding: 10px;
  117. background: #fff;
  118. overflow-x: hidden;
  119. }
  120. // footer {
  121. // padding-top: 8px;
  122. // }
  123. .pagination-container {
  124. margin-top: 0 !important;
  125. padding: 5px;
  126. }
  127. .el-pagination {
  128. margin-bottom: 0 !important;
  129. }
  130. .maintenance-overview {
  131. padding: 0 !important;
  132. }
  133. }
  134. </style>