您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Vue2.x中怎么實(shí)現(xiàn)一個(gè)條件搜索組件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
組件源碼
<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: '請(qǐng)輸入要搜索的測(cè)試名稱' } }, created () { this.setSelectItems() this.setInitType() }, methods: { setSelectItems () { this.selectItems = [{ value: '測(cè)試名', label: '測(cè)試名' }, { value: '測(cè)試ID', label: '測(cè)試ID' }] }, changeType (newType) { if (newType === '測(cè)試名') { this.placeholderDes = '請(qǐng)輸入要搜索的測(cè)試名稱' this.selectStatus = 'name' } else if (newType === '測(cè)試ID') { this.placeholderDes = '請(qǐng)輸入要搜索的測(cè)試ID' this.selectStatus = 'id' } }, searchOk (value) { console.log(this.selectStatus) console.log(value) // 調(diào)用你的搜索接口,搜索條件為搜索的類(lèi)型 + 搜索值 // yourSearch (this.selectStatus, value) }, setInitType () { this.initType = '測(cè)試名' } }, components: { VSearch } } </script> <style lang="less" scoped> </style>
關(guān)于Vue2.x中怎么實(shí)現(xiàn)一個(gè)條件搜索組件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。