封装seltiplist组件

封装seltiplist组件

1.编写组件

1.清单

依赖清单:

  • underscore.min.js JavaScript工具库

编写清单:

  • seltipoption.vue SelectTipOption,SelectTipList的子组件
  • seltiplist.vue SelectTipList,SelectTipOption的父组件
  • emitter.js 用于父子组件通讯

2.seltipoption.vue

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
<template>
<span class="tip" @click="selectOptionClick" :class="{'active':itemSelected }">{{currentLabel}}</span>
</template>
<script type="text/ecmascript-6">
import Emitter from '../util/emitter'
var underscore = require('../lib/underscore.min')
export default{
mixins: [Emitter],
componentName: 'seltipoption',
props: {
value: {
required: true
},
label: [String, Number]
},
computed: {
currentLabel () {
return this.label || ((typeof this.value === 'string' || typeof this.value === 'number') ? this.value : '')
},
parent () {
let result = this.$parent
while (!result.istipSelect) {
result = result.$parent
}
return result
},
itemSelected () {
if (!this.parent.multiple) {
return underscore._.isEqual(this.parent.value, this.value)
} else {
let isSelected = false
this.parent.value.forEach((item, index) => {
if (underscore._.isEqual(item, this.value)) {
isSelected = true
}
})
return isSelected
}
}
},
methods: {
selectOptionClick () {
this.dispatch('seltiplist', 'handleOptionClick', this.value)
}
}
}
</script>

3.seltiplist.vue

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
<template>
<div>
<slot></slot>
</div>
</template>
<script type="text/ecmascript-6">
import Emitter from '../util/emitter'
var underscore = require('../lib/underscore.min')
export default{
mixins: [Emitter],
componentName: 'seltiplist',
data () {
return {
istipSelect: true
}
},
props: {
value: {},
multiple: {
type: Boolean,
default: false
}
},

mounted () {
this.$on('handleOptionClick', this.handleOptionSelect)
},
methods: {
handleOptionSelect (val) {
if (this.multiple && underscore._.isArray(this.value)) {
let optionIndex = this.value.indexOf(val)
this.value.forEach((item, index) => {
if (underscore._.isEqual(item, val)) {
optionIndex = index
}
})
if (optionIndex < 0) {
this.value.push(val)
} else {
this.value.splice(optionIndex, 1)
}
} else {
if (val !== this.value) {
this.$emit('input', val)
} else {
this.$emit('input', '')
}
}
}
}
}
</script>

4.emitter.js

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
function broadcast (componentName, eventName, params) {
this.$children.forEach(child => {
var name = child.$options.componentName
if (name === componentName) {
child.$emit.apply(child, [eventName].concat(params))
} else {
broadcast.apply(child, [componentName, eventName].concat(params))
}
})
}
export default {
methods: {
dispatch (componentName, eventName, params) {
var parent = this.$parent || this.$root
var name = parent.$options.componentName

while (parent && (!name || name !== componentName)) {
parent = parent.$parent

if (parent) {
name = parent.$options.componentName
}
}
if (parent) {
parent.$emit.apply(parent, [eventName].concat(params))
}
},
broadcast (componentName, eventName, params) {
broadcast.call(this, componentName, eventName, params)
}
}
}

2.使用组件

App.vue

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
168
169
170
171
172
173
174
175
176
177
178
<template>
<div style="width: 1100px;margin: 40px auto">
<div class="conditionbox">
<div class="selectlist">
<div class="title">地区</div>
<div class="select-box">
<seltiplist v-model="loacl_select">
<seltipoption v-for=" item in local " :label="item.label" :value="item" :key="item.id"></seltipoption>
</seltiplist>
</div>
</div>
<div class="selectlist">
<div class="title">薪资要求</div>
<div class="select-box">
<seltiplist v-model="salary_select">
<seltipoption v-for=" item in salary " :label="item.label" :value="item" :key="item.id"></seltipoption>
</seltiplist>
</div>
</div>
<div class="selectlist">
<div class="title">福利待遇</div>
<div class="select-box">
<seltiplist :multiple="true" v-model="treatment_select">
<seltipoption v-for=" item in treatment " :label="item.label" :value="item" :key="item.id"></seltipoption>
</seltiplist>
</div>
</div>
<div class="selectlist">
<div class="title">已选条件</div>
<div class="select-box">
<span class="tip active" v-if="loacl_select">{{loacl_select.label}}</span>
<span class="tip active" v-if="salary_select">{{salary_select.label}}</span>
<span class="tip active" v-for="item in treatment_select" :key="item.id">{{item.label}}</span>
</div>
</div>
</div>
</div>
</template>
<script>
import seltiplist from './components/seltiplist.vue'
import seltipoption from './components/seltipoption.vue'

export default {
components: {
seltiplist,
seltipoption
},
data () {
return {
loacl_select: '',
treatment_select: [
{
id: 4,
label: '项目奖金'
}
],
salary_select: '',
local: [
{
id: 1,
label: '北京'
},
{
id: 2,
label: '安徽'
},
{
id: 3,
label: '上海'
},
{
id: 4,
label: '广州'
},
{
id: 5,
label: '青岛'
}
],
treatment: [
{
id: 1,
label: '五险一金'
},
{
id: 2,
label: '交通补助'
},
{
id: 3,
label: '带薪年假'
},
{
id: 4,
label: '项目奖金'
}
],
salary: [
{
id: 1,
label: '4000以下'
},
{
id: 2,
label: '4000-6000'
},
{
id: 3,
label: '6000-8000'
},
{
id: 4,
label: '8000以上'
}
]
}
},
methods: {}
}
</script>
<style>
body,
html {
margin: 0;
padding: 0;
font-family: PingFang SC, Hiragino Sans GB, Microsoft Yahei, SimSun, Arial,
Helvetica Neue, Helvetica;
-webkit-font-smoothing: antialiased;
line-height: 1.42857143;
color: #666;
font-size: 14px;
background: #fdfbfb;
}

.conditionbox {
width: 100%;
margin-top: 10px;
background: #fff;
padding: 20px;
}

.selectlist {
padding-top: 10px;
padding-bottom: 10px;
}

.selectlist .title {
display: inline-block;
width: 98px;
margin-left: 10px;
font-weight: 700;
font-size: 15px;
margin-top: 3px;
}

.selectlist .select-box {
width: 897px;

position: relative;
display: inline-block;
}
span.tip {
padding: 3px 6px;
cursor: pointer;
border-radius: 3px;
font-weight: 600;
margin: 1px 2px;
color: #465364;
}
span.tip:hover {
color: #fff;
background: #465364;
}
span.tip.active {
color: #fff;
background: #465364;
}
</style>

3.代码解释

1.两个组件

一个标签组件seltipoption,方便用v-for进行渲染

一个父组件seltiplist管理v-model接收seltipoption事件

2.实现自定义组件v-model数据双向绑定

如果要让组件的v-model生效,则这个组件它必须:

  1. 接收一个value属性。

  2. 在有新的value时触发input事件,直接赋值给this.value是无效的,无法触发更新。

  3. 当为多选项时,更新seltiplist的value数组即可,但是当为单选项时,需要使用this.$emit('input', val)来触发更新

3.父子组件通讯

seltipoption.vue响应click事件后,通过this.dispatch('seltiplist', 'handleOptionClick', this.value)触发父组件seltiplist.vue的监听this.$on('handleOptionSelect')实现组件间的通讯。

4.seltipoption的选择状态判断

  • 当为单选时,判断seltipoptionvalue是都等于seltiplistvalue
  • 当为多选时,seltipoptionvalue存在seltiplistvalue数组中

5.为了判断两个value值是否相等

此处引入工具库underscore.min.js