小程序?qū)崿F(xiàn)自定義多層級單選和多選
更新時間:2021年04月21日 11:29:36 作者:大灰狼吃小白兔
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)自定義多層級單選和多選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了小程序?qū)崿F(xiàn)自定義多層級單選和多選的具體代碼,供大家參考,具體內(nèi)容如下
效果:
ps:這兒是用自定義的下拉框,我把它封裝成了一個組件

wxml
<view class="select-box">
<view class="select-title">
<view class="cell-border">
<van-field value="{{ layout }}" data-key="layout" placeholder="請輸入" required icon="arrow" label="戶型" bind:tap="onChange" />
</view>
</view>
<view class="select-list" wx:if="{{show}}">
<view class="option" wx:for="{{layouts}}" wx:key="index">
<view class="{{curItem.checked ? 'option-item-active' : 'option-item'}}"
wx:for="{{item.column}}" wx:key="index"
wx:for-item="curItem"
data-key="{{curItem.key}}"
data-colkey="{{item.colKey}}"
data-name="{{curItem.name}}"
bind:tap="getOptionItem">
{{curItem.name}}
</view>
</view>
</view>
</view>
wxss
.select-box{
width: 100%;
padding: 20rpx;
box-sizing: border-box;
}
.cell-border {
border-radius: 6rpx;
border: 1px solid #999;
margin-bottom: 10rpx;
}
.select-list{
display: flex;
flex-direction: row;
justify-content: space-around;
width: 100%;
height: 360rpx;
padding: 20rpx;
box-sizing: border-box;
background-color: #fff;
border: 1px solid #eee;
}
.select-list .option{
display: flex;
flex-direction: column;
font-size: 24rpx;
}
.option-item{
width: 80rpx;
height: 100rpx;
background-color: #eee;
text-align: center;
margin-top: 5px;
padding: 2px;
}
.option-item-active{
width: 80rpx;
height: 100rpx;
background-color: #FF6600;
text-align: center;
margin-top: 5px;
padding: 2px;
color:#fff;
}
json
{
"component": true,
"usingComponents": {
"van-field": "../../vant/field/index",
}
}
js
ps:data是組件本身的數(shù)據(jù),layouts是數(shù)據(jù)源
Component({
properties:{
},
data:{
show:false,
curKey:-1,
colKey:-1,
layouts:[
{
colKey:0,
column:[
{name:"1室",key:0,},
{name:"2室",key:1,},
{name:"3室",key:2,},
{name:"4室",key:3,},
{name:"5室",key:4,},
{name:"6室",key:5,} ]
},
{
colKey:1,
column:[
{name:"1廳",key:0,},
{name:"2廳",key:1,},
{name:"3廳",key:2,},
{name:"4廳",key:3,},
{name:"5廳",key:4,},
{name:"6廳",key:5,} ]
},
{
colKey:2,
column:[
{name:"1廚",key:0,},
{name:"2廚",key:1,},
{name:"3廚",key:2,},
{name:"4廚",key:3,},
{name:"5廚",key:4,},
{name:"6廚",key:5,}]
},
{
colKey:3,
column:[
{name:"1衛(wèi)",key:0,},
{name:"2衛(wèi)",key:1,},
{name:"3衛(wèi)",key:2,},
{name:"4衛(wèi)",key:3,},
{name:"5衛(wèi)",key:4,},
{name:"6衛(wèi)",key:5,}
]
},
{
colKey:4,
column:[
{name:"1陽臺",key:0,},
{name:"2陽臺",key:1,},
{name:"3陽臺",key:2,},
{name:"4陽臺",key:3,},
{name:"5陽臺",key:4,},
{name:"6陽臺",key:5,}
]
}
]
},
methods:{
onChange(){
const {show} = this.data;
this.setData({
show:!show
})
},
getOptionItem(event){
console.log("event",event)
const key = event.currentTarget.dataset.key;
const cK = event.currentTarget.dataset.colkey;
const {curKey,colKey,layouts} = this.data;
this.setData({
curKey:key,
colKey:cK
})
//用checked字段判斷,允許每列之間單選,多行之間多選
layouts[cK].column.map(cur => {
return cur.checked = false;
})
layouts[cK].column[key].checked = true;
this.setData({layouts})
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
BootStrap glyphicons 字體圖標(biāo)實(shí)現(xiàn)方法
字體圖標(biāo)是在 Web 項(xiàng)目中使用的圖標(biāo)字體。接下來通過本文給大家介紹BootStrap glyphicons 字體圖標(biāo)實(shí)現(xiàn)方法,需要的朋友參考下2016-05-05
解決Js先觸發(fā)失去焦點(diǎn)事件再執(zhí)行點(diǎn)擊事件的問題
今天小編就為大家分享一篇解決Js先觸發(fā)失去焦點(diǎn)事件再執(zhí)行點(diǎn)擊事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
BootStrap智能表單實(shí)戰(zhàn)系列(九)表單圖片上傳的支持
這篇文章主要介紹了BootStrap智能表單實(shí)戰(zhàn)系列(九)表單圖片上傳的支持,介紹如何在生成表單后,可以支持上傳圖片后可以及時預(yù)覽圖片的功能,需要的朋友可以參考下2016-06-06

