记录vxe-table合并单元格
<script setup lang='ts' name="policy-area">
import { addPolicyArea, deletePolicyArea, getPolicyAreaDetail, queryAreaPolicyList, queryConditionList, queryMaterialList, queryPolicyKindList, queryPolicyResultList, queryPolicyResultValueList, queryRegionList, updatePolicyArea, updatePolicyAreaStatus } from '@fl/api/xf-api/xf-manage'
import { ElMessage, ElMessageBox } from 'element-plus'
import { cloneDeep, result } from 'lodash-es'
import { onMounted, onUnmounted, reactive, ref } from 'vue'
const stateOptions = ref([
{
label: '全部',
value: 'all',
},
{
label: '启用',
value: 1,
},
{
label: '禁用',
value: 0,
},
])
const tablePage = reactive({
currentPage: 1,
pageSize: 10,
totalResult: 0,
})
function handlePageChange({ currentPage, pageSize }) {
tablePage.currentPage = currentPage
tablePage.pageSize = pageSize
queryTableData()
}
const loading = ref(true)
const policyDialog = ref(null)
const dialogVisible = ref(false)
const dialogTitle = ref('添加政策')
const addFormRef = ref(null)
const addForm = reactive({
areaId: '',
areaName: '',
materials: [],
policyConfig: [{}],
policyId: '',
policyName: '',
policyTime: [],
})
type queryForm = {
areaId?: number | string
areaName?: string
policyName?: string
status?: number | string
}
const form = reactive<queryForm>({
areaName: '',
policyName: '',
status: 'all',
})
function resetForm() {
form.areaName = '全武汉'
form.areaId = 'wuhan'
form.policyName = ''
form.status = 'all'
tablePage.currentPage = 1
queryTableData()
}
const policyTableData = ref([])
const tagNameOptions = ref([])
function statusChange() {
tablePage.currentPage = 1
queryTableData()
}
async function queryTableData() {
tagNameOptions.value = []
const { code, data, msg } = await queryAreaPolicyList({
areaName: form.areaName != '全武汉' ? form.areaName : undefined,
delFlag: form.status !== 'all' ? form.status : undefined,
pageNum: tablePage.currentPage,
pageSize: tablePage.pageSize,
policyName: form.policyName ? form.policyName : undefined,
})
if (code === 200) {
const records = data.records || []
const initList = []
const rowArray = []
let initIndex = 0
let mergeList = []
mergeCells.value = []
records.forEach((item, index) => {
if (item.options) {
item.options.forEach((option) => {
const it = {
...option,
...item,
}
initList.push(it)
})
if (item.options.length > 1 && index < 10) {
rowArray.push([initIndex, item.options.length])
initIndex = initIndex + item.options.length
}
else {
{
initIndex = initIndex + 1
}
}
}
})
rowArray.forEach((r) => {
const tempArray = [
{ col: 0, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 1, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 2, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 8, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 9, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 10, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 11, colspan: 1, row: r[0], rowspan: r[1] },
{ col: 12, colspan: 1, row: r[0], rowspan: r[1] },
]
mergeList = mergeList.concat(tempArray)
})
mergeCells.value = mergeList
policyTableData.value = []
policyTableData.value = initList
tablePage.totalResult = data.total
}
else {
ElMessage.error(msg)
}
loading.value = false
}
const mergeCells = ref(
[
/* { row: 4, col: 0, rowspan: 5, colspan: 1 },
{ row: 4, col: 1, rowspan: 5, colspan: 1 },
{ row: 4, col: 2, rowspan: 5, colspan: 1 },
{ row: 4, col: 8, rowspan: 5, colspan: 1 },
{ row: 4, col: 9, rowspan: 5, colspan: 1 },
{ row: 4, col: 10, rowspan: 5, colspan: 1 },
{ row: 4, col: 11, rowspan: 5, colspan: 1 },
{ row: 4, col: 12, rowspan: 5, colspan: 1 },
{ row: 11, col: 0, rowspan: 2, colspan: 1 },
{ row: 11, col: 1, rowspan: 2, colspan: 1 },
{ row: 11, col: 2, rowspan: 2, colspan: 1 },
{ row: 11, col: 8, rowspan: 2, colspan: 1 },
{ row: 11, col: 9, rowspan: 2, colspan: 1 },
{ row: 11, col: 10, rowspan: 2, colspan: 1 },
{ row: 11, col: 11, rowspan: 2, colspan: 1 },
{ row: 11, col: 12, rowspan: 2, colspan: 1 } */
],
)
function policyNameChange(val) {
queryTableData()
}
function remoteQueryProject(queryString, cb) {
const results = queryString
? policyOptions.value.filter((item) => {
return item.kindName.toLowerCase().includes(queryString.toLowerCase())
})
: policyOptions.value
cb(results)
// return results
}
const xTable1 = ref(null)
const isViewFlag = ref(false)
const childCols = [
{ field: 'conditionName', minWidth: 60, title: '选项' },
{ field: 'resultName', title: '优惠类型' },
{ field: 'resultNum', title: '优惠数值' },
{ field: 'optionNotes', minWidth: 60, title: '备注' },
{ field: 'subsidyCap', title: '补贴上限(元)' },
]
// 区域信息获取
const districtTypes = ref([{ id: 'wuhan', name: '全武汉' }])
function getRegions() {
queryRegionList().then((res: any) => {
if (res.code === 200) {
if (res.data) {
districtTypes.value = districtTypes.value.concat(res.data)
}
}
}).catch((error) => {
ElMessage.error(error.msg)
})
}
function changeArea(val) {
if (val) {
const areaItem = districtTypes.value.find((item) => {
return item?.id === val
})
addForm.areaName = areaItem.name
}
}
function changeResult(val, index) {
addForm.policyConfig[index].resultNumId = ''
policyResultValueOptions.value[index] = []
if (val) {
const choseItem = policyResultOptions.value.find((item) => {
return item?.id === val
})
addForm.policyConfig[index].resultName = choseItem.resultContent
queryPolicyResultValueData(choseItem.id, index)
}
}
function changeResultNum(val, index) {
if (val) {
const choseItem = policyResultValueOptions.value[index].find((item) => {
return item?.id === val
})
addForm.policyConfig[index].resultNum = choseItem.resultNum
}
}
function changeCondition(val, index) {
if (val) {
const it = conditionOptions.value.find((item) => {
return item?.id === val
})
addForm.policyConfig[index].conditionName = it.conditionContent
}
}
function changePolicy(val) {
if (val) {
const it = policyOptions.value.find((item) => {
return item?.id === val
})
addForm.policyName = it.kindName
addForm.policyConfig.map((item) => {
item.conditionId = ''
item.conditionName = ''
item.resultId = ''
item.resultNumId = ''
return item
})
}
else {
conditionOptions.value = []
addForm.policyConfig.map((item) => {
item.conditionId = ''
item.conditionName = ''
item.resultId = ''
item.resultNumId = ''
return item
})
}
queryPolicyConditionData()
queryPolicyResultData()
}
function deleteRow(row) {
ElMessageBox.confirm(
`是否确认删除此记录`,
'系统提示',
{
cancelButtonText: '取消',
confirmButtonText: '确认',
type: 'warning',
},
)
.then(() => {
deletePolicyArea(JSON.stringify([row.id])).then((res) => {
if (res.code === 200) {
ElMessage.success('操作成功')
setTimeout(() => {
queryTableData()
}, 500)
}
else {
ElMessage.error(res.msg)
}
})
doSearch()
})
}
const materialsOptions = ref([])
function changeMaterials(val) {
}
function addRow() {
dialogTitle.value = '添加政策'
dialogVisible.value = true
queryPolicyOptionsData()
queryPolicyMaterialData()
}
function editRow(oldRow, type) {
if (type === 1) {
isViewFlag.value = true
dialogTitle.value = '查看政策'
}
else {
dialogTitle.value = '修改政策'
isViewFlag.value = false
}
const row = cloneDeep(oldRow)
addForm.id = row.id
addForm.areaId = row.areaId
addForm.areaName = row.areaName
addForm.areaName = row.areaName
addForm.policyId = row.policyId
addForm.policyName = row.policyName
addForm.policyTime = [row.policyStartTime, row.policyEndTime]
const materials = []
row.materials.forEach((item) => {
materials.push(item.id)
})
addForm.materials = materials
addForm.policyConfig = row.options
if (addForm.policyConfig) {
addForm.policyConfig.forEach((item, index) => {
queryPolicyResultValueData(item.resultId, index)
})
}
dialogVisible.value = true
queryPolicyOptionsData()
queryPolicyMaterialData()
queryPolicyConditionData()
queryPolicyResultData()
}
function closeDialog() {
isViewFlag.value = false
resetAddForm()
}
function resetAddForm() {
addForm.id = ''
addForm.policyId = ''
addForm.policyName = ''
addForm.areaId = ''
addForm.areaName = ''
addForm.materials = []
addForm.policyConfig = [{}]
addForm.policyTime = []
dialogVisible.value = false
conditionOptions.value = []
policyResultOptions.value = []
policyResultValueOptions.value = []
}
const policyOptions = ref([])
const formateType = ref<string>('YYYY-MM-DD')
function addPolicyItem() {
const item = {}
addForm?.policyConfig?.push(item)
}
function removePolicyItem(index) {
if (addForm?.policyConfig?.length > 1) {
addForm?.policyConfig?.splice(index, 1)
}
else {
ElMessage.warning('添加政策时,至少保留一项')
}
}
async function queryPolicyMaterialData() {
const { code, data, msg } = await queryMaterialList({
pageNum: 1,
pageSize: 100,
})
if (code === 200) {
const records = data.records || []
materialsOptions.value = records
}
else {
ElMessage.error(msg)
}
loading.value = false
}
async function queryPolicyOptionsData() {
const { code, data, msg } = await queryPolicyKindList({
pageNum: 1,
pageSize: 100,
})
if (code === 200) {
const records = data.records || []
policyOptions.value = records
}
else {
ElMessage.error(msg)
}
loading.value = false
}
const conditionOptions = ref([])
async function queryPolicyConditionData() {
const { code, data, msg } = await queryConditionList({
pageNum: 1,
pageSize: 100,
policyId: addForm.policyId,
})
if (code === 200) {
const records = data.records || []
conditionOptions.value = records
}
else {
ElMessage.error(msg)
}
loading.value = false
}
const policyResultOptions = ref([])
async function queryPolicyResultData() {
const { code, data, msg } = await queryPolicyResultList({
pageNum: 1,
pageSize: 100,
policyId: addForm.policyId,
})
if (code === 200) {
const records = data.records || []
policyResultOptions.value = records
}
else {
ElMessage.error(msg)
}
loading.value = false
}
const policyResultValueOptions = ref([])
async function queryPolicyResultValueData(resultId, index) {
const { code, data, msg } = await queryPolicyResultValueList({
pageNum: 1,
pageSize: 100,
policyId: addForm.policyId,
resultId,
})
if (code === 200) {
const records = data.records || []
policyResultValueOptions.value[index] = records
}
else {
ElMessage.error(msg)
}
loading.value = false
}
async function switchState(val, row) {
if (row.id === null)
return false
const result = await updatePolicyAreaStatus({ delFlag: row.delFlag, id: row.id })
if (result.code === 200) {
if (row.delFlag === '1') {
ElMessage.success('政策选项已启用')
}
else {
ElMessage.success('政策选项已禁用')
}
queryTableData()
}
else {
ElMessage.warning(result.msg)
}
}
async function savePolicyArea() {
addFormRef.value.validate(async (valid, fields) => {
if (valid) {
let isPass = true
if (addForm.policyConfig) {
addForm.policyConfig.forEach((policyItem) => {
if (!policyItem.conditionId || !policyItem.resultId || !policyItem.resultNumId) {
isPass = false
return false
}
})
}
if (!isPass) {
ElMessage.warning('请完善条件必填项信息')
return false
}
let materials = []
if (addForm.materials) {
materials = materialsOptions.value.filter((item) => {
if (addForm.materials.includes(item.id)) {
return item
}
})
}
const params = {
areaId: addForm.areaId,
areaName: addForm.areaName,
id: addForm.id ? addForm.id : undefined,
materials,
options: addForm.policyConfig,
policyEndTime: addForm.policyTime[1],
policyId: addForm.policyId,
policyName: addForm.policyName,
policyStartTime: addForm.policyTime[0],
}
let result = null
if (addForm.id) {
result = await updatePolicyArea(params)
}
else {
result = await addPolicyArea(params)
}
if (result.code === 200) {
closeDialog()
ElMessage.success('操作成功')
queryTableData()
}
else {
ElMessage.warning(result.msg)
}
}
else {
// ElMessage.warning('请填写必填项')
return false
}
})
}
const rules = reactive({
areaId: [{
message: '请选择区域',
required: true,
trigger: 'change',
}],
materials: [
{ message: '请选择所需材料', required: true, trigger: 'change' },
],
policyConfig: [{
message: '请选择条件',
required: true,
trigger: 'change',
}],
policyId: [
{ message: '请选择政策种类', required: true, trigger: 'change' },
],
policyTime: [
{ message: '请选择政策生效日期', required: true, trigger: 'change' },
],
})
function doSearch() {
queryTableData()
}
onMounted(() => {
queryTableData()
queryPolicyOptionsData()
getRegions()
})
onUnmounted(() => {
policyTableData.value = []
})
</script>
<template>
<el-card style="padding-bottom:60px">
<div style="height: 560px;padding-bottom:20px">
<el-form :inline="true"
:model="form"
label-width="100"
class="query_form"
>
<el-row>
<el-col :span="6">
<el-form-item label="政策名称"
prop="policyName"
>
<el-autocomplete v-model="form.policyName"
clearable
:fetch-suggestions="remoteQueryProject"
class="inline-input w-50"
value-key="kindName"
placeholder="请输入政策名称"
@select="policyNameChange"
@clear="policyNameChange"
/>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="区域">
<el-row style="width:100% !important;">
<el-col>
<el-select v-model="form.areaName"
class="m-2"
placeholder="请选择区域"
@change="policyNameChange"
>
<el-option v-for="item in districtTypes"
:key="item.id"
:value="item.name"
:label="item.name"
/>
</el-select>
</el-col>
</el-row>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="状态"
prop="status"
>
<el-radio-group v-model="form.status"
@change="statusChange"
>
<el-radio v-for="item in stateOptions"
:key="item.value"
:label="item.value"
>
{{ item.label }}
</el-radio>
</el-radio-group>
<!-- <el-select v-model="form.state"
placeholder="请选择状态"
clearable
>
<el-option v-for="item in stateOptions"
:key="item.value"
:value="item.value"
:label="item.label"
/>
</el-select> -->
</el-form-item>
</el-col>
<el-col :span="4"
style="text-align: right;"
>
<el-button type="primary"
@click="doSearch()"
>
搜索
</el-button>
<el-button @click="resetForm">
重置
</el-button>
</el-col>
</el-row>
</el-form>
<el-row style="width:100%;justify-content: start;">
<el-button type="primary"
class="mb-10"
@click="addRow"
>
+ 新建
</el-button>
</el-row>
<el-scrollbar>
<vxe-table ref="xTable1"
:data="policyTableData"
:height="480"
:show-header="true"
:merge-cells="mergeCells"
stripe
:border="true"
:loading="loading"
:row-config="{ isHover: true }"
:tooltip-config="{ zIndex: 9999 }"
>
<!-- title="全选"
<vxe-column type="id"
field="id"
title="政策ID"
width="80"
show-overflow="tooltip"
/> -->
<vxe-column type="policyName"
field="policyName"
title="政策名称"
min-width="160"
show-overflow="tooltip"
/>
<vxe-column type="areaName"
field="areaName"
title="区域"
min-width="100"
show-overflow="tooltip"
/>
<vxe-column field="conditionName"
title="选项"
min-width="180"
align="center"
show-overflow="tooltip"
/>
<vxe-column field="resultName"
title="优惠类型"
min-width="100"
align="center"
show-overflow="tooltip"
/>
<vxe-column field="policyAreaDiscount"
title="优惠数值"
min-width="150"
align="center"
show-overflow="tooltip"
/>
<vxe-column field="optionNotes"
title="备注"
min-width="100"
align="center"
show-overflow="tooltip"
/>
<vxe-column field="subsidyCap"
title="补贴上限(元)"
min-width="120"
align="center"
show-overflow="tooltip"
/>
<vxe-column type="applyHouseNum"
field="applyHouseNum"
title="适用楼盘数量"
width="120"
align="center"
show-overflow="tooltip"
/>
<vxe-column title="适用楼盘"
show-overflow="tooltip"
min-width="200"
align="center"
>
<template #default="{ row }">
<span v-for="it in row.applyHouse"
:key="it.id"
>{{ it.xmmc }},</span>
</template>
</vxe-column>
<vxe-column type="delFlag"
field="delFlag"
width="100"
title="状态"
>
<template #default="{ row }">
<el-switch v-model="row.delFlag"
active-value="1"
inactive-value="0"
@change="switchState($event, row)"
/>
</template>
</vxe-column>
<vxe-column title="政策生效日期"
width="200"
>
<template #default="{ row }">
{{ `${row.policyStartTime} 至 ${row.policyEndTime}` }}
</template>
</vxe-column>
<vxe-column title="操作"
fixed="right"
width="160"
>
<template #default="{ row }">
<vxe-button mode="text"
@click="editRow(row, 1)"
>
查看
</vxe-button>
<vxe-button mode="text"
style="color:#da0004"
@click="editRow(row, 2)"
>
修改
</vxe-button>
<!-- <vxe-button mode="text"
@click="deleteRow(row)"
>
删除
</vxe-button> -->
</template>
</vxe-column>
</vxe-table>
<vxe-pager :loading="loading"
:current-page="tablePage.currentPage"
:page-size="tablePage.pageSize"
:total="tablePage.totalResult"
:layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
@page-change="handlePageChange"
/>
</el-scrollbar>
</div>
<el-dialog ref="policyDialog"
v-model="dialogVisible"
:title="dialogTitle"
class="policy-dialog"
width="80%"
draggable
:destroy-on-close="true"
@close="closeDialog"
>
<el-form ref="addFormRef"
:model="addForm"
:rules="rules"
label-width="110px"
:validate-on-rule-change="false"
>
<el-form-item label="政策选择"
prop="policyId"
>
<el-col :span="12">
<el-select v-model="addForm.policyId"
:disabled="isViewFlag"
placeholder="请选择"
clearable
@change="changePolicy($event, index)"
>
<el-option v-for="item in policyOptions"
:key="item.id"
:label="item.kindName"
:value="item.id"
/>
</el-select>
</el-col>
</el-form-item>
<el-form-item label="区域"
prop="areaId"
>
<el-col :span="12">
<el-select v-model="addForm.areaId"
:disabled="isViewFlag"
placeholder="请选择区域"
clearable
@change="changeArea($event, index)"
>
<el-option v-for="item in districtTypes"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-col>
</el-form-item>
<el-form-item label="政策有效日期"
prop="policyTime"
>
<el-col :span="12">
<el-date-picker v-model="addForm.policyTime"
type="daterange"
start-placeholder="开始日期"
end-placeholder="结束日期"
:format="formateType"
:value-format="formateType"
placeholder="选择开始日期"
:disabled="isViewFlag"
/>
</el-col>
</el-form-item>
<el-form-item label="所需材料"
prop="materials"
>
<el-col :span="12">
<el-select v-model="addForm.materials"
:disabled="isViewFlag"
multiple
placeholder="请选择所需材料(可多选)"
clearable
@change="changeMaterials($event, index)"
>
<el-option v-for="item in materialsOptions"
:key="item.id"
:label="item.materialName"
:value="item.id"
/>
</el-select>
</el-col>
</el-form-item>
<el-form-item label="条件"
prop="policyConfig"
>
<template v-if="addForm.policyConfig?.length > 0">
<el-row v-for="(it, index) in addForm.policyConfig"
:key="index"
mb-8
w-full
>
<el-col :span="3">
<el-select v-model="addForm.policyConfig[index].conditionId"
placeholder="请选择"
:disabled="isViewFlag"
clearable
@change="changeCondition($event, index)"
>
<el-option v-for="item in conditionOptions"
:key="item.id"
:label="item.conditionContent"
:value="item.id"
/>
</el-select>
</el-col>
<el-col :span="4">
<el-select v-model="addForm.policyConfig[index].resultId"
placeholder="请选择"
:disabled="isViewFlag"
clearable
@change="changeResult($event, index)"
>
<el-option v-for="item in policyResultOptions"
:key="item.id"
:label="item.resultContent"
:value="item.id"
/>
</el-select>
</el-col>
<el-col :span="4">
<el-select v-model="addForm.policyConfig[index].resultNumId"
placeholder="请选择"
:disabled="isViewFlag"
clearable
@change="changeResultNum($event, index)"
>
<el-option v-for="item in policyResultValueOptions[index]"
:key="item.id"
:label="item.showValue"
:value="item.id"
/>
</el-select>
</el-col>
<el-col :span="5">
<el-input v-model="addForm.policyConfig[index].optionNotes"
:disabled="isViewFlag"
placeholder="请输入备注"
clearable
/>
</el-col>
<el-col :span="6">
<el-input-number v-model="addForm.policyConfig[index].subsidyCap"
:disabled="isViewFlag"
style="width:100%"
:controls="false"
placeholder="补贴上限数额(单位:元),请输入数字)"
clearable
/>
</el-col>
<el-col v-if="!isViewFlag"
:span="2"
class="operate-icon"
>
<el-icon @click="addPolicyItem()">
<CirclePlusFilled />
</el-icon>
<el-icon @click="removePolicyItem(index)">
<RemoveFilled />
</el-icon>
</el-col>
</el-row>
</template>
</el-form-item>
</el-form>
<template v-if="!isViewFlag"
#footer
>
<span class="dialog-footer">
<el-button @click="closeDialog">取消</el-button>
<el-button type="primary"
@click="savePolicyArea"
>
确定
</el-button>
</span>
</template>
</el-dialog>
</el-card>
</template>
<style lang="scss" scoped>
.query_form {
:deep(.el-form-item) {
width: 100%;
}
:deep(.el-form-item__content) {
width: 100%;
}
:deep(.el-radio) {
margin-right: 12px;
}
}
:deep() {
--vxe-primary-color: #da0004;
--vxe-loading-color: #da0004;
}
:deep(.vxe-table--render-default .vxe-body--row.row--stripe) {
background-color: #fff !important;
}
.operate-icon {
display: flex;
align-items: center;
.el-icon {
font-size: 24px;
margin-left: 10px;
cursor: pointer;
color: #da0004;
}
}
:deep(.vxe-pager--btn-wrapper .vxe-pager--num-btn.is--active) {
color: #fff;
}
</style>