詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選

select 下拉選擇
產(chǎn)品類型:這一項(xiàng)是select 涉及到父子組件信息傳遞 下面拆開講解
父組件
<div class="sales-board-line">
<div class="sales-board-line-left">
產(chǎn)品類型:
</div>
<div class="sales-board-line-right">
<v-selection :selections="buyTypes" @on-change="onParamChange('buyType', $event)"></v-selection>
</div>
</div>
<script>
import VSelection from '../../components/base/selection'
import _ from 'lodash'
export default {
components: {
VSelection,
VCounter,
VChooser,
VMulChooser,
MyDialog: Dialog,
BankChooser,
CheckOrder
},
data () {
return {
buyNum: 0,
buyType: {},
versions: [],
period: {},
price: 0,
versionList: [
{
label: '客戶版',
value: 0
},
{
label: '代理商版',
value: 1
},
{
label: '專家版',
value: 2
}
],
periodList: [
{
label: '半年',
value: 0
},
{
label: '一年',
value: 1
},
{
label: '三年',
value: 2
}
],
buyTypes: [
{
label: '入門版',
value: 0
},
{
label: '中級(jí)版',
value: 1
},
{
label: '高級(jí)版',
value: 2
}
],
isShowPayDialog: false,
bankId: null,
orderId: null,
isShowCheckOrder: false,
isShowErrDialog: false
}
},
methods: {
onParamChange (attr, val) {
this[attr] = val
// this.getPrice()
console.log(this[attr], attr)
},
getPrice () {
let buyVersionsArray = _.map(this.versions, (item) => {
return item.value
})
let reqParams = {
buyNumber: this.buyNum,
buyType: this.buyType.value,
period: this.period.value,
version: buyVersionsArray.join(',')
}
this.$http.post('/api/getPrice', reqParams)
.then((res) => {
this.price = res.data.amount
})
},
onChangeBanks (bankObj) {
this.bankId = bankObj.id
},
confirmBuy () {
let buyVersionsArray = _.map(this.versions, (item) => {
return item.value
})
let reqParams = {
buyNumber: this.buyNum,
buyType: this.buyType.value,
period: this.period.value,
version: buyVersionsArray.join(','),
bankId: this.bankId
}
this.$http.post('/api/createOrder', reqParams)
.then((res) => {
this.orderId = res.data.orderId
this.isShowCheckOrder = true
this.isShowPayDialog = false
}, (err) => {
this.isShowBuyDialog = false
this.isShowErrDialog = true
})
}
},
mounted () {
this.buyNum = 1
this.buyType = this.buyTypes[0]
this.versions = [this.versionList[0]]
this.period = this.periodList[0]
}
}
</script>
:selections=”buyTypes” 傳入子組件 在子組件 接收這個(gè)參數(shù)
@on-change=”onParamChange(‘buyType', $event)” 通過(guò)這個(gè)事件 接收 子組件傳入 的參數(shù)
子組件
<template>
<div class="selection-component">
<div class="selection-show" @click="toggleDrop">
<span>{{ selections[nowIndex].label }}</span>
<div class="arrow"></div>
</div>
<div class="selection-list" v-if="isDrop">
<ul>
<li v-for="(item, index) in selections" @click="chooseSelection(index)">{{ item.label }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
props: {
selections: {
type: Array,
default: [{
label: 'test',
value: 0
}]
}
},
data () {
return {
isDrop: false,
nowIndex: 0
}
},
methods: {
toggleDrop () {
this.isDrop = !this.isDrop
},
chooseSelection (index) {
this.nowIndex = index
this.isDrop = false
this.$emit('on-change', this.selections[this.nowIndex])
}
}
}
</script>
<style scoped>
.selection-component {
position: relative;
display: inline-block;
}
.selection-show {
border: 1px solid #e3e3e3;
padding: 0 20px 0 10px;
display: inline-block;
position: relative;
cursor: pointer;
height: 25px;
line-height: 25px;
border-radius: 3px;
background: #fff;
}
.selection-show .arrow {
display: inline-block;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 5px solid #e3e3e3;
width: 0;
height: 0;
margin-top: -1px;
margin-left: 6px;
margin-right: -14px;
vertical-align: middle;
}
.selection-list {
display: inline-block;
position: absolute;
left: 0;
top: 25px;
width: 100%;
background: #fff;
border-top: 1px solid #e3e3e3;
border-bottom: 1px solid #e3e3e3;
z-index: 5;
}
.selection-list li {
padding: 5px 15px 5px 10px;
border-left: 1px solid #e3e3e3;
border-right: 1px solid #e3e3e3;
cursor: pointer;
background: #fff;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.selection-list li:hover {
background: #e3e3e3;
}
</style>
select 多選
產(chǎn)品版本:這一項(xiàng)是select 涉及到父子組件信息傳遞 下面拆開講解
父組件
<div class="sales-board-line">
<div class="sales-board-line-left">
產(chǎn)品版本:
</div>
<div class="sales-board-line-right">
<v-mul-chooser
:selections="versionList"
@on-change="onParamChange('versions', $event)"></v-mul-chooser>
</div>
</div>
子組件
<template>
<div class="chooser-component">
<ul class="chooser-list">
<li
v-for="(item, index) in selections"
@click="toggleSelection(index)"
:title="item.label"
:class="{active: checkActive(index)}"
>{{ item.label }}</li>
</ul>
</div>
</div>
</template>
<script>
import _ from 'lodash'
export default {
props: {
selections: {
type: Array,
default: [{
label: 'test',
value: 0
}]
}
},
data () {
return {
nowIndexes: [0]
}
},
methods: {
toggleSelection (index) {
if (this.nowIndexes.indexOf(index) === -1) {
this.nowIndexes.push(index)
}
else {
this.nowIndexes = _.remove(this.nowIndexes, (idx) => {
return idx !== index
})
}
let nowObjArray = _.map(this.nowIndexes, (idx) => {
return this.selections[idx]
})
this.$emit('on-change', nowObjArray)
},
checkActive (index) {
return this.nowIndexes.indexOf(index) !== -1
}
}
}
</script>
<style scoped>
.chooser-component {
position: relative;
display: inline-block;
}
.chooser-list li{
display: inline-block;
border: 1px solid #e3e3e3;
height: 25px;
line-height: 25px;
padding: 0 8px;
margin-right: 5px;
border-radius: 3px;
text-align: center;
cursor: pointer;
}
.chooser-list li.active {
border-color: #4fc08d;
background: #4fc08d;
color: #fff;
}
</style>
這里用到 lodash 因?yàn)関uejs2.0 放棄了$.remove 方法 可以通過(guò)lodash 方法解決
以上所述是小編給大家介紹的vuejs2.0 select動(dòng)態(tài)綁定下拉框詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue利用vue-baidu-map實(shí)現(xiàn)獲取經(jīng)緯度和搜索地址
在開發(fā)項(xiàng)目的時(shí)候,發(fā)現(xiàn)需要獲取經(jīng)緯度,由于這個(gè)項(xiàng)目是用vue寫的,最后決定使用vue-baidu-map來(lái)快速獲取經(jīng)緯度,感興趣的可以了解一下2022-09-09
關(guān)于vue.js過(guò)渡css類名的理解(推薦)
這篇文章主要介紹了關(guān)于vue.js過(guò)渡css類名的理解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
vue & vue Cli 版本及對(duì)應(yīng)關(guān)系解讀
這篇文章主要介紹了vue & vue Cli 版本及對(duì)應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue中集成省市區(qū)街四級(jí)地址組件的實(shí)現(xiàn)過(guò)程
我們?cè)陂_發(fā)中常會(huì)遇到選擇地址的需求,有時(shí)候只需要選擇省就可以,有時(shí)候則需要選擇到市、縣,以至于鄉(xiāng)鎮(zhèn),甚至哪個(gè)村都有可能,下面這篇文章主要給大家介紹了關(guān)于vue中集成省市區(qū)街四級(jí)地址組件的相關(guān)資料,需要的朋友可以參考下2022-12-12

