data-table.vue 5.0 KB

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