filter-page-table.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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-link
  92. :key="index"
  93. :type="item.type || 'primary'"
  94. :icon="item.icon || ''"
  95. size="mini"
  96. :underline="false"
  97. @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)"
  98. >
  99. </el-link>
  100. </el-tooltip>
  101. <el-link
  102. v-else
  103. :key="index"
  104. :type="item.type || 'primary'"
  105. :icon="item.icon || ''"
  106. size="mini"
  107. :underline="false"
  108. @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)"
  109. >
  110. {{ item.label }}
  111. </el-link>
  112. <!-- <el-button v-else :key="index" type="text" size="mini" @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label, $index)">
  113. {{ item.label }}
  114. </el-button> -->
  115. </template>
  116. </template>
  117. </template>
  118. </el-table-column>
  119. </template>
  120. </el-table>
  121. <el-row type="flex" align="middle" justify="end" style="padding-top:1rem" v-if="usePage">
  122. <el-col :span="24" style="text-align:right;">
  123. <el-pagination
  124. background
  125. layout="total, prev, pager, next"
  126. :page-sizes="[10, 15, 20, 50, 100]"
  127. :total="total"
  128. :page-size="limit"
  129. :current-page.sync="currentPage"
  130. @current-change="changePage"
  131. @size-change="sizeChange"
  132. >
  133. </el-pagination>
  134. <!-- sizes -->
  135. </el-col>
  136. </el-row>
  137. </div>
  138. </template>
  139. <script>
  140. import _ from 'lodash';
  141. export default {
  142. name: 'data-table',
  143. props: {
  144. fields: { type: Array, required: true },
  145. data: { type: Array, required: true },
  146. opera: { type: Array, default: () => [] },
  147. toFormat: null,
  148. height: null,
  149. select: { type: Boolean, default: false },
  150. selected: { type: Array, default: () => [] },
  151. usePage: { type: Boolean, default: true },
  152. total: { type: Number, default: 0 },
  153. options: null,
  154. useSum: { type: Boolean, default: false },
  155. filter: { type: Array, default: () => [] },
  156. operaWidth: { type: Number, default: 200 },
  157. },
  158. components: {},
  159. data: () => ({
  160. pageSelected: [],
  161. currentPage: 1,
  162. limit: _.get(this, `$limit`, undefined) !== undefined ? this.$limit : process.env.VUE_APP_LIMIT * 1 || 10,
  163. searchInfo: {},
  164. useFilter: true,
  165. filterList: [],
  166. }),
  167. created() {},
  168. computed: {},
  169. methods: {
  170. toFormatter(row, column, cellValue, index) {
  171. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  172. if (this_fields.length > 0) {
  173. let format = _.get(this_fields[0], `format`, false);
  174. if (format) {
  175. let res;
  176. if (_.isFunction(format)) {
  177. res = format(cellValue);
  178. } else {
  179. res = this.toFormat({
  180. model: this_fields[0].prop,
  181. value: cellValue,
  182. });
  183. }
  184. return res;
  185. } else return cellValue;
  186. }
  187. },
  188. handleOpera(data, method, confirm = false, methodZh, label, index) {
  189. let self = true;
  190. if (_.isFunction(methodZh)) {
  191. methodZh = methodZh(data);
  192. } else if (!_.isString(methodZh)) {
  193. methodZh = label;
  194. self = false;
  195. }
  196. if (confirm) {
  197. this.$confirm(self ? methodZh : `您确认${methodZh}该数据?`, '提示', {
  198. confirmButtonText: '确定',
  199. cancelButtonText: '取消',
  200. type: 'warning',
  201. })
  202. .then(() => {
  203. this.$emit(method, { data, index });
  204. })
  205. .catch(() => {});
  206. } else {
  207. this.$emit(method, { data, index });
  208. }
  209. },
  210. handleSelectionChange(selection, row) {
  211. // console.log(selection);
  212. // console.log(row);
  213. //根据row是否再pageSelected中,判断是添加还是删除
  214. let res = [];
  215. if (this.pageSelected.find(i => i.id === row.id)) {
  216. res = this.pageSelected.filter(f => f.id !== row.id);
  217. } else {
  218. this.pageSelected.push(row);
  219. res = this.pageSelected;
  220. }
  221. this.$set(this, `pageSelected`, res);
  222. this.$emit(`handleSelect`, _.uniqBy(res, 'id'));
  223. },
  224. handleSelectAll(selection) {
  225. //处于没全选状态,选择之后一定是全选,只有处于全选状态时,才会反选(全取消)
  226. // console.log(selection);
  227. let res = [];
  228. if (selection.length > 0) {
  229. //全选
  230. res = _.uniqBy(this.pageSelected.concat(selection), 'id');
  231. } else {
  232. //全取消
  233. res = _.differenceBy(this.pageSelected, this.data, 'id');
  234. }
  235. this.$set(this, `pageSelected`, res);
  236. this.$emit(`handleSelect`, res);
  237. },
  238. initSelection() {
  239. this.$nextTick(() => {
  240. this.$refs.table.clearSelection();
  241. this.selected.forEach(info => {
  242. let d = this.data.filter(p => p.id === info.id);
  243. if (d.length > 0) this.$refs.table.toggleRowSelection(d[0]);
  244. });
  245. });
  246. },
  247. selectReset() {
  248. this.$refs.table.clearSelection();
  249. },
  250. display(item, row) {
  251. let display = _.get(item, `display`, true);
  252. if (display === true) return true;
  253. else {
  254. let res = display(row);
  255. return res;
  256. }
  257. },
  258. //
  259. changePage(page = this.currentPage) {
  260. this.$emit('query', { skip: (page - 1) * this.limit, limit: this.limit, ...this.searchInfo });
  261. },
  262. sizeChange(limit) {
  263. this.limit = limit;
  264. this.currentPage = 1;
  265. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  266. },
  267. getFilterList() {
  268. let res = this.fields.filter(f => _.get(f, 'filter', false));
  269. this.$set(this, `useFilter`, res.length > 0);
  270. res.map(i => {
  271. if (i.filter === 'date' && this.searchInfo[i.porp] === undefined) this.$set(this.searchInfo, i.prop, []);
  272. });
  273. res = [...res, ...this.filter];
  274. this.$set(this, `filterList`, res);
  275. },
  276. filterSearch() {
  277. this.currentPage = 1;
  278. this.$emit('query', { skip: 0, limit: this.limit, ...this.searchInfo });
  279. },
  280. rowClick(row, column, event) {
  281. this.$emit(`rowClick`, row);
  282. },
  283. toClear(prop) {
  284. delete this.searchInfo[prop];
  285. },
  286. filterReturn(data, item) {
  287. let { prop, filterReturn } = item;
  288. if (filterReturn) this.$emit('filterReturn', { data, prop });
  289. },
  290. },
  291. watch: {
  292. selected: {
  293. handler(val) {
  294. if (val.length > 0) {
  295. this.pageSelected = val;
  296. this.initSelection();
  297. }
  298. },
  299. immediate: true,
  300. },
  301. data: {
  302. handler(val, oval) {
  303. if (this.select) {
  304. this.initSelection();
  305. }
  306. },
  307. },
  308. fields: {
  309. handler(val, oval) {
  310. if (val) this.getFilterList();
  311. },
  312. immediate: true,
  313. },
  314. },
  315. };
  316. </script>
  317. <style lang="less" scoped></style>