vant+checkbox+搜索:多选出现点击错乱

需求:用户点击出现弹框,在弹框里面可以进行多个项目的选择,也可搜索选择项目。

出现问题:在搜索之后选择项目出现点击错乱的问题,点击弹框不在搜索的情况下则不会有这个问题。

错误原因:由于checkbox是根据index来进行选择,在搜索的情况下,重新获取列表,导致index错乱导致。

代码:

    <van-popup v-model="showSiteList" position="bottom" :style="{ height:'50%' }" >
      <div class="select" v-if="!showSearch">
        <div class="search" @click="showSearch=true">搜索</div>
        <div class="selectPro">选择</div>
        <div class="confirm" @click="comfirmSearch">确定</div>
      </div>
      <div v-else class="pop-search van-hairline--bottom">
          <van-field
            v-model="searchvValue"
            placeholder="搜索"
            :formatter="formatter"
            :border="false"
            name="search"
            autocomplete="off"
            class="mot-input"
            clearable
            @input="
              e => {
                onSearch(e)
              }
            "
            maxlength="20"
          >
            <template #extra>
              <span
                style="padding: 0 10px;color: #1677FF;font-size: 15px; line-height: 32px"
                @click="comfirmSearch"
                >确定</span
              >
            </template>
          </van-field>
      </div>
      <van-checkbox-group v-model="result" v-if="siteList.length > 0" @change="selectChange">
        <van-cell-group>
          <van-cell
            v-for="(item, index) in siteList"
            clickable
            :key="item.id"
            :title="item.title"
            @click="toggle(item.id)"
          >
            <template #right-icon>
              <van-checkbox :name="item.id" ref="checkboxes"/>
            </template>
          </van-cell>
        </van-cell-group>
      </van-checkbox-group>
      <div v-else>
        <div v-if="isfinished">
          <div class="no_data">暂无数据</div>
        </div>
        <div v-else class="no_data">
          <van-loading/>
        </div>
      </div>
    </van-popup>
    toggle(id,item) {
      // 解决方案
      if(this.result.includes(id)){
        let index = this.result.findIndex(item => item == id)
        this.result.splice(index, 1)
      }else {
        this.result.push(id)
      }

      // this.$refs.checkboxes[index].toggle(); 出现问题代码
    },

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值