feat: 标准库 统一组织结构树组件
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
<template>
|
||||
<el-drawer
|
||||
class="org-tree"
|
||||
:title="drawerTitle"
|
||||
:visible.sync="modalShowFlag"
|
||||
:size="width"
|
||||
:append-to-body=true
|
||||
:before-close="cancelUserInfo"
|
||||
>
|
||||
<el-tree class="common-tree" :style="style" ref="tree" :data="data" :props="defaultProps"
|
||||
:load="loadNode"
|
||||
:lazy="lazy"
|
||||
:show-checkbox="multiple"
|
||||
:node-key="nodeKey"
|
||||
:check-strictly="checkStrictly"
|
||||
:expand-on-click-node="false"
|
||||
:default-checked-keys="defaultCheckedKeys"
|
||||
:highlight-current="true"
|
||||
@node-click="handleNodeClick"
|
||||
@check-change="handleCheckChange"></el-tree>
|
||||
<div id="roleFormButton" class="demo-drawer-footer">
|
||||
<el-button class="common-button-primary" size="mini" icon="el-icon-check" type="primary" @click="saveUserInfo">确定</el-button>
|
||||
<el-button class="common-button-default" size="mini" icon="el-icon-close" @click="cancelUserInfo">取消</el-button>
|
||||
</div>
|
||||
</el-drawer>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: "treeSelectNew",
|
||||
props: {
|
||||
// 树结构数据
|
||||
data: {
|
||||
type: Array,
|
||||
default () {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
default () {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
urlChild: {
|
||||
type: String,
|
||||
default () {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
params: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
defaultProps: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
// 配置是否可多选
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default () {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
lazy: {
|
||||
type: Boolean,
|
||||
default () {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
nodeKey: {
|
||||
type: String,
|
||||
default () {
|
||||
return 'id';
|
||||
}
|
||||
},
|
||||
// 显示复选框情况下,是否严格遵循父子不互相关联
|
||||
checkStrictly: {
|
||||
type: Boolean,
|
||||
default () {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default () {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
// 默认选中的节点key数组
|
||||
checkedKeys: {
|
||||
type: Array,
|
||||
default () {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
width: {
|
||||
type: String,
|
||||
default () {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
drawerTitle:{
|
||||
type: String,
|
||||
default () {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
// 配置是否展开
|
||||
modalShowFlag: {
|
||||
type: Boolean,
|
||||
default () {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
nodeList: [],
|
||||
defaultCheckedKeys: [],
|
||||
isShowSelect: false, // 是否显示树状选择器
|
||||
options: [],
|
||||
selectedData: [], // 选中的节点
|
||||
style: 'width:' + this.width + 'px;' + 'height:100%;',
|
||||
selectStyle: 'width:100%;',
|
||||
checkedIds: [],
|
||||
checkedData: [],
|
||||
};
|
||||
},
|
||||
created(){
|
||||
},
|
||||
mounted () {
|
||||
if (this.checkedKeys.length > 0) {
|
||||
if (this.multiple) {
|
||||
this.defaultCheckedKeys = this.checkedKeys;
|
||||
this.getChildByIDSList(this.defaultCheckedKeys);
|
||||
// this.selectedData = this.checkedKeys.map((item) => {
|
||||
// var node = this.$refs.tree.getNode(item);
|
||||
// return node.label;
|
||||
// });
|
||||
} else {
|
||||
const item = this.checkedKeys[0];
|
||||
this.getChildByIDSList(item);
|
||||
// this.$refs.tree.setCurrentKey(item);
|
||||
// var node = this.$refs.tree.getNode(item);
|
||||
// this.selectedData = node.label;
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
loadNode(node, resolve) {
|
||||
const that = this;
|
||||
|
||||
if (node.level === 0) {
|
||||
that.loadTreeData(resolve);
|
||||
}
|
||||
|
||||
if (node.level >= 1) {
|
||||
setTimeout(() => {
|
||||
this.getChildByList(node.data.id, resolve);
|
||||
}, 500);
|
||||
return resolve([]); // 加上这个,防止在该节点没有子节点时一直转圈的问题发生。
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
getChildByIDSList( _parentID) { // 获取子节点请求
|
||||
let params = {ids : _parentID};
|
||||
this.$http.get(this.urlChild, params, {
|
||||
_this: this
|
||||
}, res => {
|
||||
if(res.data){
|
||||
this.$emit('popoverHide', this.checkedIds, res.data,false);
|
||||
}
|
||||
})
|
||||
},
|
||||
getChildByList( _parentID,resolve) { // 获取子节点请求
|
||||
let params = {ids : _parentID};
|
||||
this.$http.get(this.urlChild, params, {
|
||||
_this: this
|
||||
}, res => {
|
||||
if(res.data){
|
||||
resolve(res.data);
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
},
|
||||
loadTreeData(resolve) {
|
||||
// 获取loadtreeData 就是父节点数据,getChildByList就是异步获取子节点数据
|
||||
this.$http.get(this.url, '', {
|
||||
_this: this
|
||||
}, res => {
|
||||
if(res.data){
|
||||
resolve(res.data)
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
},
|
||||
popoverHide () {
|
||||
|
||||
},
|
||||
// 节点被点击时的回调,返回被点击的节点数据
|
||||
handleNodeClick (data, node) {
|
||||
if (!this.multiple) {
|
||||
let tmpMap = {};
|
||||
tmpMap.value = node.key;
|
||||
tmpMap.label = node.label;
|
||||
this.options = [];
|
||||
this.options.push(tmpMap);
|
||||
this.selectedData = node.label;
|
||||
this.isShowSelect = !this.isShowSelect;
|
||||
}
|
||||
},
|
||||
// 节点选中状态发生变化时的回调
|
||||
handleCheckChange () {
|
||||
var checkedKeys = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据
|
||||
this.options = checkedKeys.map((item) => {
|
||||
var node = this.$refs.tree.getNode(item); // 所有被选中的节点对应的node
|
||||
let tmpMap = {};
|
||||
tmpMap.value = node.key;
|
||||
tmpMap.label = node.label;
|
||||
return tmpMap;
|
||||
});
|
||||
this.selectedData = this.options.map((item) => {
|
||||
return item.label;
|
||||
});
|
||||
},
|
||||
saveUserInfo () {
|
||||
if (this.multiple) {
|
||||
this.checkedIds = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据
|
||||
this.checkedData = this.$refs.tree.getCheckedNodes(); // 所有被选中的节点所组成的数组数据
|
||||
} else {
|
||||
this.checkedIds = this.$refs.tree.getCurrentKey();
|
||||
this.checkedData = this.$refs.tree.getCurrentNode();
|
||||
}
|
||||
this.$emit('popoverHide', this.checkedIds, this.checkedData,false);
|
||||
},
|
||||
cancelUserInfo () {
|
||||
this.$emit('popoverHide', this.checkedIds, this.checkedData,false);
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.el-drawer__body{
|
||||
& > div:first-child{
|
||||
display: flex;
|
||||
flex: auto;
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
::v-deep .el-drawer__header{
|
||||
&>span:first-child{
|
||||
outline: none!important;
|
||||
}
|
||||
}
|
||||
::v-deep .el-drawer{
|
||||
outline: none!important;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user