Files
admin-govern/src/components/table/header/index.vue

298 lines
8.5 KiB
Vue
Raw Normal View History

2023-12-26 16:17:30 +08:00
<template>
2024-01-05 13:56:33 +08:00
<div ref="tableHeader" class="cn-table-header">
2024-10-22 10:50:47 +08:00
<div class="table-header ba-scroll-style" :key="num">
<el-form style="flex: 1; height: 34px; margin-right: 20px; display: flex; flex-wrap: wrap" ref="headerForm"
@submit.prevent="" @keyup.enter="onComSearch" label-position="left" :inline="true">
2024-09-24 10:19:54 +08:00
<el-form-item label="日期" v-if="datePicker" style="grid-column: span 2; max-width: 570px">
<DatePicker ref="datePickerRef" :nextFlag="nextFlag" :theCurrentTime="theCurrentTime"></DatePicker>
</el-form-item>
2024-01-30 16:52:13 +08:00
<el-form-item label="区域" v-if="area">
<Area ref="areaRef" v-model="tableStore.table.params.deptIndex" />
</el-form-item>
<slot name="select"></slot>
</el-form>
2024-09-24 10:19:54 +08:00
<template v-if="$slots.select || datePicker || showSearch">
2024-01-30 16:52:13 +08:00
<el-button type="primary" @click="showSelectChange" v-if="showUnfoldButton">
<Icon size="14" name="el-icon-ArrowUp" style="color: #fff" v-if="showSelect" />
<Icon size="14" name="el-icon-ArrowDown" style="color: #fff" v-else />
</el-button>
2024-04-01 18:29:32 +08:00
<el-button @click="onComSearch" v-if="showSearch" type="primary" :icon="Search">查询</el-button>
2024-11-19 10:39:46 +08:00
<el-button @click="onResetForm" v-if="showSearch && showReset" :icon="RefreshLeft">重置</el-button>
</template>
2024-01-05 13:56:33 +08:00
<slot name="operation"></slot>
2023-12-26 16:17:30 +08:00
</div>
2024-10-22 10:50:47 +08:00
<el-form :style="showSelect && showUnfoldButton ? headerFormSecondStyleOpen : headerFormSecondStyleClose"
ref="headerFormSecond" @submit.prevent="" @keyup.enter="onComSearch" label-position="left"
:inline="true"></el-form>
2023-12-26 16:17:30 +08:00
</div>
</template>
2024-01-05 13:56:33 +08:00
<script setup lang="ts">
2024-01-04 10:38:41 +08:00
import { inject, ref, onMounted, nextTick, onUnmounted } from 'vue'
2023-12-26 16:17:30 +08:00
import type TableStore from '@/utils/tableStore'
2023-12-29 10:05:09 +08:00
import DatePicker from '@/components/form/datePicker/index.vue'
2024-01-19 09:23:31 +08:00
import Area from '@/components/form/area/index.vue'
2024-01-04 10:38:41 +08:00
import { mainHeight } from '@/utils/layout'
2024-01-19 09:23:31 +08:00
import { useDictData } from '@/stores/dictData'
2024-01-24 15:42:54 +08:00
import { Search, RefreshLeft } from '@element-plus/icons-vue'
2024-09-24 10:19:54 +08:00
import { defineProps } from 'vue'
2024-09-30 11:08:35 +08:00
const emit = defineEmits(['selectChange',])
2024-01-24 15:42:54 +08:00
2023-12-26 16:17:30 +08:00
const tableStore = inject('tableStore') as TableStore
2024-01-04 10:38:41 +08:00
const tableHeader = ref()
2024-01-11 08:54:09 +08:00
const datePickerRef = ref()
2024-01-19 09:23:31 +08:00
const dictData = useDictData()
2024-01-22 20:24:50 +08:00
const areaRef = ref()
2024-04-01 18:29:32 +08:00
const headerForm = ref()
const headerFormSecond = ref()
2024-10-22 10:50:47 +08:00
const num = ref(0)
2023-12-27 10:39:40 +08:00
interface Props {
2023-12-27 16:36:10 +08:00
datePicker?: boolean
2024-01-19 09:23:31 +08:00
area?: boolean
2024-04-01 18:29:32 +08:00
showSearch?: boolean
2024-09-24 10:19:54 +08:00
nextFlag?: boolean //控制时间是否可以往后推
theCurrentTime?: boolean //控制时间前3天展示上个月时间
2024-12-11 11:33:44 +08:00
showReset?: boolean //是否显示重置
2023-12-27 10:39:40 +08:00
}
const props = withDefaults(defineProps<Props>(), {
2024-01-19 09:23:31 +08:00
datePicker: false,
2024-04-01 18:29:32 +08:00
area: false,
2024-09-24 10:19:54 +08:00
showSearch: true,
nextFlag: false,
2024-11-19 10:39:46 +08:00
theCurrentTime: true,
showReset: true
2023-12-27 10:39:40 +08:00
})
2024-01-04 10:38:41 +08:00
// 动态计算table高度
2024-10-22 10:50:47 +08:00
let resizeObserver = new ResizeObserver(entries => {
2024-01-04 10:38:41 +08:00
for (const entry of entries) {
handlerHeight()
2024-01-30 16:52:13 +08:00
computedSearchRow()
2024-01-04 10:38:41 +08:00
}
})
2024-01-04 16:39:05 +08:00
const showUnfoldButton = ref(false)
2024-01-30 16:52:13 +08:00
const headerFormSecondStyleOpen = {
opacity: 1,
height: 'auto',
2024-11-12 11:41:46 +08:00
padding: '0 15px 0px 15px'
2024-01-30 16:52:13 +08:00
}
const headerFormSecondStyleClose = {
opacity: 0,
height: '0',
padding: '0'
}
2024-01-04 10:38:41 +08:00
onMounted(() => {
2024-09-25 16:31:45 +08:00
if (props.datePicker && tableStore) {
tableStore.table.params.searchBeginTime = datePickerRef.value?.timeValue[0]
tableStore.table.params.searchEndTime = datePickerRef.value?.timeValue[1]
tableStore.table.params.startTime = datePickerRef.value?.timeValue[0]
tableStore.table.params.endTime = datePickerRef.value?.timeValue[1]
tableStore.table.params.timeFlag = datePickerRef.value?.timeFlag
2024-01-05 13:56:33 +08:00
}
2024-01-19 09:23:31 +08:00
if (props.area) {
tableStore.table.params.deptIndex = dictData.state.area[0].id
}
2024-01-04 16:39:05 +08:00
nextTick(() => {
resizeObserver.observe(tableHeader.value)
2024-01-30 16:52:13 +08:00
computedSearchRow()
2024-01-04 16:39:05 +08:00
})
2024-01-04 10:38:41 +08:00
})
onUnmounted(() => {
resizeObserver.disconnect()
})
const handlerHeight = () => {
2024-09-25 16:31:45 +08:00
if (tableStore && tableStore.table) {
tableStore.table.height = mainHeight(
tableStore.table.publicHeight + tableHeader.value.offsetHeight + (tableStore.showPage ? 58 : 0) + 20
).height as string
}
2024-01-04 10:38:41 +08:00
}
2024-10-22 10:50:47 +08:00
// 刷新页面handler高度出下拉
2024-01-30 16:52:13 +08:00
const computedSearchRow = () => {
2024-11-19 10:39:46 +08:00
2024-04-01 18:29:32 +08:00
if (!headerForm.value.$el) return
2024-01-30 16:52:13 +08:00
2024-10-22 10:50:47 +08:00
2024-04-01 18:29:32 +08:00
// 清空headerFormSecond.value.$el下的元素
while (headerFormSecond.value.$el.firstChild) {
headerForm.value.$el.appendChild(headerFormSecond.value.$el.firstChild)
2024-01-30 16:52:13 +08:00
}
// 获取第一行放了几个表单
2024-04-01 18:29:32 +08:00
const elFormItem = headerForm.value.$el.querySelectorAll('.el-form-item')
// 把第一行放不下的复制一份放到headerFormSecond.value.$el
2024-01-30 16:52:13 +08:00
let width = 0
for (let i = 0; i < elFormItem.length; i++) {
width += elFormItem[i].offsetWidth + 32
2024-04-01 18:29:32 +08:00
if (width > headerForm.value.$el.offsetWidth) {
headerFormSecond.value.$el.appendChild(elFormItem[i])
2024-01-30 16:52:13 +08:00
}
}
2024-04-01 18:29:32 +08:00
// 判断是否需要折叠
if (headerFormSecond.value.$el.scrollHeight > 0) {
showUnfoldButton.value = true
} else {
showUnfoldButton.value = false
}
2024-01-30 16:52:13 +08:00
}
2024-01-04 16:39:05 +08:00
const showSelect = ref(false)
2023-12-27 10:39:40 +08:00
const showSelectChange = () => {
showSelect.value = !showSelect.value
2024-09-30 11:08:35 +08:00
emit('selectChange', showSelect.value)
2023-12-27 10:39:40 +08:00
}
2024-01-05 09:14:53 +08:00
const onComSearch = async () => {
2024-01-05 13:56:33 +08:00
if (props.datePicker) {
2024-01-11 08:54:09 +08:00
tableStore.table.params.searchBeginTime = datePickerRef.value.timeValue[0]
tableStore.table.params.searchEndTime = datePickerRef.value.timeValue[1]
2024-01-11 16:40:44 +08:00
tableStore.table.params.startTime = datePickerRef.value.timeValue[0]
tableStore.table.params.endTime = datePickerRef.value.timeValue[1]
2024-01-11 08:54:09 +08:00
tableStore.table.params.timeFlag = datePickerRef.value.timeFlag
2024-01-05 13:56:33 +08:00
}
2024-01-05 09:14:53 +08:00
await tableStore.onTableAction('search', {})
2023-12-27 10:39:40 +08:00
}
2024-09-24 10:19:54 +08:00
const setDatePicker = (list: any) => {
datePickerRef.value.setTimeOptions(list)
}
2023-12-27 10:39:40 +08:00
const onResetForm = () => {
2024-06-19 20:32:10 +08:00
//时间重置成默认值
2024-09-24 10:19:54 +08:00
datePickerRef.value?.setInterval(3)
2023-12-27 10:39:40 +08:00
tableStore.onTableAction('reset', {})
2023-12-26 16:17:30 +08:00
}
2024-04-01 18:29:32 +08:00
const setInterval = (val: any) => {
datePickerRef.value.setInterval(val)
}
2024-10-22 10:50:47 +08:00
defineExpose({ onComSearch, areaRef, setDatePicker, setInterval, datePickerRef, showSelectChange, computedSearchRow })
2023-12-26 16:17:30 +08:00
</script>
2024-01-05 13:56:33 +08:00
<style scoped lang="scss">
2024-04-01 18:29:32 +08:00
.cn-table-header {
border: 1px solid var(--el-border-color);
}
2024-10-22 10:50:47 +08:00
2023-12-26 16:17:30 +08:00
.table-header {
position: relative;
overflow-x: auto;
box-sizing: border-box;
display: flex;
align-items: center;
width: 100%;
max-width: 100%;
background-color: var(--ba-bg-color-overlay);
2024-04-01 18:29:32 +08:00
2023-12-26 16:17:30 +08:00
border-bottom: none;
2024-10-14 20:15:54 +08:00
padding: 13px 15px 9px;
2023-12-26 16:17:30 +08:00
font-size: 14px;
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.table-header-operate-text {
margin-left: 6px;
}
}
.table-com-search {
box-sizing: border-box;
width: 100%;
max-width: 100%;
background-color: var(--ba-bg-color-overlay);
border: 1px solid var(--ba-border-color);
border-bottom: none;
2024-01-04 16:39:05 +08:00
padding: 13px 15px 20px 15px;
2023-12-26 16:17:30 +08:00
font-size: 14px;
2024-01-04 16:39:05 +08:00
}
2024-01-05 11:35:13 +08:00
2024-01-30 16:52:13 +08:00
#header-form-second,
#header-form {
2024-02-02 09:29:19 +08:00
// display: flex;
// flex-wrap: wrap;
2024-02-02 09:58:14 +08:00
transition: all 0.3s;
2024-01-04 16:39:05 +08:00
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.mlr-12 {
margin-left: 12px;
}
2023-12-27 10:39:40 +08:00
2024-10-22 10:50:47 +08:00
.mlr-12+.el-button {
2023-12-26 16:17:30 +08:00
margin-left: 12px;
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.table-search {
display: flex;
margin-left: auto;
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.quick-search {
width: auto;
}
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.table-search-button-group {
display: flex;
margin-left: 12px;
border: 1px solid var(--el-border-color);
border-radius: var(--el-border-radius-base);
overflow: hidden;
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
button:focus,
button:active {
background-color: var(--ba-bg-color-overlay);
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
button:hover {
background-color: var(--el-color-info-light-7);
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.table-search-button-item {
height: 30px;
border: none;
border-radius: 0;
}
2023-12-27 10:39:40 +08:00
2024-10-22 10:50:47 +08:00
.el-button+.el-button {
2023-12-26 16:17:30 +08:00
margin: 0;
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
.right-border {
border-right: 1px solid var(--el-border-color);
}
}
html.dark {
.table-search-button-group {
2024-10-22 10:50:47 +08:00
2023-12-26 16:17:30 +08:00
button:focus,
button:active {
background-color: var(--el-color-info-dark-2);
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
button:hover {
background-color: var(--el-color-info-light-7);
}
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
button {
background-color: var(--ba-bg-color-overlay);
2023-12-27 10:39:40 +08:00
2023-12-26 16:17:30 +08:00
el-icon {
color: white !important;
}
}
}
}
2024-10-22 10:50:47 +08:00
2024-02-02 09:29:19 +08:00
#header-form,
#header-form-second {
:deep(.el-select) {
--el-select-width: 220px;
}
2024-10-22 10:50:47 +08:00
2024-02-02 09:29:19 +08:00
:deep(.el-input) {
--el-input-width: 220px;
}
}
2023-12-26 16:17:30 +08:00
</style>