data-table.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <div id="data-table">
  3. <el-form :model="searchInfo" :inline="true" style="padding:0.9rem 1.875rem ;" size="mini" v-if="useFilter">
  4. <el-form-item v-for="(item, index) in filterList" :key="index">
  5. <template v-if="item.filter === 'select'">
  6. <el-select v-model="searchInfo[item.prop]" size="mini" clearable filterable :placeholder="`请选择${item.label}`" @clear="toClear(item.prop)">
  7. <slot name="options" v-bind="{ item }"></slot>
  8. </el-select>
  9. </template>
  10. <template v-else-if="item.filter === 'date'">
  11. <el-date-picker
  12. v-model="searchInfo[item.prop]"
  13. value-format="yyyy-MM-dd"
  14. format="yyyy-MM-dd"
  15. type="daterange"
  16. range-separator="-"
  17. start-placeholder="开始日期"
  18. end-placeholder="结束日期"
  19. clearable
  20. >
  21. </el-date-picker>
  22. </template>
  23. <template v-else>
  24. <el-input v-model="searchInfo[item.prop]" clearable size="mini" :placeholder="`请输入${item.label}`" @clear="toClear(item.prop)"></el-input>
  25. </template>
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button type="primary" size="mini" @click="filterSearch">查询</el-button>
  29. </el-form-item>
  30. </el-form>
  31. <el-table
  32. ref="table"
  33. row-key="id"
  34. :data="data"
  35. border
  36. stripe
  37. size="mini"
  38. :max-height="height !== null ? height : ''"
  39. @select="handleSelectionChange"
  40. @select-all="handleSelectAll"
  41. v-bind="options"
  42. :show-summary="useSum"
  43. @row-click="rowClick"
  44. >
  45. <el-table-column type="selection" width="55" v-if="select" prop="id" :reserve-selection="true"> </el-table-column>
  46. <template v-for="(item, index) in fields">
  47. <template v-if="item.custom">
  48. <el-table-column :key="index" align="center" :label="item.label" v-bind="item.options">
  49. <template v-slot="{ row }">
  50. <slot name="custom" v-bind="{ item, row }"></slot>
  51. </template>
  52. </el-table-column>
  53. </template>
  54. <template v-else>
  55. <el-table-column :key="index" align="center" :label="item.label" :prop="item.prop" :formatter="toFormatter" sortable v-bind="item.options">
  56. </el-table-column>
  57. </template>
  58. </template>
  59. <template v-if="opera.length > 0">
  60. <el-table-column label="操作" align="center">
  61. <template v-slot="{ row, $index }">
  62. <template v-for="(item, index) in opera">
  63. <template v-if="display(item, row)">
  64. <el-tooltip v-if="item.icon" :key="index" effect="dark" :content="item.label" placement="bottom">
  65. <el-button
  66. :key="index"
  67. type="text"
  68. :icon="item.icon || ''"
  69. size="mini"
  70. @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)"
  71. ></el-button>
  72. </el-tooltip>
  73. <el-button v-else :key="index" type="text" size="mini" @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)">
  74. {{ item.label }}
  75. </el-button>
  76. </template>
  77. </template>
  78. </template>
  79. </el-table-column>
  80. </template>
  81. </el-table>
  82. <el-row type="flex" align="middle" justify="end" style="padding-top:1rem" v-if="usePage">
  83. <el-col :span="24" style="text-align:right;">
  84. <el-pagination
  85. background
  86. layout="sizes, total, prev, pager, next"
  87. :page-sizes="[10, 15, 20, 50, 100]"
  88. :total="total"
  89. :page-size="limit"
  90. :current-page.sync="currentPage"
  91. @current-change="changePage"
  92. @size-change="sizeChange"
  93. >
  94. </el-pagination>
  95. </el-col>
  96. </el-row>
  97. </div>
  98. </template>
  99. <script>
  100. import _ from 'lodash';
  101. import dataForm from '@/components/form.vue';
  102. export default {
  103. name: 'data-table',
  104. props: {
  105. fields: { type: Array, required: true },
  106. data: { type: Array, required: true },
  107. opera: { type: Array, default: () => [] },
  108. toFormat: null,
  109. height: null,
  110. select: { type: Boolean, default: false },
  111. selected: { type: Array, default: () => [] },
  112. usePage: { type: Boolean, default: true },
  113. total: { type: Number, default: 0 },
  114. options: null,
  115. useSum: { type: Boolean, default: false },
  116. filter: { type: Array, default: () => [] },
  117. },
  118. components: {},
  119. data: () => ({
  120. pageSelected: [],
  121. currentPage: 1,
  122. limit: _.get(this, `$limit`, undefined) !== undefined ? this.$limit : process.env.VUE_APP_LIMIT * 1,
  123. searchInfo: {},
  124. useFilter: true,
  125. filterList: [],
  126. }),
  127. created() {},
  128. computed: {},
  129. methods: {
  130. toFormatter(row, column, cellValue, index) {
  131. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  132. if (this_fields.length > 0) {
  133. let format = _.get(this_fields[0], `format`, false);
  134. if (format) {
  135. let res;
  136. if (_.isFunction(format)) {
  137. res = format(cellValue);
  138. } else {
  139. res = this.toFormat({
  140. model: this_fields[0].prop,
  141. value: cellValue,
  142. });
  143. }
  144. return res;
  145. } else return cellValue;
  146. }
  147. },
  148. handleOpera(data, method, confirm = false, methodZh, label, index) {
  149. let self = true;
  150. if (_.isFunction(methodZh)) {
  151. methodZh = methodZh(data);
  152. } else if (!_.isString(methodZh)) {
  153. methodZh = label;
  154. self = false;
  155. }
  156. if (confirm) {
  157. this.$confirm(self ? methodZh : `您确认${methodZh}该数据?`, '提示', {
  158. confirmButtonText: '确定',
  159. cancelButtonText: '取消',
  160. type: 'warning',
  161. })
  162. .then(() => {
  163. this.$emit(method, { data, index });
  164. })
  165. .catch(() => {});
  166. } else {
  167. this.$emit(method, { data, index });
  168. }
  169. },
  170. handleSelectionChange(selection, row) {
  171. // console.log(selection);
  172. // console.log(row);
  173. //根据row是否再pageSelected中,判断是添加还是删除
  174. let res = [];
  175. if (this.pageSelected.find(i => i.id === row.id)) {
  176. res = this.pageSelected.filter(f => f.id !== row.id);
  177. } else {
  178. this.pageSelected.push(row);
  179. res = this.pageSelected;
  180. }
  181. this.$set(this, `pageSelected`, res);
  182. this.$emit(`handleSelect`, _.uniqBy(res, 'id'));
  183. },
  184. handleSelectAll(selection) {
  185. //处于没全选状态,选择之后一定是全选,只有处于全选状态时,才会反选(全取消)
  186. // console.log(selection);
  187. let res = [];
  188. if (selection.length > 0) {
  189. //全选
  190. res = _.uniqBy(this.pageSelected.concat(selection), 'id');
  191. } else {
  192. //全取消
  193. res = _.differenceBy(this.pageSelected, this.data, 'id');
  194. }
  195. this.$set(this, `pageSelected`, res);
  196. this.$emit(`handleSelect`, res);
  197. },
  198. initSelection() {
  199. this.$nextTick(() => {
  200. this.$refs.table.clearSelection();
  201. this.selected.forEach(info => {
  202. let d = this.data.filter(p => p.id === info.id);
  203. if (d.length > 0) this.$refs.table.toggleRowSelection(d[0]);
  204. });
  205. });
  206. },
  207. selectReset() {
  208. this.$refs.table.clearSelection();
  209. },
  210. display(item, row) {
  211. let display = _.get(item, `display`, true);
  212. if (display === true) return true;
  213. else {
  214. let res = display(row);
  215. return res;
  216. }
  217. },
  218. //
  219. changePage(page) {
  220. this.$emit('query', { skip: (page - 1) * this.limit, limit: this.limit, ...this.searchInfo });
  221. },
  222. sizeChange(limit) {
  223. this.limit = limit;
  224. this.currentPage = 1;
  225. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  226. },
  227. getFilterList() {
  228. let res = this.fields.filter(f => _.get(f, 'filter', false));
  229. this.$set(this, `useFilter`, res.length > 0);
  230. res.map(i => {
  231. if (i.filter === 'date' && this.searchInfo[i.porp] === undefined) this.$set(this.searchInfo, i.prop, []);
  232. });
  233. res = [...res, ...this.filter];
  234. this.$set(this, `filterList`, res);
  235. },
  236. filterSearch() {
  237. this.currentPage = 1;
  238. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  239. },
  240. rowClick(row, column, event) {
  241. this.$emit(`rowClick`, row);
  242. },
  243. toClear(prop) {
  244. delete this.searchInfo[prop];
  245. },
  246. },
  247. watch: {
  248. selected: {
  249. handler(val) {
  250. if (val.length > 0) {
  251. this.pageSelected = val;
  252. this.initSelection();
  253. }
  254. },
  255. immediate: true,
  256. },
  257. data: {
  258. handler(val, oval) {
  259. if (this.select) {
  260. this.initSelection();
  261. }
  262. },
  263. },
  264. fields: {
  265. handler(val, oval) {
  266. if (val) this.getFilterList();
  267. },
  268. immediate: true,
  269. },
  270. },
  271. };
  272. </script>
  273. <style lang="less" scoped></style>