filter-page-table.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. export default {
  102. name: 'data-table',
  103. props: {
  104. fields: { type: Array, required: true },
  105. data: { type: Array, required: true },
  106. opera: { type: Array, default: () => [] },
  107. toFormat: null,
  108. height: null,
  109. select: { type: Boolean, default: false },
  110. selected: { type: Array, default: () => [] },
  111. usePage: { type: Boolean, default: true },
  112. total: { type: Number, default: 0 },
  113. options: null,
  114. useSum: { type: Boolean, default: false },
  115. filter: { type: Array, default: () => [] },
  116. },
  117. components: {},
  118. data: () => ({
  119. pageSelected: [],
  120. currentPage: 1,
  121. limit: _.get(this, `$limit`, undefined) !== undefined ? this.$limit : process.env.VUE_APP_LIMIT * 1,
  122. searchInfo: {},
  123. useFilter: true,
  124. filterList: [],
  125. }),
  126. created() {},
  127. computed: {},
  128. methods: {
  129. toFormatter(row, column, cellValue, index) {
  130. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  131. if (this_fields.length > 0) {
  132. let format = _.get(this_fields[0], `format`, false);
  133. if (format) {
  134. let res;
  135. if (_.isFunction(format)) {
  136. res = format(cellValue);
  137. } else {
  138. res = this.toFormat({
  139. model: this_fields[0].prop,
  140. value: cellValue,
  141. });
  142. }
  143. return res;
  144. } else return cellValue;
  145. }
  146. },
  147. handleOpera(data, method, confirm = false, methodZh, label, index) {
  148. let self = true;
  149. if (_.isFunction(methodZh)) {
  150. methodZh = methodZh(data);
  151. } else if (!_.isString(methodZh)) {
  152. methodZh = label;
  153. self = false;
  154. }
  155. if (confirm) {
  156. this.$confirm(self ? methodZh : `您确认${methodZh}该数据?`, '提示', {
  157. confirmButtonText: '确定',
  158. cancelButtonText: '取消',
  159. type: 'warning',
  160. })
  161. .then(() => {
  162. this.$emit(method, { data, index });
  163. })
  164. .catch(() => {});
  165. } else {
  166. this.$emit(method, { data, index });
  167. }
  168. },
  169. handleSelectionChange(selection, row) {
  170. // console.log(selection);
  171. // console.log(row);
  172. //根据row是否再pageSelected中,判断是添加还是删除
  173. let res = [];
  174. if (this.pageSelected.find(i => i.id === row.id)) {
  175. res = this.pageSelected.filter(f => f.id !== row.id);
  176. } else {
  177. this.pageSelected.push(row);
  178. res = this.pageSelected;
  179. }
  180. this.$set(this, `pageSelected`, res);
  181. this.$emit(`handleSelect`, _.uniqBy(res, 'id'));
  182. },
  183. handleSelectAll(selection) {
  184. //处于没全选状态,选择之后一定是全选,只有处于全选状态时,才会反选(全取消)
  185. // console.log(selection);
  186. let res = [];
  187. if (selection.length > 0) {
  188. //全选
  189. res = _.uniqBy(this.pageSelected.concat(selection), 'id');
  190. } else {
  191. //全取消
  192. res = _.differenceBy(this.pageSelected, this.data, 'id');
  193. }
  194. this.$set(this, `pageSelected`, res);
  195. this.$emit(`handleSelect`, res);
  196. },
  197. initSelection() {
  198. this.$nextTick(() => {
  199. this.$refs.table.clearSelection();
  200. this.selected.forEach(info => {
  201. let d = this.data.filter(p => p.id === info.id);
  202. if (d.length > 0) this.$refs.table.toggleRowSelection(d[0]);
  203. });
  204. });
  205. },
  206. selectReset() {
  207. this.$refs.table.clearSelection();
  208. },
  209. display(item, row) {
  210. let display = _.get(item, `display`, true);
  211. if (display === true) return true;
  212. else {
  213. let res = display(row);
  214. return res;
  215. }
  216. },
  217. //
  218. changePage(page) {
  219. this.$emit('query', { skip: (page - 1) * this.limit, limit: this.limit, ...this.searchInfo });
  220. },
  221. sizeChange(limit) {
  222. this.limit = limit;
  223. this.currentPage = 1;
  224. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  225. },
  226. getFilterList() {
  227. let res = this.fields.filter(f => _.get(f, 'filter', false));
  228. this.$set(this, `useFilter`, res.length > 0);
  229. res.map(i => {
  230. if (i.filter === 'date' && this.searchInfo[i.porp] === undefined) this.$set(this.searchInfo, i.prop, []);
  231. });
  232. res = [...res, ...this.filter];
  233. this.$set(this, `filterList`, res);
  234. },
  235. filterSearch() {
  236. this.currentPage = 1;
  237. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  238. },
  239. rowClick(row, column, event) {
  240. this.$emit(`rowClick`, row);
  241. },
  242. toClear(prop) {
  243. delete this.searchInfo[prop];
  244. },
  245. },
  246. watch: {
  247. selected: {
  248. handler(val) {
  249. if (val.length > 0) {
  250. this.pageSelected = val;
  251. this.initSelection();
  252. }
  253. },
  254. immediate: true,
  255. },
  256. data: {
  257. handler(val, oval) {
  258. if (this.select) {
  259. this.initSelection();
  260. }
  261. },
  262. },
  263. fields: {
  264. handler(val, oval) {
  265. if (val) this.getFilterList();
  266. },
  267. immediate: true,
  268. },
  269. },
  270. };
  271. </script>
  272. <style lang="less" scoped></style>