123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <template>
- <el-container class="container">
- <el-header height="auto" style="line-height:36px;" v-if="filter || action">
- <slot name="filter" v-if="filter">
- <filter-box ref="filter" :matcher="matcher" :fields="filterFields" @query="query" v-bind="filterProps">
- <template v-slot:ext="form" v-if="$scopedSlots['filter-ext']">
- <slot name="filter-ext" v-bind="form"></slot>
- </template>
- </filter-box>
- </slot>
- <slot name="action" v-if="action">
- <el-button icon="el-icon-plus" type="primary" size="mini" v-if="!readonly" @click="$emit('add-new')">添加</el-button>
- </slot>
- </el-header>
- <el-main class="table-area">
- <lite-grid
- :data="data"
- :meta="meta"
- :selection="selection"
- :options="options"
- :readonly="readonly"
- :operation="operation"
- :currentRow="currentRow"
- @oper="handleOper"
- @selection="$emit('selection', $event)"
- @dblclick="$emit('dblclick', $event)"
- @currentRow="$emit('row', $event)"
- >
- <template v-slot:pre>
- <slot name="pre"> </slot>
- </template>
- <slot> </slot>
- <template v-slot:oper>
- <slot name="oper"> </slot>
- </template>
- <template v-slot:oper_header>
- <slot name="oper_header"> </slot>
- </template>
- <template v-slot:ext>
- <slot name="ext"> </slot>
- </template>
- <template v-slot:post>
- <slot name="post"> </slot>
- </template>
- </lite-grid>
- </el-main>
- <el-footer height="36px" v-if="paging">
- <el-pagination
- background
- @size-change="handleSizeChange"
- @current-change="handleCurrentChange"
- :current-page="page"
- :page-sizes="[10, 20, 50, 100, 200]"
- :page-size="size"
- :total="total"
- layout="total, sizes, prev, pager, next, jumper"
- ></el-pagination>
- </el-footer>
- </el-container>
- </template>
- <script>
- import { FieldMeta } from './meta-util';
- import LiteGrid from './lite-grid';
- import FilterBox from './filter-box';
- export default {
- name: 'filter-grid',
- components: {
- LiteGrid,
- FilterBox,
- },
- props: {
- meta: { type: Array, required: true },
- readonly: Boolean /* 是否显示操作列 */,
- filter: { type: Boolean, default: false } /* 是否显示查询 */,
- action: { type: Boolean, default: false } /* 是否显操作按钮 */,
- paging: { type: Boolean, default: false } /* 是否显示分页 */,
- selection: { type: Boolean, default: false } /* 是否显示多选 */,
- currentRow: { type: Boolean, default: false }, // 是否显示单行高亮
- options: {
- type: Object,
- default: () => ({ size: 'mini' }),
- } /* 表格扩展属性 */,
- operation: Array,
- data: Array,
- total: { type: Number, default: 0 } /* 总数据条数 */,
- pageSize: { type: Number, default: 10 },
- filterProps: { type: Object } /* filterBox属性 */,
- matcher: { type: String },
- },
- data() {
- return {
- page: 1,
- size: this.pageSize,
- currentFilter: {},
- // selectionList: [],
- disableQuery: false,
- };
- },
- methods: {
- async query({ filter } = {}) {
- this.currentFilter = filter;
- this.$emit('query', {
- filter,
- paging: { page: this.page, size: this.size },
- });
- },
- handleSizeChange(val) {
- this.size = val;
- if (this.total === 0) return;
- if (this.page > this.pages) this.page = this.pages;
- this.refresh();
- },
- handleCurrentChange(val) {
- this.page = val;
- // this.refresh(); 改为由watch触发refresh
- },
- async handleOper({ event, data }) {
- this.$emit(event, data);
- },
- resetPage(page = 1) {
- if (page == -1) {
- this.page = this.pages; // 跳到尾页
- } else {
- this.page = 1;
- }
- },
- refresh({ page } = {}) {
- this.page = page || this.page;
- return this.query({ filter: this.currentFilter });
- },
- reset() {
- this.disableQuery = true;
- this.resetPage();
- this.$refs.filter.reset();
- this.disableQuery = false;
- },
- },
- computed: {
- filterFields() {
- return this.meta
- .map(FieldMeta)
- .filter(p => p.slots.filter)
- .sort((a, b) => a.filterOpts.order - b.filterOpts.order || a.index - b.index);
- },
- pages() {
- if (this.total == 0) return 1;
- return Math.floor((this.total + this.size - 1) / this.size);
- },
- },
- watch: {
- total(val, old) {
- if (val > 0 && val <= this.page * this.size) {
- if (this.page > this.pages) {
- this.page = this.pages;
- }
- } else if (val == 0 && this.page > 1) {
- this.disableQuery = true;
- this.resetPage();
- this.disableQuery = false;
- }
- },
- page(val, old) {
- if (val != old && !this.disableQuery) {
- this.refresh();
- }
- },
- data() {
- if (this.data && this.page < this.pages && this.data.length < this.size) {
- this.refresh();
- }
- },
- },
- };
- </script>
- <style lang="less" scoped>
- .container {
- width: 100%;
- height: 100%;
- max-height: 90vh;
- }
- .el-table {
- height: 100%;
- overflow: auto;
- }
- .el-main {
- padding: 10px;
- }
- </style>
|