uni-app + vant 实现可搜索的popup

使用场景:

        产品要求需要下拉选择,并且可以搜索对应的值,针对移动端没有类似的案例,因此vant+uni-app相结合,实现了可搜索的popup,具体代码如下:

dom解构

<template>
  <!-- uni-app结合vant组件库,实现可搜索的弹层,只能单选 -->
  <view class="popup-vant-select" @click.prevent="handleOpen">
    <!-- :class="{ open: popupOpenFlag, clear: (props.clear && selectLabel) }" -->
    <text
      class="icon"
      :class="{ open: popupOpenFlag, clear: props.clear && selectLabel }"
      @click.stop="handleClear"
    ></text>
    <!-- 下拉框中显示默认的值 -->
    <view v-if="!selectLabel" class="placeholder">{
  { props.placeholder }}</view>
    <!-- 下拉框中显示选择的值 -->
    <view v-else>{
  { selectLabel }}</view>
    <uni-popup ref="popupRef" type="bottom" background-color="#fff" :is-mask-click="false">
      <view class="select-box">
        <view v-if="props.title" class="title">这里可以设置标题</view>
        <view class="btn-box">
          <text class="cancel" @click="handleCancel">取消</text>
          <text class="confirm" @click="handleConfirm">确定</text>
        </view>
        <CommonSearch
          v-if="props.filterable"
          @input="hanndleInput"
          placeholder="请输入"
          background="#fff"
        />
        <!-- option-height:选项高度;visible-option-num:可见的选项个数 -->
        <Picker
          :show-toolbar="false"
          v-model="selectValue"
          :columns="list"
          option-height="40rpx"
          visible-option-num="4"
          :columns-fi
### 集成Vant UI库到Uni-app项目 为了在uni-app项目中集成并使用Vant的sidebar组件,需遵循特定步骤来确保兼容性和功能性。 #### 安装依赖包 首先,在命令行工具中进入项目的根目录执行npm安装命令以引入Vant库: ```bash npm install @vant/weapp -S --production ``` 此操作会下载必要的文件至`node_modules`文件夹内[^1]。 #### 引入组件 接着修改`pages.json`配置文件,注册所需使用的Vant组件。对于sidebar及其关联项而言,则添加如下代码片段于适当位置: ```json { "usingComponents": { "van-sidebar": "@vant/weapp/sidebar/index", "van-sidebar-item": "@vant/weapp/sidebar-item/index" } } ``` 上述设置使得可以在页面模板里通过自定义标签形式调用这些UI部件[^2]。 #### 编写页面结构与样式 创建一个新的`.vue`文件作为承载sidebar逻辑和平铺内容展示区域的基础单元。在此处利用之前声明过的组件标签构建布局框架,并绑定相应的数据模型属性给它们控制显示状态变化。 ```html <template> <view class="container"> <!-- 左侧导航栏 --> <van-sidebar v-model="activeKey" @change="onChange"> <van-sidebar-item title="选项一"/> <van-sidebar-item title="选项二"/> <van-sidebar-item title="选项三"/> </van-sidebar> <!-- 右侧主要内容区 --> <view :class="{hideSidebar:!opened,openSidebar:opened}"> 主要内容... </view> </view> </template> <script> export default { data(){ return { activeKey:0, opened:true }; }, methods:{ onChange(event){ console.log('切换到了第', event.detail,'页'); // 更新打开关闭标志位 this.opened=!this.opened; } } }; </script> <style scoped lang="scss"> .container{ display:flex; .hideSidebar{ /* 当隐藏时 */ transform:translateX(-75%); } .openSidebar{/* 显示状态下 */ transition:.3s ease-out; } } </style> ``` 这段示例展示了如何组合运用Vue语法特性以及CSS类名动态切换机制达成交互效果,同时保持良好的用户体验[^3]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值