data-table.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <!-- TODO 多选回显显示有问题.没显示对号 row-key="id" -->
  3. <div id="data-table">
  4. <el-table
  5. ref="table"
  6. :row-key="getRowKey"
  7. :data="data"
  8. border
  9. stripe
  10. size="small"
  11. :max-height="height !== null ? height : ''"
  12. @selection-change="handleSelectionChange"
  13. >
  14. <el-table-column type="selection" width="55" v-if="select" prop="id" :reserve-selection="true"> </el-table-column>
  15. <template v-for="(item, index) in fields">
  16. <el-table-column :key="index" align="center" :label="item.label" :prop="item.prop" :formatter="toFormatter"></el-table-column>
  17. </template>
  18. <template v-if="opera.length > 0">
  19. <el-table-column label="操作" align="center">
  20. <template v-slot="{ row, $index }">
  21. <template v-for="(item, index) in opera">
  22. <template v-if="display(item, row)">
  23. <el-tooltip :key="index" effect="dark" :content="item.label" placement="bottom">
  24. <el-button
  25. :key="index"
  26. type="text"
  27. :icon="item.icon || ''"
  28. size="mini"
  29. @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)"
  30. ></el-button>
  31. </el-tooltip>
  32. </template>
  33. </template>
  34. </template>
  35. </el-table-column>
  36. </template>
  37. </el-table>
  38. </div>
  39. </template>
  40. <script>
  41. import _ from 'lodash';
  42. export default {
  43. name: 'data-table',
  44. props: {
  45. fields: { type: Array, required: true },
  46. data: { type: Array, required: true },
  47. opera: { type: Array, default: () => [] },
  48. toFormat: null,
  49. height: null,
  50. select: { type: Boolean, default: false },
  51. selected: { type: Array, default: () => [] },
  52. },
  53. components: {},
  54. data: () => ({
  55. pageSelected: [],
  56. }),
  57. created() {},
  58. computed: {},
  59. methods: {
  60. toFormatter(row, column, cellValue, index) {
  61. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  62. if (this_fields.length > 0) {
  63. let format = _.get(this_fields[0], `format`, false);
  64. if (format) {
  65. let res;
  66. if (_.isFunction(format)) {
  67. res = format(cellValue);
  68. } else {
  69. res = this.toFormat({
  70. model: this_fields[0].prop,
  71. value: cellValue,
  72. });
  73. }
  74. return res;
  75. } else return cellValue;
  76. }
  77. },
  78. handleOpera(data, method, confirm = false, methodZh, label, index) {
  79. if (!methodZh) {
  80. methodZh = label;
  81. }
  82. if (confirm) {
  83. this.$confirm(`您确认${methodZh}该数据?`, '提示', {
  84. confirmButtonText: '确定',
  85. cancelButtonText: '取消',
  86. type: 'warning',
  87. })
  88. .then(() => {
  89. this.$emit(method, { data, index });
  90. })
  91. .catch(() => {});
  92. } else {
  93. this.$emit(method, { data, index });
  94. }
  95. },
  96. handleSelectionChange(rows) {
  97. console.log(rows);
  98. this.pageSelected = rows;
  99. this.$emit(`handleSelect`, rows);
  100. },
  101. getRowKey(row) {
  102. return row.id;
  103. },
  104. setSelectTable() {
  105. this.selected.forEach(val => {
  106. this.$refs.table && this.$refs.table.toggleRowSelection(val, true);
  107. });
  108. },
  109. selectReset() {
  110. this.$refs.table.clearSelection();
  111. },
  112. display(item, row) {
  113. let display = _.get(item, `display`, true);
  114. if (display === true) return true;
  115. else {
  116. let res = display(row);
  117. return res;
  118. }
  119. },
  120. },
  121. watch: {
  122. selected: {
  123. handler(val) {
  124. if (val.length > 0) {
  125. this.setSelectTable(val);
  126. }
  127. },
  128. immediate: true,
  129. },
  130. },
  131. };
  132. </script>
  133. <style lang="less" scoped></style>