data-table.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. let self = true;
  72. if (_.isFunction(methodZh)) {
  73. methodZh = methodZh(data);
  74. } else if (!_.isString(methodZh)) {
  75. methodZh = label;
  76. self = false;
  77. }
  78. if (confirm) {
  79. this.$confirm(self ? methodZh : `您确认${methodZh}该数据?`, '提示', {
  80. confirmButtonText: '确定',
  81. cancelButtonText: '取消',
  82. type: 'warning',
  83. })
  84. .then(() => {
  85. this.$emit(method, { data, index });
  86. })
  87. .catch(() => {});
  88. } else {
  89. this.$emit(method, { data, index });
  90. }
  91. },
  92. handleSelectionChange(selection, row) {
  93. //根据row是否再pageSelected中,判断是添加还是删除
  94. let res = [];
  95. if (this.pageSelected.find(i => i.id === row.id)) res = this.pageSelected.filter(f => f.id !== row.id);
  96. else {
  97. this.pageSelected.push(row);
  98. res = this.pageSelected;
  99. }
  100. this.$emit(`handleSelect`, _.uniqBy(res, 'id'));
  101. },
  102. initSelection() {
  103. this.$nextTick(() => {
  104. this.$refs.table.clearSelection();
  105. this.selected.forEach(info => {
  106. let d = this.data.filter(p => p.id === info.id);
  107. if (d.length > 0) this.$refs.table.toggleRowSelection(d[0]);
  108. });
  109. });
  110. },
  111. selectReset() {
  112. this.$refs.table.clearSelection();
  113. },
  114. display(item, row) {
  115. let display = _.get(item, `display`, true);
  116. if (display === true) return true;
  117. else {
  118. let res = display(row);
  119. return res;
  120. }
  121. },
  122. },
  123. watch: {
  124. selected: {
  125. handler(val) {
  126. if (val.length > 0) {
  127. this.pageSelected = val;
  128. this.initSelection();
  129. }
  130. },
  131. immediate: true,
  132. },
  133. data: {
  134. handler(val, oval) {
  135. if (this.select) {
  136. this.initSelection();
  137. }
  138. },
  139. },
  140. },
  141. };
  142. </script>
  143. <style lang="less" scoped></style>