data-table.vue 6.6 KB

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