searchCom.vue
3.12 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
<template>
<div class="search-com" :style="'background-color: ' + defaultSearchs.backgroundColor">
<div class="box">
<div class="search-group" :style="'border-color: ' + defaultSearchs.borderColor">
<div class="left-box">
<form class="search-form" action="" onsubmit="return false;">
<div class="search-icon">
<img src="static/img/icon_search.png" class="search-img" />
</div>
<div class="line"></div>
<div class="input-box">
<input type="search" ref="searchInput" :placeholder="defaultSearchs.pl" :maxlength="defaultSearchs.maxlength" v-model="defaultSearchs.keyword" class="keyword" @keyup="confirm(defaultSearchs.keyword, $event)" @blur="onBlur" />
</div>
</form>
</div>
<div class="right-box">
<div class="clear-input-box" v-if="defaultSearchs.keyword" @click="clearInput">
<img src="static/img/icon_clear_input.png" class="clear-input-img" />
</div>
</div>
</div>
<div class="buttons-box">
<div class="btn btn-primary" :style="'color: ' + defaultSearchs.borderColor" @click="cancel">{{defaultSearchs.btnText}}</div>
</div>
</div>
</div>
</template>
<script>
export default {
data: function () {
return {
defaultSearchs: {
backgroundColor: "transparent",
keyword: "",
maxlength: 20,
borderColor: "#F9F9F9",
pl: "请输入搜索内容",
btnText: "取消"
}
}
},
props: ['searchs'],
methods: {
/**
* 获取搜索关键字
*/
getKeyword() {
return this.defaultSearchs.keyword;
},
/**
* 设置搜索关键字
* @param {String} keyword [搜索关键字]
*/
setKeyword(keyword) {
this.$set(this.defaultSearchs, "keyword", keyword);
},
/**
* 搜索
* @param {string} keyword [搜索关键字]
* @param {object} e [事件对象]
*/
confirm(keyword, e) {
// 判断按下的按键是不是回车(确认)
if ((keyword && e.keyCode === 13) || (!keyword && e.keyCode === 8)) {
// 判断不是空格字符串
if (keyword.trim()) {
this.$emit("confirm", keyword);
} else {
this.setKeyword('');
}
}
},
/**
* 清除input信息
*/
clearInput() {
// 清除input内容
this.$set(this.defaultSearchs, "keyword", "");
// 设置清除input事件
this.$emit("clearInput");
},
/**
* 取消按钮
*/
cancel() {
this.$emit("cancel");
},
/**
* 失去焦点处理
*/
onBlur() {
// 判断是否有onBlur函数
if (typeof this.$parent.onBlur === "function") {
this.$parent.onBlur();
}
}
},
mounted() {
// 合并参数对象
Object.assign(this.defaultSearchs, this.searchs);
// 延时执行
setTimeout(() => {
this.$nextTick(() => {
if (typeof this.$refs === 'object' && typeof this.$refs.searchInput === 'object') {
this.$refs.searchInput.focus();
}
});
}, 1000);
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less" scoped>
@import './searchCom';
</style>