[feat] 重构客户信息和供应商信息页面的搜索栏- 移除原有的冗余代码和复杂逻辑

- 新增 SearchBarEx 组件用于通用搜索栏
- 优化搜索栏的显示和交互逻辑
- 统一处理搜索、重置、新增和导出操作
This commit is contained in:
hhyykk 2024-12-13 10:44:23 +08:00
parent 91ce5f9e12
commit 692907bd43
3 changed files with 264 additions and 214 deletions

View File

@ -0,0 +1,204 @@
<template>
<el-form
class="-mb-15px"
:model="localQueryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<!-- 第一行搜索内容 -->
<el-row :gutter="20">
<!-- 左边部分最多4个搜索条件 -->
<el-col :span="22">
<!-- 上面的部分默认展示4个搜索条件 -->
<el-row :gutter="20">
<el-col :span="6" v-for="(field, index) in visibleFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<component
:is="field.component || 'el-input'"
v-model="localQueryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
v-bind="field.props"
>
<template v-if="field.component === 'el-select'">
<el-option
v-for="option in field.options"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</template>
<template v-if="field.component === 'el-tree-select'">
<el-tree-select
v-model="localQueryParams[field.prop]"
:data="field.treeData"
:props="field.treeProps"
:clearable="true"
placeholder="请选择"
:node-key="field.nodeKey"
/>
</template>
</component>
</el-form-item>
</el-col>
</el-row>
<!-- 下面的部分隐藏的搜索条件 -->
<el-collapse-transition>
<el-row :gutter="20" v-show="showMore">
<el-col :span="6" v-for="(field, index) in hiddenFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<component
:is="field.component || 'el-input'"
v-model="localQueryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
v-bind="field.props"
>
<template v-if="field.component === 'el-select'">
<el-option
v-for="option in field.options"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</template>
<template v-if="field.component === 'el-tree-select'">
<el-tree-select
v-model="localQueryParams[field.prop]"
:data="field.treeData"
:props="field.treeProps"
:clearable="true"
placeholder="请选择"
:node-key="field.nodeKey"
/>
</template>
</component>
</el-form-item>
</el-col>
</el-row>
</el-collapse-transition>
</el-col>
<!-- 右边部分展开按钮 -->
<el-col :span="2" style="text-align: right;" v-if="hasMoreFields">
<el-form-item>
<el-button type="text" @click="toggleShowMore">
{{ showMore ? '收起' : '展开更多' }}
</el-button>
</el-form-item>
</el-col>
</el-row>
<!-- 第二行搜索按钮 -->
<el-row :gutter="20">
<el-col :span="24" style="text-align: right;">
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button
type="primary"
plain
@click="handleCreate"
v-hasPermi="['pm:customer:create']"
>
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
<el-button
type="success"
plain
@click="handleExport"
:loading="exportLoading"
v-hasPermi="['pm:customer:export']"
>
<Icon icon="ep:download" class="mr-5px" /> 导出
</el-button>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
// props
const props = defineProps<{
fields: any[], //
queryParams: any //
}>()
// emits
const emit = defineEmits(['query', 'reset', 'create', 'export'])
// props queryParams
const localQueryParams = ref({ ...props.queryParams })
//
const queryFormRef = ref()
//
const exportLoading = ref(false)
//
const showMore = ref(false)
// 4
const visibleFields = computed(() => props.fields.slice(0, 4))
// 5
const hiddenFields = computed(() => props.fields.slice(4))
//
const hasMoreFields = computed(() => props.fields.length > 4)
/**
* 切换更多搜索项的显示状态
*/
const toggleShowMore = () => {
showMore.value = !showMore.value
}
/**
* 查询按钮操作
* 触发 query 事件传递当前查询参数
*/
const handleQuery = () => {
emit('query', { ...localQueryParams.value, pageNo: 1 })
}
/**
* 重置按钮操作
* 重置表单并触发 reset 事件
*/
const resetQuery = () => {
queryFormRef.value.resetFields()
emit('reset')
}
/**
* 新增按钮操作
* 触发 create 事件
*/
const handleCreate = () => {
emit('create')
}
/**
* 导出按钮操作
* 触发 export 事件
*/
const handleExport = () => {
emit('export')
}
/**
* 监听 queryParams 的变化
* queryParams 发生变化时更新 localQueryParams
*/
watch(
() => props.queryParams,
(newVal) => {
localQueryParams.value = { ...newVal }
},
{ deep: true }
)
</script>

View File

@ -1,87 +1,13 @@
<template>
<ContentWrap>
<!-- 搜索工作栏 -->
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<!-- 第一行搜索内容 -->
<el-row :gutter="20">
<!-- 左边部分最多4个搜索条件 -->
<el-col :span="22">
<el-row :gutter="20">
<el-col :span="6" v-for="(field, index) in visibleFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<el-input
v-model="queryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
</el-col>
</el-row>
</el-col>
<!-- 右边部分展开按钮 -->
<el-col :span="2" style="text-align: right;" v-if="hasMoreFields">
<el-form-item>
<el-button type="text" @click="toggleShowMore">
{{ showMore ? '收起' : '展开更多' }}
</el-button>
</el-form-item>
</el-col>
</el-row>
<!-- 更多搜索条件 -->
<el-collapse-transition>
<div v-show="showMore">
<el-row :gutter="22">
<el-col :span="6" v-for="(field, index) in hiddenFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<el-input
v-model="queryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
</el-col>
</el-row>
</div>
</el-collapse-transition>
<!-- 第二行搜索按钮 -->
<el-row :gutter="20">
<el-col :span="24" style="text-align: right;">
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button
type="primary"
plain
@click="openForm('create')"
v-hasPermi="['pm:customer:create']"
>
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
<el-button
type="success"
plain
@click="handleExport"
:loading="exportLoading"
v-hasPermi="['pm:customer:export']"
>
<Icon icon="ep:download" class="mr-5px" /> 导出
</el-button>
</el-form-item>
</el-col>
</el-row>
</el-form>
<SearchBarEx
:fields="allFields"
:queryParams="queryParams"
@query="handleQuery"
@reset="resetQuery"
@create="openForm('create')"
@export="handleExport"
/>
</ContentWrap>
<!-- 列表 -->
@ -132,7 +58,8 @@
import download from '@/utils/download'
import { CustomerApi, CustomerVO } from '@/api/pm/customer'
import CustomerForm from './CustomerForm.vue'
import { ref, onMounted, computed } from 'vue'
import { ref, onMounted, reactive } from 'vue'
import SearchBarEx from "@/components/SearchBar/SearchBarEx.vue";
/** 客户信息 列表 */
defineOptions({ name: 'Customer' })
@ -151,11 +78,20 @@ const queryParams = reactive({
contacts: undefined,
phone: undefined,
address: undefined,
address2: undefined,
date: undefined,
dateRange: undefined,
treeSelect: undefined,
//
})
const queryFormRef = ref() //
const exportLoading = ref(false) //
const showMore = ref(false) //
const formRef = ref()
//
// const treeData = [
// { id: 1, label: ' 1', children: [{ id: 4, label: ' 1-1' }] },
// { id: 2, label: ' 2', children: [{ id: 5, label: ' 2-1' }] },
// { id: 3, label: ' 3', children: [{ id: 6, label: ' 3-1' }] }
// ]
//
const allFields = [
@ -164,18 +100,14 @@ const allFields = [
{ label: '联系人', prop: 'contacts' },
{ label: '电话', prop: 'phone' },
{ label: '地址', prop: 'address' },
//
//
// { label: '', prop: 'address2', component: 'el-select', options: [{ label: '1', value: '1' }, { label: '2', value: '2' }] },
// { label: '', prop: 'date', component: 'el-date-picker', props: { type: 'date', format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD' } },
// { label: '', prop: 'dateRange', component: 'el-date-picker', props: { type: 'daterange', rangeSeparator: '-', startPlaceholder: '', endPlaceholder: '', format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD' } },
// { label: '', prop: 'treeSelect', component: 'el-tree-select', treeData: treeData, treeProps: { label: 'label', children: 'children' }, nodeKey: 'id' }
]
//
const visibleFields = computed(() => allFields.slice(0, 4))
const hiddenFields = computed(() => allFields.slice(4))
const hasMoreFields = computed(() => allFields.length > 4)
/** 切换更多搜索项的显示 */
const toggleShowMore = () => {
showMore.value = !showMore.value
}
/** 查询列表 */
const getList = async () => {
@ -190,19 +122,23 @@ const getList = async () => {
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
const handleQuery = (params: any) => {
Object.assign(queryParams, params)
getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
queryFormRef.value.resetFields()
handleQuery()
for (const key in queryParams) {
if (key !== 'pageNo' && key !== 'pageSize') {
queryParams[key] = undefined
}
}
queryParams.pageNo = 1
getList()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
@ -226,13 +162,9 @@ const handleExport = async () => {
//
await message.exportConfirm()
//
exportLoading.value = true
const data = await CustomerApi.exportCustomer(queryParams)
download.excel(data, '客户信息.xls')
} catch {
} finally {
exportLoading.value = false
}
} catch {}
}
/** 初始化 **/

View File

@ -1,87 +1,14 @@
<template>
<ContentWrap>
<!-- 搜索工作栏 -->
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<!-- 第一行搜索内容 -->
<el-row :gutter="20">
<!-- 左边部分最多4个搜索条件 -->
<el-col :span="22">
<el-row :gutter="20">
<el-col :span="6" v-for="(field, index) in visibleFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<el-input
v-model="queryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
</el-col>
</el-row>
</el-col>
<!-- 右边部分展开按钮 -->
<el-col :span="2" style="text-align: right;" v-if="hasMoreFields">
<el-form-item>
<el-button type="text" @click="toggleShowMore">
{{ showMore ? '收起' : '展开更多' }}
</el-button>
</el-form-item>
</el-col>
</el-row>
<!-- 更多搜索条件 -->
<el-collapse-transition>
<div v-show="showMore">
<el-row :gutter="22">
<el-col :span="6" v-for="(field, index) in hiddenFields" :key="index">
<el-form-item :label="field.label" :prop="field.prop">
<el-input
v-model="queryParams[field.prop]"
:placeholder="`请输入${field.label}`"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
</el-col>
</el-row>
</div>
</el-collapse-transition>
<!-- 第二行搜索按钮 -->
<el-row :gutter="20">
<el-col :span="24" style="text-align: right;">
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button
type="primary"
plain
@click="openForm('create')"
v-hasPermi="['pm:subcontractor:create']"
>
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
<el-button
type="success"
plain
@click="handleExport"
:loading="exportLoading"
v-hasPermi="['pm:subcontractor:export']"
>
<Icon icon="ep:download" class="mr-5px" /> 导出
</el-button>
</el-form-item>
</el-col>
</el-row>
</el-form>
<SearchBarEx
:fields="allFields"
:queryParams="queryParams"
@query="handleQuery"
@reset="resetQuery"
@create="openForm('create')"
@export="handleExport"
/>
</ContentWrap>
<!-- 列表 -->
@ -130,7 +57,8 @@
import download from '@/utils/download'
import { SubcontractorApi, SubcontractorVO } from '@/api/pm/subcontractor'
import SubcontractorForm from './SubcontractorForm.vue'
import { ref, onMounted, computed } from 'vue'
import { ref, onMounted, reactive } from 'vue'
import SearchBarEx from "@/components/SearchBar/SearchBarEx.vue";
/** 供应商信息 列表 */
defineOptions({ name: 'Subcontractor' })
@ -149,10 +77,9 @@ const queryParams = reactive({
contacts: undefined,
phone: undefined,
address: undefined,
//
})
const queryFormRef = ref() //
const exportLoading = ref(false) //
const showMore = ref(false) //
const formRef = ref()
//
const allFields = [
@ -161,18 +88,9 @@ const allFields = [
{ label: '联系人', prop: 'contacts' },
{ label: '电话', prop: 'phone' },
{ label: '地址', prop: 'address' },
//
]
//
const visibleFields = computed(() => allFields.slice(0, 4))
const hiddenFields = computed(() => allFields.slice(4))
const hasMoreFields = computed(() => allFields.length > 4)
/** 切换更多搜索项的显示 */
const toggleShowMore = () => {
showMore.value = !showMore.value
}
/** 查询列表 */
const getList = async () => {
loading.value = true
@ -186,19 +104,23 @@ const getList = async () => {
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
const handleQuery = (params: any) => {
Object.assign(queryParams, params)
getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
queryFormRef.value.resetFields()
handleQuery()
for (const key in queryParams) {
if (key !== 'pageNo' && key !== 'pageSize') {
queryParams[key] = undefined
}
}
queryParams.pageNo = 1
getList()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
@ -222,13 +144,9 @@ const handleExport = async () => {
//
await message.exportConfirm()
//
exportLoading.value = true
const data = await SubcontractorApi.exportSubcontractor(queryParams)
download.excel(data, '供应商信息.xls')
} catch {
} finally {
exportLoading.value = false
}
} catch {}
}
/** 初始化 **/
@ -236,7 +154,3 @@ onMounted(() => {
getList()
})
</script>
<style scoped>
/* 自定义样式可以根据需要添加 */
</style>