luyj-tree-navigation.vue
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<template>
<view class="title">
<scroll-view ref="sea" scroll-x style="width: 100%;white-space: nowrap;">
<!-- 全部 -->
<view class="inline-item" @click="clickItem(null,-1)">
<text v-if="!isre && treeStack.length == 0" class="none">{{ $t('common.allText') }}</text>
<text v-else class="active">{{ $t('common.allText') }}</text>
</view>
<!-- 全部 -->
<!-- 搜索结果 -->
<view v-if="isre" @click="clickItem(null,-2)"
:class="activeSearch?'active inline-item':' none inline-item'">
<i class="iconfont icon-z043 iconclass" />
搜索结果
</view>
<!-- 搜索结果 -->
<!-- 当前树的层级值 -->
<view v-for="(item,index) in treeStack" class="inline-item" :key="index">
<view class="inline-item" @click="clickItem(item,index)">
<i class="iconfont icon-z043 iconclass" />
<text v-if="index== treeStack.length-1" class="none inline-item">
{{item[slabel]}}
</text>
<text v-else class="active">
{{item[slabel]}}
</text>
</view>
</view>
<!-- 当前树的层级值 -->
</scroll-view>
</view>
</template>
<script>
/**
* 无限级树-面包屑导航
* @description 无限级树的面包屑导航
* @property {String} slabel 显示的label值
* @return {Function} clickItem(item , index) 点击导航栏的索引
* @item 表示导航项对应的值
* @index 表示导航项的层级别索引
* @value -1 全部
* @value -2 表示层级
* @value 其他 (从最外层开始,依次0,1,2,3……)
* @return {Object} inF 导航条内部的方法
* @param {Function} isIre 设置是否搜索状态
* @param {Function} setTreeStack 设置导航树的值
* @param {Function} pushTreeStack 为导航树添加项
* @param {Function} clearTreeStack 清空导航树
*/
// scrollLeft : 暂时
export default {
name: "luyj-tree-navigation",
props: {
// 显示的label值
slabel: {
type: String,
default: 'label'
},
},
data() {
return {
isre: false, // 是否进行了搜索(返回是否进行了搜索)
treeStack: [], // 当前搜索值
}
},
computed: {
// 是否可点击搜索结果
activeSearch: function() {
return this.treeStack.length > 0;
}
},
created: function() {
// 浅拷贝导航列表的每一个对象(为了不改变item值,也不复制过多的数据)
this.treeStack.forEach(item => {
var tempItem = Object.assign(item);
this.treeStack.push(tempItem);
});
var obj = {
setIsre: this.setIsre,
getIsre : this.getIsre,
setTreeStack: this.setTreeStack,
concatTreeStack : this.concatTreeStack,
pushTreeStack: this.pushTreeStack,
clearTreeStack: this.clearTreeStack,
getTreeStack : this.getTreeStack
};
this.$emit("inF", obj); // 导出的导航栏调用方法
},
methods: {
// ================================== 初始化时导出方法(用于外部调用内部结果) =========================================================
/** 设置isre值(是否搜索)
* @param {Boolean} isre 设置是否搜索
*/
setIsre: function(isre) {
this.isre = isre;
},
/**
* 获取isr值(获取是否搜索中)
*/
getIsre: function(){
return this.isre;
},
/** 设置导航树
* @param {Array} treeStack 导航树
*/
setTreeStack: function(treeStack) {
this.treeStack = treeStack;
},
/** 拼接导航树
* @param {Object} treeStack 导航树
*/
concatTreeStack : function(treeStack){
this.treeStack = this.treeStack.concat(treeStack);
},
/** 为导航树添加项
* @param {Object} item 待添加的对象
*/
pushTreeStack: function(item) {
this.treeStack.push(item);
},
/**
* 获取当前导航条
*/
getTreeStack : function(){
return this.treeStack;
},
/**
* 清空导航树
*/
clearTreeStack: function() {
this.treeStack.splice(0);
},
// ================================== 监听事件 ===========================================================
/** 点击导航栏索引
* @param {Object} item 当前层的值
* @param {Number} index 索引值
*/
clickItem(item, index) {
if (index == -1) {
// 点击全部
this.isre = false;
this.treeStack.splice(0);
} else if (index == -2) {
// 搜索结果
if (this.activeSearch) {
this.isre = true;
this.treeStack.splice(0);
}
} else {
// 点击某一层级树
this.isre = false;
if (this.treeStack.length - 1 > index) {
this.treeStack.splice(index + 1);
}
}
this.$emit("clickItem", item, index);
},
},
// ============================================================================================================
}
</script>
<style lang="scss" scoped>
@import "luyj-tree-navigation.scss";
@import "icon.css";
</style>