写完方便下次利用,于是放在这里,也可以给大家参考参考遇到的话

代码片段
<template>
<div>
<el-dialog title="批量转移客户资料" :visible.sync="tranVisible" width="30%">
<el-form
ref="ruleForm"
:model="ruleForm"
:rules="rules"
label="right"
label-width="60px"
class="demo-ruleForm"
:label-position="labelPosition"
>
<el-form-item label-width="22px">
<el-radio v-model="radio" label="1">{{ `已选择${ids.length}个客户` }}</el-radio>
</el-form-item>
<el-form-item label="组别" prop="treeValue">
<div class="tree-container">
<el-select
ref="selectTree"
v-model="ruleForm.treeValue"
class="main-select-tree"
clearable
style="width: 60%;"
>
<el-option
v-for="item in formatData(deptOptions)"
:key="item.value"
:label="item.label"
:value="item.value"
style="display: none;"
/>
<el-tree
ref="selecteltree"
class="main-select-el-tree"
:data="deptOptions"
node-key="id"
highlight-current
:props="defaultProps"
:current-node-key="ruleForm.treeValue"
:expand-on-click-node="expandOnClickNode"
:default-expand-all="false"
@node-click="handleNodeClick"
/>
</el-select>
</div>
</el-form-item>
<el-form-item v-if="CheckList.length > 0" prop="type" label-width="20px">
<div class="content">
<div class="selectAll">
<div
:class="['select', SelectId.length == CheckList.length ? 'selectActv' : '']"
@click="allFun()"
>
</div>
<span>全选/反选</span> <span class="zhuan">选择转入工号,数字为当天转入数量</span>
</div>
<div class="CheckBox">
<div v-for="(item, index) in CheckList" :key="index" class="CheckItem">
<div
:class="['select', SelectId.includes(item.userId) ? 'selectActv' : '']"
@click="selectFun(item.userId)"
></div>
<span>{{ item.userId }}: {{ item.nickName }}</span>
<span style="padding-left: 4px;">{{ [Number(item.count)] }}</span>
</div>
</div>
</div>
</el-form-item>
<el-form-item>
<el-button style="float:right;" type="primary" @click="submit('ruleForm')">确定转移</el-button>
<el-button style="float:right;margin-right:15px" @click="cancel('ruleForm')">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
</div>
</template>
<script>
import { treeselect } from '@/api/admin/sys-dept'
import { getTranCtm, plTrainCustom } from "@/api/ctm/ctm-my";
export default {
name: "TramCtm",
components: {
},
props: {
ids: {
type: Array,
default: () => []
}
},
data() {
return {
radio: '1',
tranVisible: false,
labelPosition: 'right',
ruleForm: {
treeValue: null,
},
rules: {
treeValue: [{ required: true, message: "请选择部门", trigger: "change" }],
},
SelectId: [],
CheckList: [],
expandOnClickNode: true,
deptOptions: [],
defaultProps: {
children: 'children',
label: 'label'
}
}
},
computed: {
ChooseList() {
return this.CheckList.filter((item) => {
if (this.SelectId.includes(item.id)) {
return item
}
})
},
},
watch: {
'ruleForm.treeValue': {
deep: true,
immediate: true,
handler(v) {
this.getDepPeople(v)
}
}
},
mounted() {
this.getTreeselect()
},
methods: {
getTreeselect() {
treeselect().then(response => {
this.deptOptions = response.data
})
},
getDepPeople(id) {
const deptId = {
deptId: id
}
getTranCtm(deptId).then((res) => {
this.CheckList = res.data
})
},
formatData(data) {
const options = [];
data.forEach((item, key) => {
options.push({ label: item.label, value: item.id });
if (item.children) {
item.children.forEach((items, keys) => {
options.push({ label: items.label, value: items.id });
if (items.children) {
items.children.forEach((itemss, keyss) => {
options.push({ label: itemss.label, value: itemss.id });
if (itemss.children) {
itemss.children.forEach((itemsss, keysss) => {
options.push({ label: itemsss.label, value: itemsss.id });
});
}
});
}
});
}
});
return options;
},
handleNodeClick(node) {
this.ruleForm.treeValue = node.id;
this.$refs.selectTree.blur();
},
selectFun(userId) {
if (!this.SelectId.includes(userId)) {
this.SelectId.push(userId)
} else {
const index = this.SelectId.indexOf(userId)
this.SelectId.splice(index, 1)
}
console.log(this.SelectId, 'this.SelectId');
},
allFun() {
if (this.SelectId.length === this.CheckList.length) {
this.SelectId = []
} else {
this.CheckList.forEach((item) => {
if (!this.SelectId.includes(item.userId)) {
this.SelectId.push(item.userId)
}
})
}
console.log(this.ChooseList, 'ChooseList');
},
cancel() {
this.tranVisible = false
this.SelectId = []
},
submit(formName) {
this.$refs[formName].validate(async (valid) => {
if (valid) {
let res;
const data = {
CustomerIds: this.ids,
UserIds: this.SelectId
}
res = await plTrainCustom(data);
if (res.code === 200) {
this.tranVisible = false
this.$message.success("转交客户成功");
this.$refs.ruleForm.resetFields();
this.$emit('ok');
this.SelectId = []
}
} else {
return false;
}
});
},
showDialog() {
this.tranVisible = true
},
}
}
</script>
<style lang="scss" scoped>
.tree-container {
padding: 0 !important;
}
.main-select-el-tree .el-tree-node .is-current>.el-tree-node__content {
font-weight: bold;
color: #409eff;
}
.main-select-el-tree .el-tree-node.is-current>.el-tree-node__content {
font-weight: bold;
color: #409eff;
}
.CheckBox {
width: 100%;
display: flex;
// flex-wrap: wrap;
// margin: 50px auto;
// margin-bottom: 20px;
flex-direction: column;
height: 300px;
overflow: auto;
}
.CheckItem {
display: flex;
// margin: 0px 30px 30px 0px;
align-items: center;
}
.select {
width: 20px;
height: 20px;
border-radius: 2px;
border: 1px solid #ccc;
display: flex;
justify-content: center;
cursor: pointer;
margin-right: 10px;
align-items: center;
}
.selectActv::before {
display: block;
content: "";
width: 5px;
height: 12px;
border-bottom: 2px solid #13ce66;
border-right: 2px solid #13ce66;
// border-right: 2px solid #aaa;
transform: rotate(45deg);
}
.selectAll {
display: flex;
align-items: center;
}
.content {
width: 500px;
// margin: 120px auto;
}
.zhuan {
display: inline-block;
padding-left: 12px;
font-size: 0.5rem;
color: #b8b6b6;
}
</style>
拆成组件,页面中使用
<tranCtm
ref="tran"
:tran-visible="'tranVisible'"
:ids="ids"
@ok="handleOk"
></tranCtm>
// 子组件要触发父组件查询方法
handleOk() {
this.onSelect()
},
这里来触发弹窗显示
handleTran() {
this.$refs.tran.showDialog()
// this.tranVisible=true
},