溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在小程序中自定義一個(gè)多層級(jí)單選和多選功能

發(fā)布時(shí)間:2021-04-21 15:50:13 來(lái)源:億速云 閱讀:401 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹怎么在小程序中自定義一個(gè)多層級(jí)單選和多選功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

wxml

<view class="select-box">
    <view class="select-title">
        <view class="cell-border">
            <van-field value="{{ layout }}" data-key="layout" placeholder="請(qǐng)輸入" required icon="arrow" label="戶(hù)型" 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陽(yáng)臺(tái)",key:0,},
                    {name:"2陽(yáng)臺(tái)",key:1,},
                    {name:"3陽(yáng)臺(tái)",key:2,},
                    {name:"4陽(yáng)臺(tái)",key:3,},
                    {name:"5陽(yáng)臺(tái)",key:4,},
                    {name:"6陽(yáng)臺(tái)",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})
        }
    }
})

關(guān)于怎么在小程序中自定義一個(gè)多層級(jí)單選和多選功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI