1.2.3 • Published 4 months ago

cy-tree-transfer-vue3 v1.2.3

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

tree-transfer-vue3

效果图

tree-transfer-vue3效果图

简介

tree-transfer-vue3 是一个基于 VUE 和 element-plus 的树形穿梭框组件,使用前请确认已经引入element-plus! 此组件功能类似于element-plustransfer组件,但是里面的数据是树形结构! 实际上,tree-transfer-vue3 依赖的 element-plus 组件分别是Checkbox 多选框Button 按钮,和最主要的Tree 树形控件写成!并非是在 element-plus 的穿梭框组件上的扩展,而仅仅是参照了其外观样式和功能。

我命油我不油天,仿写一个 vue3 版本的树形穿梭框组件玩玩;参考el-tree-transfer

重要更新请使用最新版本

1.2.1 处理 defaultProps 改变(非默认值),数据不显示的问题

NPM - GitHub - CSDN

快速上手

npm 下载插件

npm install cy-tree-transfer-vue3 --save

npm i cy-tree-transfer-vue3 -S

然后你可以像使用普通组件一样使用 tree-transfer-vue3

<template>
  <tree-transfer
    ref="transferRef"
    v-model:fromData="fromData"
    v-model:toData="toData"
    :defaultProps="{
      id: 'id', // 节点id
      parentId: 'parentId', // 父节点id
      label: 'label',
      children: 'children',
      disabled: 'disabled',
    }"
    rootPid="0"
    @add="handleAdd"
    @remove="handleremove"
  />
</template>

<script setup>
import { ref } from 'vue';
import TreeTransfer from 'tree-transfer-vue3'; // 引入

const transferRef = ref(null); // 穿梭框Ref
const fromData = ref([
  {
    id: '1',
    parentId: 0,
    label: '一级 1',
    children: [
      {
        id: '1-1',
        parentId: '1',
        label: '二级 1-1',
        children: [],
      },
      {
        id: '1-2',
        parentId: '1',
        label: '二级 1-2',
        children: [
          {
            id: '1-2-1',
            parentId: '1-2',
            disabled: true,
            children: [],
            label: '二级 1-2-1',
          },
          {
            id: '1-2-2',
            parentId: '1-2',
            children: [],
            label: '二级 1-2-2',
          },
          {
            id: '1-2-3',
            parentId: '1-2',
            children: [],
            label: '二级 1-2-3',
          },
        ],
      },
    ],
  },
  {
    id: '2',
    parentId: 0,
    label: '一级 2',
    children: [
      {
        id: '2-1',
        parentId: '2',
        label: '二级 2-1',
      },
      {
        id: '2-2',
        parentId: '2',
        label: '二级 2-2',
      },
    ],
  },
  {
    id: '4',
    parentId: 0,
    label: '一级 4',
    children: [
      {
        id: '4-1',
        parentId: '4',
        label: '二级 4-1',
      },
      {
        id: '4-2',
        parentId: '4',
        label: '二级 4-2',
      },
    ],
  },
]);
const toData = ref([]);

const handleAdd = (_fromData, _toData, { checkedKeys, checkedNodes, harfKeys, harfNodes }) => {
  console.log('handleAdd', checkedKeys, checkedNodes, harfKeys, harfNodes);
};
const handleremove = (_fromData, _toData, { checkedKeys, checkedNodes, harfKeys, harfNodes }) => {
  console.log('handleremove', checkedKeys, checkedNodes, harfKeys, harfNodes);
};
</script>

属性

属性名说明类型默认值
language语言(默认中文,可选:zh-cn、en)Stringzh-cn
titleList标题列表Array'源列表', '目标列表'
showFilter是否显示过滤框Booleantrue
placeholder搜索框提示文字(注意:如果 placeholderList 里面有值,优先采用 placeholderList)String请输入关键字搜索
placeholderList搜索框提示文字列表Array[]
showBtnTxt是否显示按钮文字Booleanfalse
btnTitleList按钮文字(在 showBtnTxt 为 true 的情况下展示)Array'添加', '移除'
defaultProps树节点的属性配置Object见下表
rootPid根节点 pid,用于结束递归String/Number0
renderAfterExpand是否在第一次展开某个树节点后才渲染其子节点Booleantrue
load加载子树数据的方法,仅当 lazy 属性为 true 时生效Function--
fromRenderContent左侧 from 树节点的内容区的渲染函数Function--
toRenderContent右侧 to 树节点的内容区的渲染Function--
highlightCurrent是否高亮当前选中节点Booleanfalse
defaultExpandAll是否默认展开所有节点Booleantrue
expandOnClickNode是否在点击节点的时候展开或者收缩节点Booleanfalse
checkOnClickNode是否在点击节点的时候选中节点Booleantrue
autoExpandParent是否自动展开父节点Booleantrue
checkStrictly是否严格的遵守父子节点不互相关联Booleanfalse
accordion是否每次只打开一个同级树节点Booleanfalse
indent相邻级节点的水平缩进,单位为像素Number18
icon自定义显示节点前的展开箭头String--
lazy是否懒加载子节点数据Booleanfalse
draggable是否开启拖拽节点功能Booleanfalse
fromAllowDrag左侧树判断节点能否被拖拽 如果返回 false ,节点不能被拖动Function--
toAllowDrag右侧树判断节点能否被拖拽 如果返回 false ,节点不能被拖动Function--

defaultProps

defaultProps说明类型
id指定节点 id 为节点对象的某个属性值string
parentId指定节点的父节点 id 为节点对象的某个属性值string
label指定节点标签为节点对象的某个属性值string/Function
children指定子树为节点对象的某个属性值string
disabled指定节点选择框是否禁用为节点对象的某个属性值string/Function
isLeaf指定节点是否为叶子节点,仅在指定了 lazy 属性的情况下生效string/Function
class自定义节点类名string/Function

事件

序号事件名称说明回调参数
1add点击添加按钮时触发的事件(2.4.0 以前为 addBtn)function(fromData,toData,obj),树形穿梭框 transfer 模式分别为 1.移动后左侧数据,2.移动后右侧数据,3.移动的节点 keys、nodes、halfKeys、halfNodes 对象;
2remove点击移除按钮时触发的事件(2.4.0 以前为 removeBtn)function(fromData,toData,obj),树形穿梭框 transfer 模式分别为 1.移动后左侧数据,2.移动后右侧数据,3.移动的节点 keys、nodes、halfKeys、halfNodes 对象;
3nodeDragStart开始拖拽时触发的事件共 3 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、event
4nodeDragEnter拖拽进入其他节点时触发的事件共 4 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、所进入节点对应的 Node、event
5nodeDragLeave拖拽离开某个节点时触发的事件共 4 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、所离开节点对应的 Node、event
6nodeDragOver在拖拽节点时触发的事件(类似浏览器的 mouseover 事件)共 4 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、当前进入节点对应的 Node、event
7nodeDragEnd拖拽结束时(可能未成功)触发的事件共 5 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、结束拖拽时最后进入的节点(可能为空)、被拖拽节点的放置位置(before、after、inner)、event
8nodeDrop拖拽成功完成时触发的事件共 5 个参数,依次为:"from"/"to"、对应被拖拽节点对应的 Node、结束拖拽时最后进入的节点、被拖拽节点的放置位置(before、after、inner)、event

方法

序号名称说明
1fromTreeRef左侧 from 树 ref 对象
2toTreeRef右侧 to 树 ref 对象
3addToAims手动调用添加穿梭,用于调整初始数据默认穿梭 function(useCallBack: Boolean) 本次穿梭是否需要触发@add 的 emit 回调; 默认 true 触发
4removeToSource手动调用添加穿梭,用于调整初始数据默认穿梭 function(useCallBack: Boolean) 本次穿梭是否需要触发@remove 的 emit 回调; 默认 true 触发
5clearFilter清除选中节点,默认清除全部 function(type: String) from 左边; to 右边; all 全部; 默认 all
6clearCheck清除选中节点,默认清除全部 function(type: String) from 左边; to 右边; all 全部; 默认 all

插槽

插槽名说明
from-titlefrom 穿梭框顶部标题
to-titleto 穿梭框顶部标题
from-tree-contentfrom 穿梭框 tree 节点内容(节点 node、数据 data,两个参数)
to-tree-contentto 穿梭框 tree 节点内容(节点 node、数据 data,两个参数)
from-footerfrom 穿梭框底部
to-footerto 穿梭框底部

版本说明

1.1.0 添加说明文档,更新事件、方法、插槽

1.2.0 导出方式修改,页面样式修改,请使用最新版本

1.2.1 处理 defaultProps 改变(非默认值),数据不显示的问题

1.2.2 语言功能

GitHub demo 代码地址 欢迎 star 谢谢

有 BUG 请随时联系我,可以在我的博客下面留言!