data-table.vue 5.2 KB

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