2023-06-28 21:35:03 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="page-table">
|
|
|
|
|
|
<!--
|
|
|
|
|
|
实现:通过我们配置好的 查询条件
|
|
|
|
|
|
首先去创建form表单,根据我们配置的查询条件去做一个循环判断,展示出不用类型所对应不同的输入框
|
|
|
|
|
|
比如:text对应普通的输入框,select对应下拉选择,dateTime对应日期时间选择器
|
|
|
|
|
|
在使用时,父组件会传来一个queryForm空的对象,
|
|
|
|
|
|
循环出来的输入框会绑定表格配置中的prop字段绑定在queryForm对象中
|
|
|
|
|
|
-->
|
|
|
|
|
|
<el-card>
|
|
|
|
|
|
<div class="query" ref="queryRef">
|
|
|
|
|
|
<div>
|
2023-07-03 21:42:04 +08:00
|
|
|
|
<div v-if="props.query.length > 0">
|
2023-07-04 14:13:47 +08:00
|
|
|
|
<el-form :model="props.queryForm" label-width="auto" :size="props.size">
|
2023-07-03 21:42:04 +08:00
|
|
|
|
<el-row v-for="i in Math.ceil((props.query.length + 1) / (defaultQueryCount + 1))" :key="i"
|
|
|
|
|
|
v-show="i == 1 || isOpenMoreQuery" :class="i > 1 && isOpenMoreQuery ? 'is-open' : ''">
|
|
|
|
|
|
|
|
|
|
|
|
<el-form-item :label="item.label" style="margin-right: 12px; margin-bottom: 0px;"
|
|
|
|
|
|
v-for="item in getRowQueryItem(i)" :key="item.prop">
|
2023-06-28 21:35:03 +08:00
|
|
|
|
<!-- 这里只获取指定个数的筛选条件 -->
|
|
|
|
|
|
<el-input v-model="queryForm[item.prop]" :placeholder="'输入' + item.label + '关键字'"
|
|
|
|
|
|
clearable v-if="item.type == 'text'"></el-input>
|
|
|
|
|
|
|
|
|
|
|
|
<el-select-v2 v-model="queryForm[item.prop]" :options="item.options" clearable
|
|
|
|
|
|
:placeholder="'选择' + item.label + '关键字'" v-else-if="item.type == 'select'" />
|
|
|
|
|
|
|
|
|
|
|
|
<el-date-picker v-model="queryForm[item.prop]" clearable type="datetimerange"
|
|
|
|
|
|
format="YYYY-MM-DD hh:mm:ss" value-format="x" range-separator="至"
|
|
|
|
|
|
start-placeholder="开始时间" end-placeholder="结束时间" v-else-if="item.type == 'date'" />
|
|
|
|
|
|
|
2023-07-03 21:42:04 +08:00
|
|
|
|
<template v-else-if="item.slot == 'queryBtns'">
|
|
|
|
|
|
<template v-if="props.query?.length > defaultQueryCount">
|
|
|
|
|
|
<el-button @click="isOpenMoreQuery = !isOpenMoreQuery" v-if="!isOpenMoreQuery"
|
|
|
|
|
|
icon="ArrowDownBold" circle></el-button>
|
|
|
|
|
|
<el-button @click="isOpenMoreQuery = !isOpenMoreQuery" v-else icon="ArrowUpBold"
|
|
|
|
|
|
circle></el-button>
|
|
|
|
|
|
</template>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
2023-07-03 21:42:04 +08:00
|
|
|
|
<el-button @click="queryData()" type="primary" icon="search" plain>查询</el-button>
|
|
|
|
|
|
<el-button @click="reset()" icon="RefreshRight">重置</el-button>
|
|
|
|
|
|
</template>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
2023-07-03 21:42:04 +08:00
|
|
|
|
<slot :name="item.slot"></slot>
|
|
|
|
|
|
</el-form-item>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
2023-07-03 21:42:04 +08:00
|
|
|
|
</el-row>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</el-form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="slot">
|
|
|
|
|
|
<!-- 查询栏右侧slot插槽(用来添加表格其他操作,比如,新增数据,删除数据等其他操作) -->
|
|
|
|
|
|
<slot name="queryRight"></slot>
|
|
|
|
|
|
|
|
|
|
|
|
<!--
|
|
|
|
|
|
动态表头显示,根据表格每条配置项中的show字段来决定改列是否显示或者隐藏
|
|
|
|
|
|
columns 就是我们表格配置的数组对象
|
2023-07-03 21:42:04 +08:00
|
|
|
|
-->
|
2023-07-01 14:34:42 +08:00
|
|
|
|
<el-popover placement="bottom" title="表格配置"
|
|
|
|
|
|
popper-style="max-height: 550px; overflow: auto; max-width: 450px" width="auto" trigger="click">
|
2023-06-28 21:35:03 +08:00
|
|
|
|
<div v-for="(item, index) in props.columns" :key="index">
|
|
|
|
|
|
<el-checkbox v-model="item.show" :label="item.label" :true-label="true" :false-label="false" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<template #reference>
|
|
|
|
|
|
<!-- 一个Element Plus中的图标 -->
|
2023-07-04 14:13:47 +08:00
|
|
|
|
<el-button icon="Operation" :size="props.size"></el-button>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
</el-popover>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2023-07-05 22:06:32 +08:00
|
|
|
|
<el-table v-bind="$attrs" max-height="700" @selection-change="handleSelectionChange" :data="props.data"
|
2023-07-04 14:13:47 +08:00
|
|
|
|
highlight-current-row v-loading="loadingData" :size="props.size">
|
2023-07-01 14:34:42 +08:00
|
|
|
|
|
|
|
|
|
|
<el-table-column v-if="props.showSelection" type="selection" width="40" />
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
|
|
|
|
|
<template v-for="(item, index) in columns">
|
|
|
|
|
|
<el-table-column :key="index" v-if="item.show" :prop="item.prop" :label="item.label" :fixed="item.fixed"
|
2023-07-03 21:42:04 +08:00
|
|
|
|
:align="item.align" :show-overflow-tooltip="item.showOverflowTooltip" :min-width="item.minWidth"
|
|
|
|
|
|
:sortable="item.sortable || false" :type="item.type" :width="item.width">
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
|
|
|
|
|
<!-- 插槽:预留功能 -->
|
|
|
|
|
|
<template #default="scope" v-if="item.slot">
|
2023-07-01 21:24:07 +08:00
|
|
|
|
<slot :name="item.prop" :data="scope.row"></slot>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<template #default="scope" v-else>
|
2023-06-29 11:49:14 +08:00
|
|
|
|
<span>{{ item.getValueByData(scope.row)}}</span>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
</el-table-column>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-table>
|
|
|
|
|
|
|
|
|
|
|
|
<el-row style="margin-top: 20px" type="flex" justify="end">
|
2023-07-04 14:13:47 +08:00
|
|
|
|
<el-pagination :small="props.size == 'small'" @current-change="handlePageChange"
|
|
|
|
|
|
@size-change="handleSizeChange" style="text-align: right"
|
2023-06-28 21:35:03 +08:00
|
|
|
|
layout="prev, pager, next, total, sizes, jumper" :total="props.total"
|
2023-07-05 22:06:32 +08:00
|
|
|
|
v-model:current-page="state.pageNum" v-model:page-size="state.pageSize" :page-sizes="pageSizes" />
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</el-row>
|
|
|
|
|
|
</el-card>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang='ts' setup>
|
|
|
|
|
|
import { toRefs, watch, reactive, onMounted } from 'vue';
|
|
|
|
|
|
import { TableColumn, TableQuery } from './index';
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
const emit = defineEmits(['update:queryForm', 'update:pageNum', 'update:pageSize', 'update:selectionData', 'pageChange'])
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
2023-07-04 14:13:47 +08:00
|
|
|
|
size: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
default: "",
|
|
|
|
|
|
},
|
|
|
|
|
|
inputWidth: {
|
|
|
|
|
|
type: [Number, String],
|
|
|
|
|
|
default: 0,
|
|
|
|
|
|
},
|
2023-06-28 21:35:03 +08:00
|
|
|
|
// 是否显示选择列
|
2023-07-01 14:34:42 +08:00
|
|
|
|
showSelection: {
|
2023-06-28 21:35:03 +08:00
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
// 当前选择的数据
|
2023-07-01 14:34:42 +08:00
|
|
|
|
selectionData: {
|
|
|
|
|
|
type: Array<any>
|
2023-06-28 21:35:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
// 列信息
|
|
|
|
|
|
columns: {
|
|
|
|
|
|
type: Array<TableColumn>,
|
|
|
|
|
|
default: function () {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
// 表格数据
|
|
|
|
|
|
data: {
|
|
|
|
|
|
type: Array,
|
|
|
|
|
|
},
|
|
|
|
|
|
total: {
|
|
|
|
|
|
type: [Number],
|
|
|
|
|
|
default: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
pageNum: {
|
|
|
|
|
|
type: Number,
|
|
|
|
|
|
default: 1,
|
|
|
|
|
|
},
|
|
|
|
|
|
pageSize: {
|
|
|
|
|
|
type: [Number],
|
|
|
|
|
|
default: 10,
|
|
|
|
|
|
},
|
2023-07-04 14:13:47 +08:00
|
|
|
|
// 查询条件配置
|
|
|
|
|
|
query: {
|
|
|
|
|
|
type: Array<TableQuery>,
|
|
|
|
|
|
default: function () {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2023-06-28 21:35:03 +08:00
|
|
|
|
// 绑定的查询表单
|
|
|
|
|
|
queryForm: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
default: function () {
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const state = reactive({
|
2023-07-05 22:06:32 +08:00
|
|
|
|
pageSizes: [] as any, // 可选每页显示的数据量
|
2023-06-28 21:35:03 +08:00
|
|
|
|
pageSize: 10,
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
isOpenMoreQuery: false,
|
2023-07-05 22:06:32 +08:00
|
|
|
|
defaultQueryCount: 2, // 默认显示的查询参数个数,展开后每行显示查询条件个数为该值加1。第一行用最后一列来占用按钮
|
2023-06-28 21:35:03 +08:00
|
|
|
|
queryForm: {} as any,
|
2023-07-01 14:34:42 +08:00
|
|
|
|
loadingData: false,
|
2023-07-04 14:13:47 +08:00
|
|
|
|
// 输入框宽度
|
|
|
|
|
|
inputWidth: "200px" as any,
|
2023-06-28 21:35:03 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
2023-07-05 22:06:32 +08:00
|
|
|
|
pageSizes,
|
2023-06-28 21:35:03 +08:00
|
|
|
|
isOpenMoreQuery,
|
|
|
|
|
|
defaultQueryCount,
|
|
|
|
|
|
queryForm,
|
2023-07-01 14:34:42 +08:00
|
|
|
|
loadingData,
|
2023-07-04 14:13:47 +08:00
|
|
|
|
inputWidth,
|
2023-06-28 21:35:03 +08:00
|
|
|
|
} = toRefs(state)
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.queryForm, (newValue: any) => {
|
|
|
|
|
|
state.queryForm = newValue
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.pageNum, (newValue: any) => {
|
|
|
|
|
|
state.pageNum = newValue
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.pageSize, (newValue: any) => {
|
|
|
|
|
|
state.pageSize = newValue
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.data, (newValue: any) => {
|
2023-07-05 22:06:32 +08:00
|
|
|
|
if (newValue && newValue.length > 0) {
|
2023-06-28 21:35:03 +08:00
|
|
|
|
props.columns.forEach(item => {
|
|
|
|
|
|
if (item.autoWidth && item.show) {
|
2023-06-29 11:49:14 +08:00
|
|
|
|
item.autoCalculateMinWidth(props.data);
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
2023-07-05 22:06:32 +08:00
|
|
|
|
const pageSize = props.pageSize;
|
|
|
|
|
|
|
2023-06-28 21:35:03 +08:00
|
|
|
|
state.pageNum = props.pageNum;
|
2023-07-05 22:06:32 +08:00
|
|
|
|
state.pageSize = pageSize;
|
2023-06-28 21:35:03 +08:00
|
|
|
|
state.queryForm = props.queryForm;
|
2023-07-05 22:06:32 +08:00
|
|
|
|
state.pageSizes = [pageSize, pageSize * 2, pageSize * 3, pageSize * 4]
|
2023-07-04 14:13:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果没传输入框宽度,则根据组件size设置默认宽度
|
|
|
|
|
|
if (!props.inputWidth) {
|
|
|
|
|
|
state.inputWidth = props.size == "small" ? "150px" : "200px"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
state.inputWidth = props.inputWidth;
|
|
|
|
|
|
}
|
2023-06-28 21:35:03 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2023-07-03 21:42:04 +08:00
|
|
|
|
const getRowQueryItem = (row: number) => {
|
|
|
|
|
|
// 第一行需要加个查询等按钮列
|
|
|
|
|
|
if (row === 1) {
|
|
|
|
|
|
const res = props.query.slice(row - 1, defaultQueryCount.value)
|
|
|
|
|
|
// 查询等按钮列
|
|
|
|
|
|
res.push(TableQuery.slot("", "", "queryBtns"))
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
const columnCount = defaultQueryCount.value + 1;
|
|
|
|
|
|
return props.query.slice((row - 1) * columnCount - 1, row * columnCount - 1)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
const handleSelectionChange = (val: any) => {
|
|
|
|
|
|
emit('update:selectionData', val);
|
|
|
|
|
|
}
|
2023-06-28 21:35:03 +08:00
|
|
|
|
|
|
|
|
|
|
const handlePageChange = () => {
|
|
|
|
|
|
emit('update:pageNum', state.pageNum)
|
2023-07-01 14:34:42 +08:00
|
|
|
|
execQuery();
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleSizeChange = () => {
|
2023-07-05 22:06:32 +08:00
|
|
|
|
changePageNum(1);
|
2023-07-01 14:34:42 +08:00
|
|
|
|
emit('update:pageSize', state.pageSize);
|
|
|
|
|
|
execQuery();
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const queryData = () => {
|
2023-07-04 14:13:47 +08:00
|
|
|
|
changePageNum(1);
|
2023-07-01 14:34:42 +08:00
|
|
|
|
execQuery();
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const reset = () => {
|
2023-07-04 14:13:47 +08:00
|
|
|
|
// 将查询参数绑定的值置空,并重新粗发查询接口
|
|
|
|
|
|
for (let qi of props.query) {
|
|
|
|
|
|
state.queryForm[qi.prop] = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
changePageNum(1);
|
|
|
|
|
|
emit('update:queryForm', state.queryForm);
|
2023-07-01 14:34:42 +08:00
|
|
|
|
execQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-04 14:13:47 +08:00
|
|
|
|
const changePageNum = (pageNum: number) => {
|
|
|
|
|
|
state.pageNum = pageNum;
|
|
|
|
|
|
emit('update:pageNum', state.pageNum)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
const execQuery = () => {
|
2023-06-28 21:35:03 +08:00
|
|
|
|
emit("pageChange")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-01 14:34:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 是否正在加载数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
const loading = (loading: boolean) => {
|
|
|
|
|
|
state.loadingData = loading;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defineExpose({ loading })
|
|
|
|
|
|
|
2023-06-28 21:35:03 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
|
.page-table {
|
|
|
|
|
|
.query {
|
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
|
|
|
|
|
|
.is-open {
|
2023-07-03 21:42:04 +08:00
|
|
|
|
// padding: 10px 0;
|
2023-06-28 21:35:03 +08:00
|
|
|
|
max-height: 200px;
|
2023-07-03 21:42:04 +08:00
|
|
|
|
margin-top: 10px;
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: flex-start;
|
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
|
|
|
|
|
|
|
.slot {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.page {
|
|
|
|
|
|
margin-top: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
::v-deep(.el-form-item__label) {
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-04 14:13:47 +08:00
|
|
|
|
.el-select-v2 {
|
|
|
|
|
|
width: v-bind(inputWidth);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-28 21:35:03 +08:00
|
|
|
|
.el-input {
|
2023-07-04 14:13:47 +08:00
|
|
|
|
width: v-bind(inputWidth);
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-04 14:13:47 +08:00
|
|
|
|
.el-select {
|
|
|
|
|
|
width: v-bind(inputWidth);
|
2023-06-28 21:35:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-04 14:13:47 +08:00
|
|
|
|
.el-date-editor {
|
2023-06-28 21:35:03 +08:00
|
|
|
|
width: 380px !important;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|