账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vant 的picker如何绑定多级联动?
    408
    2

    想使用vant的picker组件绑定多级联动的

    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 3 元积分
    最佳答案01-13 13:31

    我自己写了一下,只不过这种不太通用,就是改变的时候没有让他一级一级往下获取,这个你们可以自行解决
    `<el-button type="primary" @click="show=true">弹窗</el-button>

    <van-popup v-model="show" position="bottom" :overlay="true">
        <van-picker show-toolbar title="标题" :columns="columns" @cancel="onCancel" @confirm="onConfirm" @change="onChange"/>
    </van-popup>`

    `<script>

    var pickerData=[{
        id:1,
        name:'A区',
        child:[{
            id:11,
            name:"1-11列",
            
        },{
            id:12,
            name:"1-12列",
        }]
    },{
        id:2,
        name:'B区',
        child:[{
            id:21,
            name:"2-21列",
        },{
            id:24,
            name:"2-24列",
        }]
    }] var vm = new Vue({
        el: '#app',
        data(){
            return {
                show:false,            address:[],//当前选中的盘点位置
    
                columns:[{
                    values: [],
          className: 'column1'
                },{
          values: [],
          className: 'column2',
          defaultIndex: 0
        }],
            }
        },
        created(){        for(var i in pickerData){            var obj = {
                    text:pickerData[i].name,
                    id:pickerData[i].id
                }
                this.columns[0].values.push(obj)
            }        for(var i in pickerData[0].child){            var obj = {
                    text:pickerData[0].child[i].name,
                    id:pickerData[0].child[i].id
                }
                this.columns[1].values.push(obj)
            }
            
        },
        methods:{
            onCancel(){
                this.show = false
            },
            onConfirm(picker,value,index){
                console.log('onConfirm',picker,value,index)
                this.address=[]            for(var i in picker){
                    this.address.push(picker[i].id)
                }
                this.show = false
            },
            onChange(picker,value,index){
                console.log('onChange',picker,value,index)            if(index == 0){                var arr = []                for(var i in pickerData){                    if(value[0].id == pickerData[i].id){                        for(var x in pickerData[i].child){                            var obj = {
                                    text:pickerData[i].child[x].name,
                                    id:pickerData[i].child[x].id
                                }
                                arr.push(obj)
                            }
                        }
                    }
                    picker.setColumnValues(1,arr)
                }else if(index == 1){
                    
                }
            }
        }
    })

    </script>`


        全部回答
    • 2
    • 苏格拉没有底 普通会员 2020-01-13 13:311楼

      我自己写了一下,只不过这种不太通用,就是改变的时候没有让他一级一级往下获取,这个你们可以自行解决
      `<el-button type="primary" @click="show=true">弹窗</el-button>

      <van-popup v-model="show" position="bottom" :overlay="true">
          <van-picker show-toolbar title="标题" :columns="columns" @cancel="onCancel" @confirm="onConfirm" @change="onChange"/>
      </van-popup>`

      `<script>

      var pickerData=[{
          id:1,
          name:'A区',
          child:[{
              id:11,
              name:"1-11列",
              
          },{
              id:12,
              name:"1-12列",
          }]
      },{
          id:2,
          name:'B区',
          child:[{
              id:21,
              name:"2-21列",
          },{
              id:24,
              name:"2-24列",
          }]
      }] var vm = new Vue({
          el: '#app',
          data(){
              return {
                  show:false,            address:[],//当前选中的盘点位置
      
                  columns:[{
                      values: [],
            className: 'column1'
                  },{
            values: [],
            className: 'column2',
            defaultIndex: 0
          }],
              }
          },
          created(){        for(var i in pickerData){            var obj = {
                      text:pickerData[i].name,
                      id:pickerData[i].id
                  }
                  this.columns[0].values.push(obj)
              }        for(var i in pickerData[0].child){            var obj = {
                      text:pickerData[0].child[i].name,
                      id:pickerData[0].child[i].id
                  }
                  this.columns[1].values.push(obj)
              }
              
          },
          methods:{
              onCancel(){
                  this.show = false
              },
              onConfirm(picker,value,index){
                  console.log('onConfirm',picker,value,index)
                  this.address=[]            for(var i in picker){
                      this.address.push(picker[i].id)
                  }
                  this.show = false
              },
              onChange(picker,value,index){
                  console.log('onChange',picker,value,index)            if(index == 0){                var arr = []                for(var i in pickerData){                    if(value[0].id == pickerData[i].id){                        for(var x in pickerData[i].child){                            var obj = {
                                      text:pickerData[i].child[x].name,
                                      id:pickerData[i].child[x].id
                                  }
                                  arr.push(obj)
                              }
                          }
                      }
                      picker.setColumnValues(1,arr)
                  }else if(index == 1){
                      
                  }
              }
          }
      })

      </script>`


    • 冰蓝无敌 超级管理员 2020-01-13 13:332楼

      最近处理了个二级分类的,处理成官方数据形式来写。
      :columns="columns"
      columns写到计算属性computed里,computed return一个过滤的数据和官方提供的数据格式一致即可。

       onChange(picker, values){    //选择项目操作
          picker.setColumnValues(1, this.arr[values[0]]);
       } 
       onConfirm(value,index){    //确认选择操作
          //value:选择的项目数组,index:选择的项目下标数组,可以利用下标数组操作你自己的对象}


    • 我是阿正啊 普通会员 3楼

      vant 的picker组件提供了多种联动功能,可以通过以下方式绑定多级联动:

      1. 多级联动选项:在picker中,可以设置多级联动选项,每个选项对应不同的类别或者属性。以下是一个简单的示例:

      html <van-picker v-model="selectedCategory" multiple type="dropdown" :options="categoryOptions"> <van-menu-item v-for="(item, index) in selectedCategory" :key="index" :label="item.label" :icon="item.icon" @click="handleItemClick(index)"> {{ item.label }} </van-menu-item> </van-picker>

      在这个示例中,categoryOptions是多个option的数组,每个option的label属性为option的名称,icon属性为option的图标,@click事件处理函数handleItemClick接受index参数,表示当前选中的option的索引,通过item.label获取option的名称,通过item.icon获取option的图标。

      1. 多级联动选择器:在picker的options选项中,可以设置多级联动的选项。例如,可以设置第一个层级的选项为category-1,第二个层级的选项为category-2,以此类推。以下是一个示例:

      html <van-picker v-model="selectedCategory" multiple type="dropdown" :options="categoryOptions" :multiple-level="true"> <van-menu-item v-for="(item, index) in selectedCategory" :key="index" :label="item.label" :icon="item.icon" @click="handleItemClick(index)"> {{ item.label }} </van-menu-item> </van-picker>

      在这个示例中,categoryOptions是多级联动选项的数组,multiple-level参数设置为true,表示默认为多级联动模式,当多个options被选中时,这些options会被分别绑定到多个category-后面,例如category-1category-2等。

      1. 多级联动插槽:在picker的options选项中,可以设置多级联动的插槽。例如,可以设置第一个层级的插槽为category-1,第二个层级的插槽为category-2,以此类推。以下是一个示例:

      html <van-picker v-model="selectedCategory" multiple type="dropdown" :options="categoryOptions" :multiple-level="true"> <van-menu-item v-for="(item, index) in selectedCategory" :key="index" :label="item.label" :icon="item.icon" @click="handleItemClick(index)"> <van-swatch slot="category-{{ index }}"> <template slot="icon">{{ item.icon }}</template> <template slot="label">{{ item.label }}</template> </van-swatch> </van-menu-item> </van-picker>

      在这个示例中,categoryOptions是多级联动选项的数组,multiple-level参数设置为true,表示默认为多级联动模式,当多个options被选中时,这些options会被分别绑定到多个category-后面,每个category-后面有一个<van-swatch>插槽,<van-swatch>中包含两个模板:一个是<template slot="icon">,用于绑定item的图标,另一个是<template slot="label">,用于绑定item的名称。每个item的iconlabel会被分别绑定到这两个模板中,形成多级联动的效果。

      通过上述方式,你可以在vant的picker组件中绑定多级联动,实现不同层级的选项之间的联动效果。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部