BaseTable.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div class="mt-20">
  3. <el-table
  4. :data="items"
  5. :style="`width: 100%; min-height:${minHeight};`"
  6. v-bind="$attrs"
  7. @selection-change="handleSelectionChange"
  8. >
  9. <slot name="table">
  10. <template v-for="column in columns">
  11. <el-table-column
  12. v-if="column.key === 'selection'"
  13. :key="column.key"
  14. :type="column.key"
  15. :min-width="column.minWidth"
  16. :width="column.width || 50"
  17. :align="column.align || 'center'"
  18. />
  19. <el-table-column
  20. v-else
  21. :key="column.key"
  22. :prop="column.key"
  23. :label="column.name"
  24. :min-width="column.minWidth"
  25. :width="column.width"
  26. :align="column.align"
  27. :fixed="column.fixed"
  28. :show-overflow-tooltip="column['show-overflow-tooltip']"
  29. >
  30. <template slot-scope="scope">
  31. <ex-slot
  32. v-if="column.render"
  33. :render="column.render"
  34. :row="scope.row"
  35. :index="scope.$index"
  36. :column="column"
  37. />
  38. <span v-else-if="column.type === 'tag'">
  39. <el-tag
  40. size="medium"
  41. :type="column.fetchTagType(scope.row[column.key]) || ''"
  42. >{{
  43. typeof column.tagName === 'function'
  44. ? column.tagName(scope.row)
  45. : scope.row[column.key]
  46. }}</el-tag
  47. >
  48. </span>
  49. <span v-else>{{ scope.row[column.key] || '-' }}</span>
  50. </template>
  51. </el-table-column>
  52. </template>
  53. </slot>
  54. </el-table>
  55. <div class="page-select">
  56. <div class="btn-box">
  57. <slot :selected="multipleSelection" name="btn-box" />
  58. </div>
  59. <el-pagination
  60. v-if="showPage"
  61. background
  62. layout="prev, pager, next"
  63. :current-page="pagination.page"
  64. :page-size="pagination.pageSize"
  65. :total="pagination.total"
  66. @current-change="num => pageChange(num)"
  67. />
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. // 自定义内容的组件
  73. var exSlot = {
  74. functional: true,
  75. props: {
  76. row: Object,
  77. render: Function,
  78. index: Number,
  79. column: {
  80. type: Object,
  81. default: null
  82. }
  83. },
  84. render: (h, data) => {
  85. const params = {
  86. row: data.props.row,
  87. index: data.props.index
  88. };
  89. if (data.props.column) params.column = data.props.column;
  90. return data.props.render(h, params);
  91. }
  92. };
  93. export default {
  94. name: 'TableWithPage',
  95. components: {
  96. 'ex-slot': exSlot
  97. },
  98. props: {
  99. columns: {
  100. type: Array,
  101. default: () => []
  102. },
  103. items: {
  104. type: Array,
  105. default: () => []
  106. },
  107. pagination: {
  108. type: Object,
  109. default: () => ({})
  110. },
  111. pageChange: {
  112. type: Function,
  113. default: () => {}
  114. },
  115. minHeight: {
  116. type: String,
  117. default: '600px'
  118. },
  119. showPage: {
  120. type: Boolean,
  121. default: true
  122. }
  123. },
  124. data() {
  125. return {
  126. multipleSelection: []
  127. };
  128. },
  129. watch: {
  130. items: {
  131. handler(val) {
  132. this.multipleSelection = [];
  133. },
  134. immedaite: true
  135. }
  136. },
  137. methods: {
  138. handleSelectionChange(params) {
  139. this.multipleSelection = params;
  140. }
  141. }
  142. };
  143. </script>
  144. <style scoped>
  145. .page-select {
  146. display: flex;
  147. justify-content: space-between;
  148. align-items: center;
  149. padding: 6px 20px 0 20px;
  150. }
  151. .btn-box .el-button {
  152. margin: 2px 10px;
  153. }
  154. </style>