filter-page-table.vue 9.5 KB

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