data-table.vue 5.8 KB

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