data-table.vue 5.4 KB

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