【升级】新增xnPageSelect分页组件,用户管理均已使用该新组件

pull/125/head
小诺 2023-07-06 01:30:45 +08:00 committed by 俞宝山
parent a200f1899c
commit 44f5d94e59
3 changed files with 319 additions and 153 deletions

View File

@ -0,0 +1,144 @@
<template>
<a-spin size="small" :spinning="spinning">
<a-select
v-model:value="modelValue"
:options="options"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
:placeholder="props.placeholder"
:allow-clear="props.allowClear"
:disabled="props.disabled"
@change="handleChange"
@popupScroll="handlePopupScroll"
/>
</a-spin>
</template>
<script setup name="xnPageSelector">
import { cloneDeep } from 'lodash-es'
import { watch } from 'vue'
const current = ref(1) //
const total = ref(0) //
const initParams = ref({})
const options = ref([])
const spinning = ref(false)
const emit = defineEmits({ change: null, 'update:value': null })
const props = defineProps({
value: {
type: String,
default: () => undefined
},
placeholder: {
type: String,
default: () => '请选择'
},
pageFunction: {
type: Function
},
echoFunction: {
type: Function
},
pageSize: {
type: Number,
default: () => 10
},
allowClear: {
type: Boolean,
default: () => false
},
disabled: {
type: Boolean,
default: () => false
}
})
const modelValue = ref(props.value)
watch(props, (newValue) => {
modelValue.value = newValue.value
})
//
const onPage = (param) => {
if (props.pageFunction) {
initParams.value = param
initParams.value.size = props.pageSize
// API
spinning.value = true
props
.pageFunction(initParams.value)
.then((data) => {
initParams.value.current = data.current
//
total.value = data.total
options.value = data.records
queryEcho()
})
.finally(() => {
spinning.value = false
})
}
}
const queryEcho = () => {
//
if (props.echoFunction) {
nextTick(() => {
if (modelValue.value) {
const entity = options.value.find((f) => f.id === modelValue.value)
if (!entity) {
const param = {
idList: [modelValue.value]
}
props.echoFunction(param).then((data) => {
options.value.push(data[0])
})
}
}
})
}
}
// change
const handleChange = (value, array) => {
modelValue.value = value
// change
emit('change', value, array)
//
emit('update:value', value)
}
//
const handlePopupScroll = (e) => {
const { target } = e
const { scrollTop, scrollHeight, clientHeight } = target
if (scrollTop + 2 + clientHeight >= scrollHeight) {
//
handlePagination()
}
}
//
const handlePagination = () => {
//
if (options.value.length < total.value) {
const param = cloneDeep(initParams.value)
param.current = initParams.value.current + 1
spinning.value = true
props
.pageFunction(param)
.then((data) => {
if (data.records.length > 0) {
options.value = [...cloneDeep(options.value), ...data.records].filter((item, index, self) => {
return (
self.findIndex((f) => {
return f.id === item.id
}) === index
)
})
}
})
.finally(() => {
spinning.value = false
})
}
}
defineExpose({
onPage
})
</script>

View File

@ -76,28 +76,26 @@
</a-col>
<a-col :span="8">
<a-form-item label="选择职位:" name="positionId">
<a-select
<xn-page-select
ref="xnPositionPageSelectRef"
v-model:value="formData.positionId"
:options="positionData"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择职位"
allow-clear
>
</a-select>
:page-function="selectApiFunction.positionSelector"
:echo-function="selectApiFunction.echoPosition"
/>
</a-form-item>
</a-col>
<a-col :span="8">
<a-form-item label="选择主管:" name="directorId">
<a-select
<xn-page-select
ref="xnUserPageSelectRef"
v-model:value="formData.directorId"
:options="directorData"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择主管"
allow-clear
>
</a-select>
:page-function="selectApiFunction.userSelector"
:echo-function="selectApiFunction.echoUser"
/>
</a-form-item>
</a-col>
</a-row>
@ -139,7 +137,6 @@
>
<a-tree-select
v-model:value="positionInfo.orgId"
show-search
style="width: 100%"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
placeholder="请选择机构"
@ -148,7 +145,7 @@
:tree-data="treeData"
:tree-default-expanded-keys="treeDefaultExpandedKeys"
:field-names="{ children: 'children', label: 'name', value: 'id' }"
@select="childOrgSelect(positionInfo, 0)"
@select="childOrgSelect(positionInfo, 0, index)"
></a-tree-select>
</a-form-item>
</a-col>
@ -157,28 +154,26 @@
:name="['positionJson', index, 'positionId']"
:rules="{ required: true, message: '请选择岗位' }"
>
<a-select
<xn-page-select
ref="xnChildPositionPageSelectRef"
v-model:value="positionInfo.positionId"
:options="childPosData(positionInfo.orgId)"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择职位"
placeholder="请选择岗位"
allow-clear
>
</a-select>
:page-function="selectApiFunction.childPositionSelector"
:echo-function="selectApiFunction.echoPosition"
/>
</a-form-item>
</a-col>
<a-col :span="7">
<a-form-item :name="['positionJson', index, 'directorId']">
<a-select
<xn-page-select
ref="xnChildUserPageSelectRef"
v-model:value="positionInfo.directorId"
:options="childUserData(positionInfo.orgId)"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择主管"
allow-clear
>
</a-select>
:page-function="selectApiFunction.childUserSelector"
:echo-function="selectApiFunction.echoUser"
/>
</a-form-item>
</a-col>
<a-col :span="3" style="margin-top: 4px">
@ -331,27 +326,26 @@
import bizUserApi from '@/api/biz/bizUserApi'
import { required } from '@/utils/formRules'
import tool from '@/utils/tool'
import userCenterApi from '@/api/sys/userCenterApi'
//
let visible = $ref(false)
const visible = ref(false)
const formRef = ref()
const activeTabsKey = ref('1')
const emit = defineEmits({ successful: null })
const formLoading = ref(false)
const treeData = ref([])
const treeDefaultExpandedKeys = ref([])
//
let positionData = ref([])
//
let directorData = ref([])
//
let childrenOrgPosArray = ref([])
// selectdom
const xnPositionPageSelectRef = ref()
const xnUserPageSelectRef = ref()
const xnChildPositionPageSelectRef = ref()
const xnChildUserPageSelectRef = ref()
//
let formData = ref({})
const formData = ref({})
//
const onOpen = (record, orgId) => {
visible = true
visible.value = true
formData.value = {
gender: '男',
positionJson: []
@ -392,9 +386,7 @@
const onClose = () => {
treeData.value = []
treeDefaultExpandedKeys.value = []
positionData.value = []
directorData.value = []
visible = false
visible.value = false
}
//
const convertFormData = (record) => {
@ -404,14 +396,18 @@
//
bizUserApi.userDetail(param).then((data) => {
if (data.positionJson) {
const positionJsonLocal = JSON.parse(data.positionJson).map((item) => {
childOrgSelect(item)
return item
})
//
data.positionJson = positionJsonLocal
data.positionJson = JSON.parse(data.positionJson)
}
formData.value = Object.assign(formData.value, data)
//
if (data.positionJson) {
//
data.positionJson.map((item, index) => {
childOrgSelect(item, 1, index)
return item
})
}
selePositionData(formData.value.orgId)
})
}
@ -426,16 +422,14 @@
//
const selePositionData = (orgId, type) => {
if (orgId) {
const param = {
orgId: orgId,
size: -1
const xnPositionPageSelectParam = {
orgId: orgId
}
bizUserApi.userPositionSelector(param).then((data) => {
positionData.value = data.records
})
bizUserApi.userSelector().then((data) => {
directorData.value = data.records
})
xnPositionPageSelectRef.value.onPage(xnPositionPageSelectParam)
const xnUserPageSelectParam = {
orgId: orgId
}
xnUserPageSelectRef.value.onPage(xnUserPageSelectParam)
//
if (type === 0) {
formData.value.positionId = undefined
@ -446,6 +440,40 @@
formData.value.directorId = undefined
}
}
// API
const selectApiFunction = {
positionSelector: (param) => {
return bizUserApi.userPositionSelector(param).then((data) => {
return Promise.resolve(data)
})
},
userSelector: (param) => {
return bizUserApi.userSelector(param).then((data) => {
return Promise.resolve(data)
})
},
childPositionSelector: (param) => {
return bizUserApi.userPositionSelector(param).then((data) => {
return Promise.resolve(data)
})
},
childUserSelector: (param) => {
return bizUserApi.userSelector(param).then((data) => {
return Promise.resolve(data)
})
},
// id
echoPosition: (param) => {
return userCenterApi.userCenterGetPositionListByIdList(param).then((data) => {
return Promise.resolve(data)
})
},
echoUser: (param) => {
return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
return Promise.resolve(data)
})
}
}
//
const addDomains = () => {
if (formData.value.positionJson === null) {
@ -462,11 +490,11 @@
formData.value.positionJson.splice(index, 1)
}
//
const childOrgSelect = async (data, type) => {
const childOrgSelect = async (data, type, index) => {
//
if (type === 0) {
formData.value.positionJson.filter((item) => {
if (item.orgId === data.orgId) {
formData.value.positionJson.filter((item, serial) => {
if (item.orgId === data.orgId && serial === index) {
item.positionId = undefined
item.directorId = undefined
}
@ -475,30 +503,10 @@
const param = {
orgId: data.orgId
}
//
const posList = await bizUserApi.userPositionSelector(param)
//
const userList = await bizUserApi.userSelector(param)
const obj = {
orgId: data.orgId,
posList: posList.records,
userList: userList.records
}
childrenOrgPosArray.value.push(obj)
}
//
const childPosData = (value) => {
const resultData = childrenOrgPosArray.value.filter((item) => item.orgId === value)
if (resultData.length > 0) {
return resultData[0].posList
}
}
//
const childUserData = (value) => {
const resultData = childrenOrgPosArray.value.filter((item) => item.orgId === value)
if (resultData.length > 0) {
return resultData[0].userList
}
nextTick(() => {
xnChildPositionPageSelectRef.value[index].onPage(param)
xnChildUserPageSelectRef.value[index].onPage(param)
})
}
//
const onSubmit = () => {

View File

@ -76,25 +76,25 @@
</a-col>
<a-col :span="8">
<a-form-item label="选择职位:" name="positionId">
<a-select
<xn-page-select
ref="xnPositionPageSelectRef"
v-model:value="formData.positionId"
:options="positionData"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择职位"
allow-clear
:page-function="selectApiFunction.positionSelector"
:echo-function="selectApiFunction.echoPosition"
/>
</a-form-item>
</a-col>
<a-col :span="8">
<a-form-item label="选择主管:" name="directorId">
<a-select
<xn-page-select
ref="xnUserPageSelectRef"
v-model:value="formData.directorId"
:options="directorData"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择主管"
allow-clear
:page-function="selectApiFunction.userSelector"
:echo-function="selectApiFunction.echoUser"
/>
</a-form-item>
</a-col>
@ -137,7 +137,6 @@
>
<a-tree-select
v-model:value="positionInfo.orgId"
show-search
style="width: 100%"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
placeholder="请选择组织"
@ -146,7 +145,7 @@
:tree-data="treeData"
:tree-default-expanded-keys="treeDefaultExpandedKeys"
:field-names="{ children: 'children', label: 'name', value: 'id' }"
@select="childOrgSelect(positionInfo, 0)"
@select="childOrgSelect(positionInfo, 0, index)"
/>
</a-form-item>
</a-col>
@ -155,25 +154,25 @@
:name="['positionJson', index, 'positionId']"
:rules="{ required: true, message: '请选择职位' }"
>
<a-select
<xn-page-select
ref="xnChildPositionPageSelectRef"
v-model:value="positionInfo.positionId"
:options="childPosData(positionInfo.orgId)"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择职位"
allow-clear
:page-function="selectApiFunction.childPositionSelector"
:echo-function="selectApiFunction.echoPosition"
/>
</a-form-item>
</a-col>
<a-col :span="7">
<a-form-item :name="['positionJson', index, 'directorId']">
<a-select
<xn-page-select
ref="xnChildUserPageSelectRef"
v-model:value="positionInfo.directorId"
:options="childUserData(positionInfo.orgId)"
:field-names="{ label: 'name', value: 'id' }"
style="width: 100%"
placeholder="请选择主管"
allow-clear
:page-function="selectApiFunction.childUserSelector"
:echo-function="selectApiFunction.echoUser"
/>
</a-form-item>
</a-col>
@ -327,29 +326,28 @@
<script setup>
import userApi from '@/api/sys/userApi'
import userCenterApi from '@/api/sys/userCenterApi'
import { required } from '@/utils/formRules'
import tool from '@/utils/tool'
//
let visible = $ref(false)
const visible = ref(false)
const formRef = ref()
const activeTabsKey = ref('1')
const emit = defineEmits({ successful: null })
const formLoading = ref(false)
const treeData = ref([])
const treeDefaultExpandedKeys = ref([])
//
let positionData = ref([])
//
let directorData = ref([])
//
let childrenOrgPosArray = ref([])
// selectdom
const xnPositionPageSelectRef = ref()
const xnUserPageSelectRef = ref()
const xnChildPositionPageSelectRef = ref()
const xnChildUserPageSelectRef = ref()
//
let formData = ref({})
const formData = ref({})
//
const onOpen = (record, orgId) => {
visible = true
visible.value = true
formData.value = {
gender: '男',
positionJson: []
@ -390,9 +388,7 @@
const onClose = () => {
treeData.value = []
treeDefaultExpandedKeys.value = []
positionData.value = []
directorData.value = []
visible = false
visible.value = false
}
//
const convertFormData = (record) => {
@ -402,17 +398,22 @@
//
userApi.userDetail(param).then((data) => {
if (data.positionJson) {
const positionJsonLocal = JSON.parse(data.positionJson).map((item) => {
childOrgSelect(item)
return item
})
//
data.positionJson = positionJsonLocal
data.positionJson = JSON.parse(data.positionJson)
}
formData.value = Object.assign(formData.value, data)
//
if (data.positionJson) {
//
data.positionJson.map((item, index) => {
childOrgSelect(item, 1, index)
return item
})
}
selePositionData(formData.value.orgId)
})
}
//
const formRules = {
account: [required('请输入账号')],
@ -424,16 +425,14 @@
//
const selePositionData = (orgId, type) => {
if (orgId) {
const param = {
orgId: orgId,
size: -1
const xnPositionPageSelectParam = {
orgId: orgId
}
userApi.userPositionSelector(param).then((data) => {
positionData.value = data.records
})
userApi.userSelector().then((data) => {
directorData.value = data.records
})
xnPositionPageSelectRef.value.onPage(xnPositionPageSelectParam)
const xnUserPageSelectParam = {
orgId: orgId
}
xnUserPageSelectRef.value.onPage(xnUserPageSelectParam)
//
if (type === 0) {
formData.value.positionId = undefined
@ -444,6 +443,40 @@
formData.value.directorId = undefined
}
}
// API
const selectApiFunction = {
positionSelector: (param) => {
return userApi.userPositionSelector(param).then((data) => {
return Promise.resolve(data)
})
},
userSelector: (param) => {
return userApi.userSelector(param).then((data) => {
return Promise.resolve(data)
})
},
childPositionSelector: (param) => {
return userApi.userPositionSelector(param).then((data) => {
return Promise.resolve(data)
})
},
childUserSelector: (param) => {
return userApi.userSelector(param).then((data) => {
return Promise.resolve(data)
})
},
// id
echoPosition: (param) => {
return userCenterApi.userCenterGetPositionListByIdList(param).then((data) => {
return Promise.resolve(data)
})
},
echoUser: (param) => {
return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
return Promise.resolve(data)
})
}
}
//
const addDomains = () => {
if (formData.value.positionJson === null) {
@ -459,12 +492,13 @@
const delDomains = (index) => {
formData.value.positionJson.splice(index, 1)
}
//
const childOrgSelect = async (data, type) => {
const childOrgSelect = (data, type, index) => {
//
if (type === 0) {
formData.value.positionJson.filter((item) => {
if (item.orgId === data.orgId) {
formData.value.positionJson.filter((item, serial) => {
if (item.orgId === data.orgId && serial === index) {
item.positionId = undefined
item.directorId = undefined
}
@ -473,30 +507,10 @@
const param = {
orgId: data.orgId
}
//
const posList = await userApi.userPositionSelector(param)
//
const userList = await userApi.userSelector(param)
const obj = {
orgId: data.orgId,
posList: posList.records,
userList: userList.records
}
childrenOrgPosArray.value.push(obj)
}
//
const childPosData = (value) => {
const resultData = childrenOrgPosArray.value.filter((item) => item.orgId === value)
if (resultData.length > 0) {
return resultData[0].posList
}
}
//
const childUserData = (value) => {
const resultData = childrenOrgPosArray.value.filter((item) => item.orgId === value)
if (resultData.length > 0) {
return resultData[0].userList
}
nextTick(() => {
xnChildPositionPageSelectRef.value[index].onPage(param)
xnChildUserPageSelectRef.value[index].onPage(param)
})
}
//
const onSubmit = () => {