data-table.vue 4.3 KB

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