mpvue小程序vant组件多选

小程序开发常用的组件,多选
我用的是vant组件,但是多选没有我想要的,于是自己写了个。结合van-checkbox和van-popup和can-cell-group还有van-cell写了一个按钮多选式的
html代码

<van-field v-model.trim="recordList.parkingCategoryNameList" required label="停车场类型:" placeholder="请选择停车场类型" readonly is-link="true" @click="showCategoryPopups"/>
        <van-popup
          :show="showCategoryPopup"
          closeable
          position="bottom"
          custom-style="height: 60%;padding:40rpx 20rpx;"
          @close="onCloseCategoryPopup"
        >
          <van-checkbox-group v-model="result" @change="onChangeCategoryList" class="checkbox-info">
            <van-cell-group>
              <van-cell v-for="(item,index) in parkingCategoryArr" :key="index" :title="item.name" :label="item.code" clickable
                        data-index="index" title-width="94%" center>
                <!--                所以在此处赋值的一定要支出对象的具体ID值“itme.id”-->
                <van-checkbox :name="item.id" data-data="item"></van-checkbox>
              </van-cell>
            </van-cell-group>
          </van-checkbox-group>
          <van-button type="info" color="#4bbcff"  class="checkBtn" block @click="onCategoryConfirm">确定</van-button>
        </van-popup>

data数据:

data(){
	return{
		recordList:{
			parkingCategoryList:[],
        	parkingCategoryNameList:[],
		},
		result:[],//用来接收选中后的数据
		parkingCategoryArr:[
        {id:1,name:'地面'},
        {id:2,name:'地下'},
        {id:3,name:'机械'},
        {id:4,name:'立体'},
        {id:5,name:'其他'},
      ],
      showParkingCategory:false,//类型
	}
}

js代码:

//打开弹出层,停车场类型选择
    showCategoryPopups(){
      this.showCategoryPopup = true;
    },
    onCloseCategoryPopup(){
      this.showCategoryPopup = false;
      // this.result = [];
    },
    onChangeCategoryList: function (event) {
      // console.log(event)
      var that = this;
      var array = event.mp.detail;
      // that.setData({
      //   result: array,
      // })
      that.result = array;
      console.log(that.result)
    },
    //确定选中
    onCategoryConfirm(){
      this.showCategoryPopup = false;
      console.log(this.result)
      var that = this;
      that.recordList.parkingCategoryList = that.result;
      that.recordList.parkingCategoryNameList = [];
      that.parkingCategoryArr.forEach(function(i){
          that.result.forEach(function(j){
            if(j == i.id){
              that.recordList.parkingCategoryNameList.push(i.name)
            }
          })
        }
      )
    },

页面效果是这样的:
在这里插入图片描述

暂时没做单击整行的选中状态,有需要补充的,欢迎下方评论区骚扰!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值