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. Spring Cloud Eureka源码分析之服务注册的流程与数据存储设计!

    Spring Cloud是一个生态,它提供了一套标准,这套标准可以通过不同的组件来实现,其中就包含服务注册/发现.熔断.负载均衡等,在spring-cloud-common这个包中,org.sprin ...

  2. C# 编写一个小巧快速的 Windows 动态桌面软件

    开源自己前段时间使用 C# 编写的 Windows 动态桌面软件,在接下来的博客我将描写一些技术细节和遇到的一些坑.这个软件可以把视频设置成桌面背景播放,不仅如此而且还可以把网页或一个网页文件设置成桌 ...

  3. 我在这里的处女篇(Word技巧集团)

    传说这里的文章可以在Word上打好了发布,Word嘛,有[听写]功能,不用打字了: 写好的文章还可以[大声朗读],边听边看最容易找"通假字"了. 冲这,我的新阵地就定这里了,哈哈~

  4. PowerDotNet平台化软件架构设计与实现系列(07):数据同步平台

    上文介绍定时任务调度平台的时候提到,定时任务调度平台的类模式一般用于处理耗时较长的任务.但是根据经验,有些耗时较长的任务,可以通过简化业务逻辑.分页.批量多次处理,改造为耗时较小的适合使用RESTfu ...

  5. CF46B T-shirts from Sponsor 题解

    Content 有一家服装店,有 \(\texttt{S}\) 码的衣服 \(n_S\) 件.\(\texttt{M}\) 码的衣服 \(n_M\) 件,\(\texttt{L}\) 码的衣服 \(n ...

  6. java 多线程 :ThreadLocal 共享变量多线程不同值方案;InheritableThreadLocal变量子线程中自定义值,孙线程可继承

      ThreadLocal类的使用 变量值的共享可以使用public static变量的形式,所有的线程都是用同一个public static变量.如果想实现每一个线程都有自己的值.该变量可通过Thr ...

  7. redis pipeset发布订阅

    #!/usr/bin/env python # Author:Zhangmingda import redis,time pool = redis.ConnectionPool(host='192.1 ...

  8. label标签利用jquery获取值得方式为$("#message").html()

    label标签利用jquery获取值的方式为$("#message").text(), 赋值的方式为:$("message").html("北京欢迎你 ...

  9. windows下php安装redis扩展

    查看当前PHP版本 代码中添加 phpinfo(); 下载对应的redis扩展 下载链接:https://pecl.php.net/package/redis 因为我的PHP版本是5.6的,所以red ...

  10. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...