antd中在vue项目中自定义穿梭框

news/2024/11/17 23:37:43/

antd中在vue项目中自定义穿梭框

1、完成代码

<template><a-modaltitle="高危因素选择":width="1000":visible="riskVisible":confirm-loading="confirmLoading"@ok="handleOk"@cancel="handleCancel"okText="确认"><a-transferclass="tree-transfer":data-source="dataSource":target-keys="targetKeys":render="item => item.title":show-select-all="false"@change="onChange":titles="['可选择高危因素', '已选中高危因素']"><templateslot="children"slot-scope="{ props: { direction, selectedKeys }, on: { itemSelect } }"><div v-if="direction === 'left'"><div class="nav-list"><div  v-for="(item, index) in navList":key="index"@click="handleNavChange(index)":class="['nav-item', currentNav === index ? 'current-color' : '']">{{item.title}}</div></div><div class="left-ipt"><a-input placeholder="搜索可选择内容"><a-icon slot="prefix" type="search"></a-icon></a-input><a-button type="primary" class="search-btn">搜索</a-button></div><a-treev-if='treeData.length':showLine="showLine"blockNodecheckablecheckStrictly:tree-data="renderTreeData":defaultExpandedKeys="['1', '2']":checkedKeys="[...selectedKeys, ...targetKeys]"@check="(_, props) => {onLeftChecked(_, props, [...selectedKeys, ...targetKeys], itemSelect);}"><template slot='custom' slot-scope='item'><div v-if="item.flag === '1'"> {{ item.title+'('+item.level+')' }}</div><div v-if="item.flag === '2'"><span :style="{width:'20px',height:'10px',background:item.color,padding:'5px 10px',marginRight:'5px'}">{{ item.levelText}}</span>{{ item.title}}</div></template></a-tree></div><div v-if="direction === 'right'"><div class="left-ipt"><a-input placeholder="搜索可选择内容"><a-icon slot="prefix" type="search"></a-icon></a-input><a-button type="primary" class="search-btn">搜索</a-button></div><div><div v-for="item in rightData" :key="item.key"><a-checkbox @change="(e) => handleRightChange(e, [...selectedKeys], itemSelect, item.key)"><span :style="{width:'20px',height:'10px',background:item.color,padding:'5px 10px',marginRight:'5px'}">{{ item.levelText}}</span>{{ item.title}}</a-checkbox></div></div></div></template></a-transfer></a-modal>
</template>
<script>
import { getAction} from '@api/manage'
export default {props: {riskVisible: {type: Boolean,default: false}},data() {return {dataSource: [], // selectedKeys: [],confirmLoading: false,targetKeys: [],treeData: [],navList:[{title: '颜色分类',},{title: '常用分类'},{title: '疾病分类'}],currentNav: 0,showLine: true,rightData: [],rightSelectKeys: [],};},computed: {renderTreeData() {return this.handleTreeData(this.treeData, this.targetKeys);},},created() {this.initData ();},methods: {initData () {getAction("/biz/bizFiveColorConfig/getTreeData",null).then(res => {if (res.success) {this.treeData = res.result;let resultTranferData = [];res.result.forEach(item => {resultTranferData.push({title: item.title, key: item.key});if(item.children && item.children !== null) {item.children.forEach(it => {resultTranferData.push({title: it.title,key: it.key,});})}})this.dataSource = resultTranferData;} else {this.treeData = [];this.dataSource = [];}})},handleTreeData(data, targetKeys = []) {data.forEach(item => {item['disabled'] = true;if (item.children && item.children != null) {item.children.forEach(it => {if(targetKeys.includes(it.key)) {it.disabled = true;} else {it.disabled = false}})}});return data;},handleRightChange(event, checkedKeys, itemSelect, key) {itemSelect(key, !this.isChecked(checkedKeys, key));const { target } = event;if(target.checked) {this.rightSelectKeys.push(key);this.rightSelectKeys = [...new Set(this.rightSelectKeys)];}},isChecked(selectedKeys, eventKey) {return selectedKeys.indexOf(eventKey) !== -1;},onChange(targetKeys, direction) {if(direction == 'right') {this.targetKeys = targetKeys;let resultData = [];this.treeData.forEach(item => {if(item.children && item.children != null) {}item.children.forEach(it => {if(this.targetKeys.includes(it.key)) {resultData.push(it);}})})this.rightData = resultData;} else {const resKeys = [];const resRightData = [];this.rightData.forEach(item => {if(!this.rightSelectKeys.includes(item.key)) {resKeys.push(item.key);resRightData.push(item);}})this.targetKeys = resKeys;this.rightData = resRightData;}},// 左侧复选框选中onLeftChecked(_, e, checkedKeys, itemSelect) {const { eventKey } = e.node;itemSelect(eventKey, !this.isChecked(checkedKeys, eventKey));},// 确认handleOk() {const chooseFactorArr = [];this.treeData.forEach(item => {let children = [];if(item.children && item.children != null) {children = item.children.filter(it => it.disabled);if(children.length) {chooseFactorArr.push({...item,children})}}})this.confirmLoading = true;setTimeout(() => {this.$emit('handleRiskVisible', { bool: false, chooseFactorArr})this.confirmLoading = false;}, 1000);},// 取消handleCancel() {this.$emit('handleRiskVisible', { bool: false})},// nav切换handleNavChange(index) {this.currentNav = index;}},
};
</script>
<style scoped lang="less">.left-ipt {display: flex;margin: 15px 0 13px;
}.search-btn {margin-left: 8px;
}.nav-list {display: flex;cursor: pointer;
}.nav-item {padding: 0 8px;height: 24px;line-height: 24px;background: #EEEEEE;border-radius: 4px;margin-right: 16px;font-size: 12px;font-weight: 400;color: #333333;
}.current-color {background: #2AB967;color: white;
}.right-ipt {display: flex;
}/deep/.ant-modal-footer {border-top: none;padding: 4px 24px 30px;
}.tips {margin-top: 10px;font-size: 14px;color: #666666;line-height: 20px;
}/deep/.ant-transfer-list-content-item {padding: 0 12px;
}/deep/.ant-transfer-list {border: none;
}/deep/.ant-transfer-list-header {border-bottom: none;border-radius: 0;
}/deep/.ant-transfer-list-body {border-radius: 4px;border: 1px solid #DDDDDD;
}/deep/.ant-transfer-list-header-selected span:first-child {position: relative;left: 100px;font-size: 12px;color: #999999;
}
.folder-icon {width: 14px;height: 11px;margin: 4px 0 6px 0;
}
/deep/.ant-transfer-list-header-title {left: 0;font-size: 14px;font-weight: 500;color: #333333;
}/deep/.ant-input-affix-wrapper {color: #999999;
}/deep/.ant-transfer-list {width: 100%;overflow: auto;
}/deep/.ant-tree.ant-tree-block-node li span.ant-tree-checkbox + .ant-tree-node-content-wrapper {text-overflow: ellipsis;overflow: hidden;white-space: nowrap;
}
</style>

2、以上需要注意的点:

dataSource做为穿梭框的数据源需要是一维数组,并且里面的属性title和key都要为string类型,不然就会报错(dataSource类型不正确)

在这里插入图片描述

3、最终实现的效果
在这里插入图片描述


http://www.ppmy.cn/news/1094279.html

相关文章

3dsMax软件安装包分享(附安装教程)

目录 一、软件简介 二、软件下载 一、软件简介 3dsMax是一款由Autodesk公司开发的著名的三维计算机图形软件&#xff0c;广泛应用于动画、游戏、建筑和产品设计等领域。它以强大的建模、动画、渲染和特效功能而闻名&#xff0c;为用户提供了一个完整的制作流程&#xff0c;从…

【已解决】钉钉审批流回调瞬间返回两次通知

【已解决】钉钉审批流回调瞬间返回两次通知 一、产生原因二、解决方案&#xff08;一&#xff09;理论方案参考啊&#xff08;二&#xff09;代码方案参考 三、参考链接 一、产生原因 钉钉审批流回调应该只发一次通知给开发者。但实际情况是&#xff0c;钉钉有时会瞬间返回两次…

【CUDA OUT OF MEMORY】【Pytorch】计算图与CUDA OOM

计算图与CUDA OOM 在实践过程中多次碰到了CUDA OOM的问题&#xff0c;有时候这个问题是很好解决的&#xff0c;有时候DEBUG一整天还是头皮发麻。 最近实践对由于计算图积累导致CUDA OOM有一点新的看法&#xff0c;写下来记录一下。包括对计算图的一些看法和一个由于计算图引发…

解决DCNv2不能使用高版本pytorch编译的问题

可变形卷积网络GitHub - CharlesShang/DCNv2: Deformable Convolutional Networks v2 with Pytorch代码已经出来好几年了&#xff0c;虽然声称"Now the master branch is for pytorch 1.x"&#xff0c;实际上由于pytorch自1.11版开始发生了很大变化&#xff0c;原来基…

设计模式8:代理模式-动态代理

上一篇&#xff1a;设计模式8&#xff1a;代理模式-静态代理 目录 如何理解“动态”这两个字&#xff1f;动态代理简单的代码实例一个InvocationHandler代理多个接口有动态代理&#xff0c;为什么还要用Cglib代理&#xff1f; 如何理解“动态”这两个字&#xff1f; “动态”…

数学建模圈养湖羊的空间利用率

数学建模圈养湖羊的空间利用率 问题&#xff1a;规模化的圈养养殖场通常根据牲畜的性别和生长阶段分群饲养&#xff0c;适应不同种类、不同阶段的牲畜对空间的不同要求&#xff0c;以保障牲畜安全和健康&#xff1b;与此同时&#xff0c;也要尽量减少空间闲置所造成的资源浪费…

使用Apache Doris自动同步整个 MySQL/Oracle 数据库进行数据分析

Flink-Doris-Connector 1.4.0 允许用户一步将包含数千个表的整个数据库&#xff08;MySQL或Oracle &#xff09;摄取到Apache Doris&#xff08;一种实时分析数据库&#xff09;中。 通过内置的Flink CDC&#xff0c;连接器可以直接将上游源的表模式和数据同步到Apache Doris&…

vue深拷贝的几种实现方式

1、通过递归方式实现深拷贝 比较全面的深拷贝&#xff0c;缺点是较为繁琐 function deepClone(obj) {var target {};for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) {if (typeof obj[key] object) {target[key] deepClone(obj[key]);} else {…