BaseTable.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. {{
  44. typeof column.tagName === 'function'
  45. ? column.tagName(scope.row)
  46. : scope.row[column.key]
  47. }}
  48. </el-tag>
  49. </span>
  50. <span v-else-if="column.type === 'switch'">
  51. <el-switch
  52. :value="scope.row[column.key]"
  53. :active-text="column.switchName[0]"
  54. :inactive-text="column.switchName[1]"
  55. @change="column.api(scope.row)"
  56. >
  57. </el-switch>
  58. </span>
  59. <span v-else>{{ scope.row[column.key] || '-' }}</span>
  60. </template>
  61. </el-table-column>
  62. </template>
  63. </slot>
  64. </el-table>
  65. <div class="page-select">
  66. <div class="btn-box">
  67. <slot :selected="multipleSelection" name="btn-box" />
  68. </div>
  69. <el-pagination
  70. v-if="showPage"
  71. background
  72. layout="prev, pager, next"
  73. :current-page="pagination.page"
  74. :page-size="pagination.pageSize"
  75. :total="pagination.total"
  76. @current-change="num => pageChange(num)"
  77. />
  78. </div>
  79. </div>
  80. </template>
  81. <script>
  82. // 自定义内容的组件
  83. var exSlot = {
  84. functional: true,
  85. props: {
  86. row: Object,
  87. render: Function,
  88. index: Number,
  89. column: {
  90. type: Object,
  91. default: null
  92. }
  93. },
  94. render: (h, data) => {
  95. const params = {
  96. row: data.props.row,
  97. index: data.props.index
  98. };
  99. if (data.props.column) params.column = data.props.column;
  100. return data.props.render(h, params);
  101. }
  102. };
  103. export default {
  104. name: 'TableWithPage',
  105. components: {
  106. 'ex-slot': exSlot
  107. },
  108. props: {
  109. columns: {
  110. type: Array,
  111. default: () => []
  112. },
  113. items: {
  114. type: Array,
  115. default: () => []
  116. },
  117. pagination: {
  118. type: Object,
  119. default: () => ({})
  120. },
  121. pageChange: {
  122. type: Function,
  123. default: () => {}
  124. },
  125. minHeight: {
  126. type: String,
  127. default: '600px'
  128. },
  129. showPage: {
  130. type: Boolean,
  131. default: true
  132. }
  133. },
  134. data() {
  135. return {
  136. multipleSelection: []
  137. };
  138. },
  139. watch: {
  140. items: {
  141. handler(val) {
  142. this.multipleSelection = [];
  143. },
  144. immedaite: true
  145. }
  146. },
  147. methods: {
  148. handleSelectionChange(params) {
  149. this.multipleSelection = params;
  150. }
  151. }
  152. };
  153. </script>
  154. <style scoped>
  155. .page-select {
  156. display: flex;
  157. justify-content: space-between;
  158. align-items: center;
  159. padding: 6px 20px 0 20px;
  160. }
  161. .btn-box .el-button {
  162. margin: 2px 10px;
  163. }
  164. </style>