filter-grid.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <el-container class="container">
  3. <el-header height="auto" style="line-height:36px;" v-if="filter || action">
  4. <slot name="filter" v-if="filter">
  5. <filter-box ref="filter" :matcher="matcher" :fields="filterFields" @query="query" v-bind="filterProps">
  6. <template v-slot:ext="form" v-if="$scopedSlots['filter-ext']">
  7. <slot name="filter-ext" v-bind="form"></slot>
  8. </template>
  9. </filter-box>
  10. </slot>
  11. <slot name="action" v-if="action">
  12. <el-button icon="el-icon-plus" type="primary" size="mini" v-if="!readonly" @click="$emit('add-new')">添加</el-button>
  13. </slot>
  14. </el-header>
  15. <el-main class="table-area">
  16. <lite-grid
  17. :data="data"
  18. :meta="meta"
  19. :selection="selection"
  20. :options="options"
  21. :readonly="readonly"
  22. :operation="operation"
  23. :currentRow="currentRow"
  24. @oper="handleOper"
  25. @selection="$emit('selection', $event)"
  26. @dblclick="$emit('dblclick', $event)"
  27. @currentRow="$emit('row', $event)"
  28. >
  29. <template v-slot:pre>
  30. <slot name="pre"> </slot>
  31. </template>
  32. <slot> </slot>
  33. <template v-slot:oper>
  34. <slot name="oper"> </slot>
  35. </template>
  36. <template v-slot:oper_header>
  37. <slot name="oper_header"> </slot>
  38. </template>
  39. <template v-slot:ext>
  40. <slot name="ext"> </slot>
  41. </template>
  42. <template v-slot:post>
  43. <slot name="post"> </slot>
  44. </template>
  45. </lite-grid>
  46. </el-main>
  47. <el-footer height="36px" v-if="paging">
  48. <el-pagination
  49. background
  50. @size-change="handleSizeChange"
  51. @current-change="handleCurrentChange"
  52. :current-page="page"
  53. :page-sizes="[10, 20, 50, 100, 200]"
  54. :page-size="size"
  55. :total="total"
  56. layout="total, sizes, prev, pager, next, jumper"
  57. ></el-pagination>
  58. </el-footer>
  59. </el-container>
  60. </template>
  61. <script>
  62. import { FieldMeta } from './meta-util';
  63. import LiteGrid from './lite-grid';
  64. import FilterBox from './filter-box';
  65. export default {
  66. name: 'filter-grid',
  67. components: {
  68. LiteGrid,
  69. FilterBox,
  70. },
  71. props: {
  72. meta: { type: Array, required: true },
  73. readonly: Boolean /* 是否显示操作列 */,
  74. filter: { type: Boolean, default: false } /* 是否显示查询 */,
  75. action: { type: Boolean, default: false } /* 是否显操作按钮 */,
  76. paging: { type: Boolean, default: false } /* 是否显示分页 */,
  77. selection: { type: Boolean, default: false } /* 是否显示多选 */,
  78. currentRow: { type: Boolean, default: false }, // 是否显示单行高亮
  79. options: {
  80. type: Object,
  81. default: () => ({ size: 'mini' }),
  82. } /* 表格扩展属性 */,
  83. operation: Array,
  84. data: Array,
  85. total: { type: Number, default: 0 } /* 总数据条数 */,
  86. pageSize: { type: Number, default: 10 },
  87. filterProps: { type: Object } /* filterBox属性 */,
  88. matcher: { type: String },
  89. },
  90. data() {
  91. return {
  92. page: 1,
  93. size: this.pageSize,
  94. currentFilter: {},
  95. // selectionList: [],
  96. disableQuery: false,
  97. };
  98. },
  99. methods: {
  100. async query({ filter } = {}) {
  101. this.currentFilter = filter;
  102. this.$emit('query', {
  103. filter,
  104. paging: { page: this.page, size: this.size },
  105. });
  106. },
  107. handleSizeChange(val) {
  108. this.size = val;
  109. if (this.total === 0) return;
  110. if (this.page > this.pages) this.page = this.pages;
  111. this.refresh();
  112. },
  113. handleCurrentChange(val) {
  114. this.page = val;
  115. // this.refresh(); 改为由watch触发refresh
  116. },
  117. async handleOper({ event, data }) {
  118. this.$emit(event, data);
  119. },
  120. resetPage(page = 1) {
  121. if (page == -1) {
  122. this.page = this.pages; // 跳到尾页
  123. } else {
  124. this.page = 1;
  125. }
  126. },
  127. refresh({ page } = {}) {
  128. this.page = page || this.page;
  129. return this.query({ filter: this.currentFilter });
  130. },
  131. reset() {
  132. this.disableQuery = true;
  133. this.resetPage();
  134. this.$refs.filter.reset();
  135. this.disableQuery = false;
  136. },
  137. },
  138. computed: {
  139. filterFields() {
  140. return this.meta
  141. .map(FieldMeta)
  142. .filter(p => p.slots.filter)
  143. .sort((a, b) => a.filterOpts.order - b.filterOpts.order || a.index - b.index);
  144. },
  145. pages() {
  146. if (this.total == 0) return 1;
  147. return Math.floor((this.total + this.size - 1) / this.size);
  148. },
  149. },
  150. watch: {
  151. total(val, old) {
  152. if (val > 0 && val <= this.page * this.size) {
  153. if (this.page > this.pages) {
  154. this.page = this.pages;
  155. }
  156. } else if (val == 0 && this.page > 1) {
  157. this.disableQuery = true;
  158. this.resetPage();
  159. this.disableQuery = false;
  160. }
  161. },
  162. page(val, old) {
  163. if (val != old && !this.disableQuery) {
  164. this.refresh();
  165. }
  166. },
  167. data() {
  168. if (this.data && this.page < this.pages && this.data.length < this.size) {
  169. this.refresh();
  170. }
  171. },
  172. },
  173. };
  174. </script>
  175. <style lang="less" scoped>
  176. .container {
  177. width: 100%;
  178. height: 100%;
  179. max-height: 90vh;
  180. }
  181. .el-table {
  182. height: 100%;
  183. overflow: auto;
  184. }
  185. .el-main {
  186. padding: 10px;
  187. }
  188. </style>