這篇文章將為大家詳細講解有關Vue2.x中怎么實現一個條件搜索組件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
組件源碼
<template>
<div class="search">
<el-select v-model="type" @change="changeType" class="select">
<el-option
v-for="item in selectItems"
:key="item.value"
:lable="item.label"
:value="item.value">
</el-option>
</el-select>
<div class='search-input'>
<el-input :placeholder="placeholderDes" v-model="searchValue"></el-input>
</div>
<el-button icon="el-icon-search" @click="search"></el-button>
</div>
</template>
<script>
export default {
data () {
return {
searchValue: '',
type: ''
}
},
created () {
this.type = this.initType
},
props: {
selectItems: {
type: Array,
require: true
},
placeholderDes: {
type: String,
require: true
},
initType: {
type: String,
require: true
}
},
methods: {
changeType (newType) {
this.$emit('changeType', newType)
},
search () {
this.$emit('searchOk', this.searchValue)
}
}
}
</script>
<style lang="less" scoped>
.search {
display: flex;
.el-select {
width: 90px;
height: 40px;
box-sizing: border-box;
border-right: none;
border-radius: 0;
background-color: #DDF0FE;
border: 1px solid #40b0ff;
}
.search-input {
width: 216px;
height: 40px;
border: 1px solid #40b0ff;
border-left: none;
box-sizing: border-box;
font-family: 'MicrosoftYaHei';
font-size: 14px;
color: #909399;
border-radius: 0;
}
.el-button {
width: 44px;
height: 40px;
padding: 0;
border: 1px solid #40b0ff;
border-radius: 0;
color: #fff;
background: #40b0ff;
&:hover {
background: #10b0ff
}
}
}
</style>父組件中的引用
<template>
<div class="test">
<v-search :initType="initType" :selectItems="selectItems" :placeholderDes="placeholderDes" @changeType="changeType" @searchOk="searchOk"></v-search>
</div>
</template>
<script>
import VSearch from '@/components/Common/ZLGComponents/XGQTest/Search/Search'
export default {
data () {
return {
selectItems: [],
selectStatus: 'devname',
initType: '',
placeholderDes: '請輸入要搜索的測試名稱'
}
},
created () {
this.setSelectItems()
this.setInitType()
},
methods: {
setSelectItems () {
this.selectItems = [{
value: '測試名',
label: '測試名'
}, {
value: '測試ID',
label: '測試ID'
}]
},
changeType (newType) {
if (newType === '測試名') {
this.placeholderDes = '請輸入要搜索的測試名稱'
this.selectStatus = 'name'
} else if (newType === '測試ID') {
this.placeholderDes = '請輸入要搜索的測試ID'
this.selectStatus = 'id'
}
},
searchOk (value) {
console.log(this.selectStatus)
console.log(value)
// 調用你的搜索接口,搜索條件為搜索的類型 + 搜索值
// yourSearch (this.selectStatus, value)
},
setInitType () {
this.initType = '測試名'
}
},
components: {
VSearch
}
}
</script>
<style lang="less" scoped>
</style>關于Vue2.x中怎么實現一個條件搜索組件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。