iview的树组件在有默认选中状态的时候默认选中状态的样式改变有bug,默认选中的样式不好看,鉴于此,有renderContent来改造iview的树组件,

效果如图

代码如下

<template>
<div class="transfer">
<div class="transfer-left">
<div class="transfer-header">
<h2 class="title">源数据</h2>
</div>
<div class="transfer-body">
<vue-scroll>
<Tree
ref="tree"
:data="data2"
multiple
:render="renderContent">
</Tree>
</vue-scroll>
</div>
</div>
<div class="transfer-middle"></div>
<div class="transfer-right">
<div class="transfer-header">
<h2 class="title">目的数据</h2>
</div>
<div class="transfer-body">
<ul class="transfer-lists">
<li class="transfer-list" v-for="(item, index) in selectedList" :key="index">
<span class="name">{{item.title}}</span>
<Button class="btn-del" icon="md-close" type="text" @click="cancelSelected(item, index)"></Button>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'transfer',
data () {
return {
SelectClass: 'ivu-tree-title ivu-tree-title-selected',
DefineClass: 'ivu-tree-title',
selectedList: [],
selectedData: [
{
'id': 111,
'title': 'leaf 1-1-1'
},
{
'id': 112,
'title': 'leaf 1-1-2'
}
],
data2: [
{
id: 1,
title: 'parent 1',
expand: true,
hasChild: true,
children: [
{
id: 11,
title: 'parent 1-1',
expand: true,
hasChild: true,
children: [
{
id: 111,
title: 'leaf 1-1-1'
},
{
id: 112,
title: 'leaf 1-1-2'
}
]
},
{
id: 12,
title: 'parent 1-2',
expand: true,
hasChild: true,
children: [
{
id: 121,
title: 'leaf 1-2-1'
},
{
id: 122,
title: 'leaf 1-2-2'
}
]
}
]
}
]
}
},
created () {
for (let i = 0, l = this.selectedData.length; i < l; i++) {
this.traverseTree(this.data2[0], this.selectedData[i].id, false)
}
this.selectedList = this.selectedData
},
methods: {
iconType (hasChild, expand) {
let iconType = 'ios-document'
if (hasChild) {
if (expand) {
iconType = 'ios-folder-open'
} else {
iconType = 'ios-folder'
}
} else {
iconType = 'ios-document'
}
return iconType
},
cancelSelected (item, index) {
this.selectedList.splice(index, 1)
this.traverseTree(this.data2[0], item.id, true)
this.$refs.tree.$el.querySelectorAll('.ivu-tree-title-selected')[index].className = 'ivu-tree-title'
},
traverseTree (node, id, isDel) {
if (!node) {
return
}
if (node.id === id) {
if (isDel) {
node.selected = false
} else {
node.selected = true
}
}
let children = node.children
if (children && children.length > 0) {
for (let i = 0, l = children.length; i < l; i++) {
this.traverseTree(children[i], id, isDel)
}
}
},
renderContent (h, { root, node, data }) {
if (node.node.selected) {
return h('div', {
class: ['ivu-tree-title', 'ivu-tree-title-selected'],
on: {
click: (e) => {
let thisClassName = e.target.className
let parentClassName = e.target.parentNode.className
if (thisClassName === this.SelectClass || parentClassName === this.SelectClass) {
node.node.selected = false
if (thisClassName === this.SelectClass) {
e.target.className = this.DefineClass
} else {
e.target.parentNode.className = this.DefineClass
}
} else {
node.node.selected = true
if (thisClassName === this.DefineClass) {
e.target.className = this.SelectClass
} else {
e.target.parentNode.className = this.SelectClass
}
}
this.OnSelect(node)
}
}
},
[
h('Icon', {
props: {
type: this.iconType(node.node.hasChild, node.node.expand)
}
}),
h('span', data.title),
h('Icon', {
props: {
type: 'md-checkmark'
}
})
])
} else {
return h('div', {
class: ['ivu-tree-title'],
on: {
click: (e) => {
let thisClassName = e.target.className
let parentClassName = e.target.parentNode.className
if (thisClassName === this.SelectClass || parentClassName === this.SelectClass) {
node.node.selected = false
if (thisClassName === this.SelectClass) {
e.target.className = this.DefineClass
} else {
e.target.parentNode.className = this.DefineClass
}
} else {
node.node.selected = true
if (thisClassName === this.DefineClass) {
e.target.className = this.SelectClass
} else {
e.target.parentNode.className = this.SelectClass
}
}
this.OnSelect(node)
}
}
},
[
h('Icon', {
props: {
type: this.iconType(node.node.hasChild, node.node.expand)
}
}),
h('span', data.title),
h('Icon', {
props: {
type: 'md-checkmark'
}
})
])
}
},
OnSelect (data) {
this.$emit('OnSelectChange', data)
this.selectedList = this.$refs.tree.getSelectedNodes()
}
}
}
</script>
<style lang="scss" scoped>
@import './transfer.scss';
</style>
<style lang="scss">
.transfer {
.ivu-tree-title-selected,
.ivu-tree-title-selected:hover,
.ivu-tree-title:hover {
background-color: #fff;
}
.ivu-tree-title {
vertical-align: middle;
span {
margin: 0 5px;
}
.ivu-icon {
color: #2b85e4;
font-size: 16px;
&.ivu-icon-md-checkmark {
display: none;
}
}
&.ivu-tree-title-selected {
.ivu-icon {
&.ivu-icon-md-checkmark {
display: inline-block;
color: #19be6b;
}
}
}
}
}
</style>

对于节点的点击事件,可以做简单的抽离,js部分代码如下:

export default {
name: 'transfer',
data () {
return {
nodeKey: null,
SelectClass: 'ivu-tree-title ivu-tree-title-selected',
DefineClass: 'ivu-tree-title',
selectedList: [],
selectedData: [
{
'id': 111,
'title': 'leaf 1-1-1'
},
{
'id': 112,
'title': 'leaf 1-1-2'
}
],
data2: [
{
id: 1,
title: 'parent 1',
expand: true,
hasChild: true,
children: [
{
id: 11,
title: 'parent 1-1',
expand: true,
hasChild: true,
children: [
{
id: 111,
title: 'leaf 1-1-1'
},
{
id: 112,
title: 'leaf 1-1-2'
}
]
},
{
id: 12,
title: 'parent 1-2',
expand: true,
hasChild: true,
children: [
{
id: 121,
title: 'leaf 1-2-1'
},
{
id: 122,
title: 'leaf 1-2-2'
}
]
}
]
}
]
}
},
created () {
for (let i = 0, l = this.selectedData.length; i < l; i++) {
this.traverseTree(this.data2[0], this.selectedData[i].id, false)
}
this.selectedList = this.selectedData
},
methods: {
iconType (hasChild, expand) {
let iconType = 'ios-document'
if (hasChild) {
if (expand) {
iconType = 'ios-folder-open'
} else {
iconType = 'ios-folder'
}
} else {
iconType = 'ios-document'
}
return iconType
},
cancelSelected (item, index) {
this.selectedList.splice(index, 1)
this.traverseTree(this.data2[0], item.id, true)
this.$refs.tree.$el.querySelectorAll('.ivu-tree-title-selected')[index].className = 'ivu-tree-title'
},
renderContent (h, { root, node, data }) {
if (node.node.selected) {
return h('div', {
class: ['ivu-tree-title', 'ivu-tree-title-selected'],
on: {
click: (e) => {
let thisClassName = e.target.className
let parentClassName = e.target.parentNode.className
let o = this.changeNode(thisClassName, parentClassName)
node.node.selected = o.selected
if (thisClassName === o.otherClassName) {
e.target.className = o.className
} else if (parentClassName === o.otherClassName) {
e.target.parentNode.className = o.className
}
this.OnSelect(node)
}
}
},
[
h('Icon', {
props: {
type: this.iconType(node.node.hasChild, node.node.expand)
}
}),
h('span', data.title),
h('Icon', {
props: {
type: 'md-checkmark'
}
})
])
} else {
return h('div', {
class: ['ivu-tree-title'],
on: {
click: (e) => {
let thisClassName = e.target.className
let parentClassName = e.target.parentNode.className
let o = this.changeNode(thisClassName, parentClassName)
node.node.selected = o.selected
if (thisClassName === o.otherClassName) {
e.target.className = o.className
} else if (parentClassName === o.otherClassName) {
e.target.parentNode.className = o.className
}
this.OnSelect(node)
}
}
},
[
h('Icon', {
props: {
type: this.iconType(node.node.hasChild, node.node.expand)
}
}),
h('span', data.title),
h('Icon', {
props: {
type: 'md-checkmark'
}
})
])
}
},
OnSelect (data) {
this.$emit('OnSelectChange', data)
this.selectedList = this.$refs.tree.getSelectedNodes()
},
changeNode (thisClassName, parentClassName) {
let selected = false
let className = this.DefineClass
let otherClassName = this.SelectClass
if (thisClassName === this.SelectClass || parentClassName === this.SelectClass) {
selected = false
className = this.DefineClass
otherClassName = this.SelectClass
} else {
selected = true
className = this.SelectClass
otherClassName = this.DefineClass
}
return {
selected,
className,
otherClassName
}
},
traverseTree (node, id, isDel) {
if (!node) {
return
}
if (node.id === id) {
if (isDel) {
node.selected = false
} else {
node.selected = true
}
}
let children = node.children
if (children && children.length > 0) {
for (let i = 0, l = children.length; i < l; i++) {
this.traverseTree(children[i], id, isDel)
}
}
}
}
}

iView 用renderContent自定义树组件的更多相关文章

  1. iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key

    iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key 关 ...

  2. 基于HTML5树组件延迟加载技术实现

    HT for Web的HTML5树组件有延迟加载的功能,这个功能对于那些需要从服务器读取具有层级依赖关系数据时非常有用,需要获取数据的时候再向服务器发起请求,这样可减轻服务器压力,同时也减少了浏览器的 ...

  3. HT for Web的HTML5树组件延迟加载技术实现

    HT for Web的HTML5树组件有延迟加载的功能,这个功能对于那些需要从服务器读取具有层级依赖关系数据时非常有用,需要获取数据的时候再向服务器发起请求,这样可减轻服务器压力,同时也减少了浏览器的 ...

  4. SSIS自定义数据流组件开发(血路)

    由于特殊的原因(怎么特殊不解释),需要开发自定义数据流组件处理. 查了很多资料,用了不同的版本,发现各种各样的问题没有找到最终的解决方案. 遇到的问题如下: 用VS2015编译出来的插件,在SSDTB ...

  5. Android Studio开发基础之自定义View组件

    一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...

  6. [UE4]自定义MovementComponent组件

    自定义Movement组件 目的:实现自定义轨迹如抛物线,线性,定点等运动方式,作为组件控制绑定对象的运动. 基类:UMovementComponent 过程: 1.创建UCustomMovement ...

  7. 【转】Android学习基础自定义Checkbox组件

    原文网址:http://forum.maiziedu.com/thread-515-1-1.html heckbox组件是一种可同时选中多项的基础控件,即复选框,在android学习中,Checkbo ...

  8. GUI树组件,表格

    树组件首先要new一个JTree,再加结点,然后添加到 JScrollPane JTree tree1=new JTree(); //.......添加节点 add(new ScrollPane(tr ...

  9. Tree( 树) 组件[4]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...

随机推荐

  1. CPU中的上下文

    目录 一.简介 二.进程切换 三.线程切换 四.中断切换 五.中断检测和查看 六.模拟 一.简介 Linux是多任务操作系统,cpu划分固定时间片,分给每个进程,当前进程时间片执行完毕,将挂起,运行下 ...

  2. [BUUCTF]PWN2——rip

    [BUUCTF]PWN2-rip 题目网址:https://buuoj.cn/challenges#rip 步骤: 例行检查附件,64位程序,没有开启任何保护 nc一下,看看输入点的提示字符串,让我们 ...

  3. vscode 设置

    { "security.workspace.trust.enabled": false, "workbench.editor.enablePreview": f ...

  4. Python3 json &pickle 数据序列化

    json 所有语言通用的信息交换格式 json.dumps()将list列表.dict字典.元组.函数等对象转换为可以存储的字符格式存入文件 json.dump(数据对象名,已以写方式打开的对象) 直 ...

  5. 【LeetCode】5083. Occurrences After Bigram 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串分割遍历 日期 题目地址:https://le ...

  6. 【九度OJ】题目1181:遍历链表 解题报告

    [九度OJ]题目1181:遍历链表 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1181 题目描述: 建立一个升序链表并遍历输出. ...

  7. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  8. 【剑指Offer】丑数 解题报告

    [剑指Offer]丑数 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述: ...

  9. CHARINDEX 用法

    CHARINDEX 返回字符串中指定表达式的起始位置. 语法 CHARINDEX ( expression1 , expression2 [ , start_location ] ) 参数 expre ...

  10. MySQL中视图的定义、原理--触发器

    视图概述 视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用 ...