use-table.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div ref="useTable"></div>
  3. </template>
  4. <script>
  5. import bus from '@/common/bus';
  6. export default {
  7. props: {
  8. navHeight: {
  9. type: Number,
  10. default: 0
  11. },
  12. bottom: {
  13. type: Number,
  14. default: 15
  15. },
  16. right: {
  17. type: Number,
  18. default: 15
  19. },
  20. top: {
  21. type: Number,
  22. default: 0
  23. }
  24. },
  25. data() {
  26. return {
  27. height: 0,
  28. tblHeight: 0,
  29. body: null,
  30. containers: [],
  31. tableObj: null,
  32. tableButtonHeight: 0,
  33. tablePaginationHeight: 0
  34. };
  35. },
  36. mounted() {
  37. this.body = document.getElementsByTagName('uni-page-body')[0];
  38. this.__height = this.body.clientHeight;
  39. this.containers = this.$refs.useTable.parentElement.querySelectorAll('.container');
  40. this.tableStyle();
  41. this.tableChange();
  42. window.onresize = () => {
  43. this.tableChange();
  44. };
  45. },
  46. methods: {
  47. tableStyle() {
  48. if (this.tableObj && this.tableObj.ele) {
  49. return;
  50. }
  51. this.containers.forEach(ele => {
  52. if (![...ele.classList].includes('use-table')) {
  53. this.__height -= ele.clientHeight - 15;
  54. } else {
  55. this.tableObj = { offset: this.$api.offset(ele), ele };
  56. }
  57. });
  58. this.tableObj.ele.style.position = 'fixed';
  59. this.tableObj.ele.style.top = this.tableObj.offset.t + 'px';
  60. this.tableObj.ele.style.left = this.tableObj.offset.l + 'px';
  61. this.tableObj.ele.style.bottom = this.bottom + 'px';
  62. this.tableObj.ele.style.right = this.right + 'px';
  63. if (this.tableObj.ele.childNodes.length) {
  64. this.tableObj.ele.childNodes.forEach(ele => {
  65. if ([...ele.classList].includes('el-pagination')) {
  66. this.tablePaginationHeight = 35;
  67. } else if ([...ele.classList].includes('el-table')) {
  68. // 当前表格
  69. } else {
  70. this.tableButtonHeight = 55;
  71. }
  72. });
  73. }
  74. },
  75. tableChange() {
  76. // 40 为上下 padding 20
  77. this.tblHeight = this.tableObj.ele.clientHeight - 40 - this.tableButtonHeight - this.tablePaginationHeight;
  78. this.tableObj.ele.querySelector('.el-table').style.height = this.tblHeight + 'px';
  79. }
  80. },
  81. created() {
  82. bus.$on('fold', res => {
  83. if (res) {
  84. this.tableObj.ele.style.left = this.tableObj.offset.l - 116 + 'px';
  85. } else {
  86. this.tableObj.ele.style.left = this.tableObj.offset.l + 'px';
  87. }
  88. });
  89. }
  90. };
  91. </script>
  92. <style></style>