Commit 593dbda9 by xinla

扩展SelectWeUser

1 parent fda702be
<script> <script>
import { getDeptTree, getDeptUser } from '@/api/organization' import { getDeptTree, getDeptUser } from '@/api/organization'
export default { export default {
name: 'SelectWeUser', name: 'SelectWeUser',
components: {}, components: {},
props: { props: {
// 添加标签显隐 // 添加标签显隐
visible: { visible: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
title: { title: {
type: String, type: String,
default: '组织架构' default: '组织架构',
}, },
// 是否 只选择叶子节点(人员节点)/禁止选择父节点(部门节点) // 是否 只选择叶子节点(人员节点)/禁止选择父节点(部门节点)
isOnlyLeaf: { isOnlyLeaf: {
type: Boolean, type: Boolean,
default: true default: true,
}, },
// 是否单选 // 是否单选
isSigleSelect: { isSigleSelect: {
type: Boolean, type: Boolean,
default: false default: false,
},
// 默认选中的节点
defaultValues: {
type: Array,
default: () => [],
// 必须含有userId和name属性
validator(val) {
return val.every((e) => e.userId && e.name)
}, },
// 默认选中的节点 },
defaultValues: { // 禁止选中的节点
type: Array, disabledValues: {
default: () => [], type: Array,
// 必须含有userId和name属性 default: () => [],
validator(val) { },
return val.every((e) => e.userId && e.name) destroyOnClose: Boolean,
},
data() {
return {
treeData: [],
userList: [],
defaultKeys: [],
deptId: '', //根部门Id
keywords: '',
searchResult: [],
searchState: false,
checked: [],
}
},
watch: {
defaultValues: {
handler(value) {
const checkedUserList = []
if (!Array.isArray(value) || value.length == 0) {
this.userList = []
} else {
value.forEach((i) => {
const id = i.userId || i.id
if (id) {
checkedUserList.push(i)
}
})
this.userList = checkedUserList
} }
}, },
// 禁止选中的节点 immediate: true,
disabledValues: {
type: Array,
default: () => []
},
destroyOnClose: Boolean
}, },
data() { Pvisible(val) {
return { val && this.$refs.tree && this.$refs.tree.setCheckedKeys(this.defaultValues.map((e) => e.userId || e.id))
treeData: [], if (!this.$refs.tree) {
userList: [], setTimeout(() => {
defaultKeys: [], this.$refs.tree && this.$refs.tree.setCheckedKeys(this.defaultValues.map((e) => e.userId || e.id))
deptId: '', //根部门Id }, 300)
keywords: '',
searchResult: [],
searchState: false,
checked: []
} }
}, },
watch: { },
defaultValues: { computed: {
handler(value) { Pvisible: {
const checkedUserList = [] get() {
if (!Array.isArray(value) || value.length == 0) { return this.visible
this.userList = [] },
} else { set(val) {
value.forEach((i) => { this.$emit('update:visible', val)
const id = i.userId || i.id
if (id) {
checkedUserList.push(i)
}
})
this.userList = checkedUserList
}
},
immediate: true
}, },
Pvisible(val) {
val && this.$refs.tree && this.$refs.tree.setCheckedKeys(this.defaultValues.map((e) => e.userId || e.id))
if (!this.$refs.tree) {
setTimeout(() => {
this.$refs.tree && this.$refs.tree.setCheckedKeys(this.defaultValues.map((e) => e.userId || e.id))
}, 300)
}
}
}, },
computed: { defaultProps() {
Pvisible: { let that = this
get() { return {
return this.visible label: 'name',
children: 'children',
disabled(data, node) {
return (that.isOnlyLeaf && data.isParty) || that.disabledValues.some((e) => e == (data.userId || data.id))
},
isLeaf(data, node) {
return !data.id
}, },
set(val) {
this.$emit('update:visible', val)
}
},
defaultProps() {
let that = this
return {
label: 'name',
children: 'children',
disabled(data, node) {
return (that.isOnlyLeaf && data.isParty) || that.disabledValues.some((e) => e == (data.userId || data.id))
},
isLeaf(data, node) {
return !data.id
}
}
} }
}, },
created() {}, },
mounted() {}, created() {},
methods: { mounted() {},
setChange(e) { methods: {
if (!e) { setChange(e) {
this.searchState = false if (!e) {
this.$refs.tree.setCheckedKeys(this.userList.map((e) => e.userId || e.id)) this.searchState = false
this.$refs.tree.setCheckedKeys(this.userList.map((e) => e.userId || e.id))
}
},
setSearch() {
if (!this.keywords) return
this.searchResult = []
this.searchState = true
this.checked = this.userList.map((dd) => {
return dd.userId
})
getDeptUser({
deptId: this.deptId,
userName: this.keywords,
}).then((res) => {
this.searchResult = this.handleName(res.rows)
})
},
setSelectChange(e, index) {
if (e) {
if (this.isSigleSelect) {
this.userList[0] = this.searchResult[index]
} else {
this.userList.push(this.searchResult[index])
} }
},
setSearch() {
if (!this.keywords) return
this.searchResult = []
this.searchState = true
this.checked = this.userList.map((dd) => { this.checked = this.userList.map((dd) => {
return dd.userId return dd.userId
}) })
getDeptUser({ this.$forceUpdate()
deptId: this.deptId, } else {
userName: this.keywords this.cancle(this.searchResult[index].userId)
}).then((res) => { }
this.searchResult = this.handleName(res.rows) setTimeout(() => {
this.keywords = ''
this.setChange()
}, 500)
},
treeFormat(list) {
let dealOptions = []
list.forEach((one) => {
let findIndex = list.findIndex((item) => {
return item.id === one.parentId
}) })
}, if ((!one.parentId && one.parentId !== 0 && one.parentId !== false) || findIndex === -1) {
setSelectChange(e, index) { dealOptions.push(one)
if (e) {
if (this.isSigleSelect) {
this.userList[0] = this.searchResult[index]
} else {
this.userList.push(this.searchResult[index])
}
this.checked = this.userList.map((dd) => {
return dd.userId
})
this.$forceUpdate()
} else { } else {
this.cancle(this.searchResult[index].userId) if (!list[findIndex].children) {
} list[findIndex].children = []
setTimeout(() => { list[findIndex].children.push(one)
this.keywords = ''
this.setChange()
}, 500)
},
treeFormat(list) {
let dealOptions = []
list.forEach((one) => {
let findIndex = list.findIndex((item) => {
return item.id === one.parentId
})
if ((!one.parentId && one.parentId !== 0 && one.parentId !== false) || findIndex === -1) {
dealOptions.push(one)
} else { } else {
if (!list[findIndex].children) { list[findIndex].children.push(one)
list[findIndex].children = []
list[findIndex].children.push(one)
} else {
list[findIndex].children.push(one)
}
} }
})
return dealOptions
},
loadNode(node, resolve) {
if (node.level === 0) {
if (!Array.isArray(this.defaultValues) || this.defaultValues.length == 0) {
this.userList = []
}
getDeptTree().then(({ data }) => {
let _data = this.handleTree(data)
let value = this.treeFormat(_data)
if (value.length) {
this.deptId = value[0].deptId
}
resolve(value)
})
} else {
getDeptUser({ deptId: node.data.id }).then(({ rows }) => {
let arr = this.handleName(rows)
node.data.children && arr.push(...node.data.children)
resolve(arr)
})
} }
}, })
handleTree(data) { return dealOptions
return data.map((i) => { },
if (i.deptId) { loadNode(node, resolve) {
i.userId = i.deptId if (node.level === 0) {
i.name = i.deptName if (!Array.isArray(this.defaultValues) || this.defaultValues.length == 0) {
i.id = i.deptId this.userList = []
i.isParty = true }
getDeptTree().then(({ data }) => {
let _data = this.handleTree(data)
let value = this.treeFormat(_data)
if (value.length) {
this.deptId = value[0].deptId
} }
return i resolve(value)
}) })
}, } else {
handleName(data) { getDeptUser({ deptId: node.data.id }).then(({ rows }) => {
return data.map((i) => { let arr = this.handleName(rows)
if (i.userName) { node.data.children && arr.push(...node.data.children)
i.userId = i.weUserId resolve(arr)
i.name = i.userName
}
return i
}) })
}, }
// 选择变化 },
handleCheckChange(data, checked, indeterminate) { handleTree(data) {
if (checked) { return data.map((i) => {
if (this.isSigleSelect) { if (i.deptId) {
// 单选情况 i.userId = i.deptId
this.$refs.tree.setCheckedKeys([data.userId]) i.name = i.deptName
} i.id = i.deptId
if (this.isOnlyLeaf) { i.isParty = true
if (data.userId && !data.isParty) { }
this.userList.push(data) return i
} })
} else { },
const isExist = this.userList.findIndex((i) => i.userId === data.userId) > -1 handleName(data) {
!isExist && this.userList.push(data) return data.map((i) => {
if (i.userName) {
i.userId = i.weUserId
i.name = i.userName
}
return i
})
},
// 选择变化
handleCheckChange(data, checked, indeterminate) {
if (checked) {
if (this.isSigleSelect) {
// 单选情况
this.$refs.tree.setCheckedKeys([data.userId])
}
if (this.isOnlyLeaf) {
if (data.userId && !data.isParty) {
this.userList.push(data)
} }
} else { } else {
let index = this.userList.findIndex((i) => i.userId === data.userId) const isExist = this.userList.findIndex((i) => i.userId === data.userId) > -1
index > -1 && this.userList.splice(index, 1) !isExist && this.userList.push(data)
} }
this.userList = this.unique(this.userList) } else {
}, let index = this.userList.findIndex((i) => i.userId === data.userId)
unique(arr) { index > -1 && this.userList.splice(index, 1)
for (var i = 0; i < arr.length; i++) { }
for (var j = i + 1; j < arr.length; j++) { this.userList = this.unique(this.userList)
if (arr[i].userId.split('_')[0] == arr[j].userId.split('_')[0]) { },
arr.splice(j, 1) unique(arr) {
j-- for (var i = 0; i < arr.length; i++) {
} for (var j = i + 1; j < arr.length; j++) {
if (arr[i].userId.split('_')[0] == arr[j].userId.split('_')[0]) {
arr.splice(j, 1)
j--
} }
} }
return arr
},
// 确 定
submit() {
this.Pvisible = false
this.$emit('success', [...this.userList])
},
// 取消选择
cancle(key) {
this.$refs.tree.setChecked(key, false)
let index = this.userList.findIndex((i) => i.userId === key)
index > -1 && this.userList.splice(index, 1)
let order = this.defaultValues.findIndex((i) => i.userId === key)
order > -1 && this.defaultValues.slice().splice(order, 1)
} }
} return arr
} },
// 确 定
submit() {
this.Pvisible = false
this.$emit('success', [...this.userList])
},
// 取消选择
cancle(key) {
this.$refs.tree.setChecked(key, false)
let index = this.userList.findIndex((i) => i.userId === key)
index > -1 && this.userList.splice(index, 1)
let order = this.defaultValues.findIndex((i) => i.userId === key)
order > -1 && this.defaultValues.slice().splice(order, 1)
},
},
}
</script> </script>
<template> <template>
<el-dialog :title="title" :visible.sync="Pvisible" :close-on-click-modal="false" :destroy-on-close="destroyOnClose"> <el-dialog
v-bind="$attrs"
:title="title"
:visible.sync="Pvisible"
:close-on-click-modal="false"
:destroy-on-close="destroyOnClose">
<el-row :gutter="20" class="content"> <el-row :gutter="20" class="content">
<el-col :span="12" :xs="24"> <el-col :span="12" :xs="24">
<div class="search-input"> <div class="search-input">
...@@ -262,8 +267,7 @@ ...@@ -262,8 +267,7 @@
@keyup.enter.native="setSearch" @keyup.enter.native="setSearch"
placeholder="请输入员工名称" placeholder="请输入员工名称"
clearable clearable
@change="setChange" @change="setChange">
>
<el-button slot="append" icon="el-icon-search" @click="setSearch"></el-button> <el-button slot="append" icon="el-icon-search" @click="setSearch"></el-button>
</el-input> </el-input>
</div> </div>
...@@ -291,8 +295,7 @@ ...@@ -291,8 +295,7 @@
:load="loadNode" :load="loadNode"
:props="defaultProps" :props="defaultProps"
:check-strictly="isOnlyLeaf" :check-strictly="isOnlyLeaf"
@check-change="handleCheckChange" @check-change="handleCheckChange"></el-tree>
></el-tree>
</div> </div>
</transition> </transition>
</el-col> </el-col>
...@@ -313,32 +316,32 @@ ...@@ -313,32 +316,32 @@
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.content { .content {
min-height: 400px; min-height: 400px;
} }
.search-input { .search-input {
margin-top: 5px; margin-top: 5px;
margin-bottom: 10px; margin-bottom: 10px;
} }
.user-list { .user-list {
.el-row { .el-row {
line-height: 26px; line-height: 26px;
}
}
.search-list {
margin-top: 20px;
padding: 0 10px;
}
.fade-enter-active {
transition: opacity 0.5s;
}
.fade-enter {
opacity: 0;
}
.fade-leave-active {
transition: opacity 0.5s;
}
.fade-leave-to {
opacity: 0;
} }
}
.search-list {
margin-top: 20px;
padding: 0 10px;
}
.fade-enter-active {
transition: opacity 0.5s;
}
.fade-enter {
opacity: 0;
}
.fade-leave-active {
transition: opacity 0.5s;
}
.fade-leave-to {
opacity: 0;
}
</style> </style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!