uniapp select选择器

<template>
  <view class="aaa">
    <u-form-item required label="性别" labelWidth="150rpx">
      <text @click="show = true" :class="value == '请选择' ? 'hui' : ''">
        {{ value }}</text
      >
      <u-select
        v-model="show"
        mode="single-column"
        :list="list"
        @confirm="confirm"
        label-name="label"
      ></u-select>
    </u-form-item>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {
          value: "1",
          label: "江",
        },
        {
          value: "2",
          label: "湖",
        },
      ],
      show: false,
      value: "请选择",
    };
  },
  onShow() {},
  methods: {
    confirm(e) {
      console.log(e);
      this.value = e[0].value;
    },
  },
};
</script>

<style lang="scss" scoped>
.aaa {
}
.hui {
  color: rgb(192, 196, 204);
}
</style>
<style>
page {
  /* background-color: #f7f7f7; */
}
.biaodan .u-border {
  border: 1px solid transparent !important;
}
.biaodan .u-form-item {
  border-bottom: 1px solid #f7f7f7;
  padding: 5px 15px !important;
}

.biaodan .u-input {
  padding: 0 !important;
  font-size: 14px !important;
}
.u-input__content__field-wrapper__field {
}
</style>

fengzhuang

     <xyselect :form="form" @confirm2="confirm2"></xyselect>
     
     relationship: "",
        relationship_text: "",

    confirm2(name, id) {
      console.log("asdfasdf", 11111);

      this.form.relationship = id;
      this.form.relationship_text = name;
    },
<template>
  <view class="aaa">
    <u-form-item required label="成员关系" labelWidth="150rpx">
      <text @click="show = true" :class="form.relationship_text ? '' : 'hui'">
        {{ form.relationship_text ? form.relationship_text : "请选择" }}</text
      >
      <u-select
        v-model="show"
        mode="single-column"
        :list="list"
        @confirm="confirm"
        label-name="label"
      ></u-select>
    </u-form-item>
  </view>
</template>

<script>
export default {
  props: ["form"],
  data() {
    return {
      list: [
        {
          value: 0,
          label: "朋友",
        },
        {
          value: 1,
          label: "父亲",
        },
        {
          value: 2,
          label: "母亲",
        },
        {
          value: 3,
          label: "儿子",
        },
        {
          value: 14,
          label: "女儿",
        },
        {
          value: 15,
          label: "妻子",
        },
        {
          value: 16,
          label: "丈夫",
        },
        {
          value: 17,
          label: "其他",
        },
      ],
      show: false,
      value: "请选择",
      value2: "",
    };
  },
  onShow() {},
  methods: {
    confirm(e) {
      console.log(e);
      this.$emit("confirm2", e[0].label, e[0].value);
    },
  },
};
</script>

<style lang="scss" scoped>
.aaa {
}
.hui {
  color: rgb(192, 196, 204);
}
</style>
<style>
page {
  /* background-color: #f7f7f7; */
}
.biaodan .u-border {
  border: 1px solid transparent !important;
}
.biaodan .u-form-item {
  border-bottom: 1px solid #f7f7f7;
  padding: 5px 15px !important;
}

.biaodan .u-input {
  padding: 0 !important;
  font-size: 14px !important;
}
.u-input__content__field-wrapper__field {
}
</style>

### 实现 UniAppSelect 下拉单选选择器UniApp 中实现一个功能完善的 `Select` 下拉单选选择器可以通过自定义组件来完成。这不仅能满足特定的业务需求,还能提供更好的用户体验。 #### 组件结构设计 为了构建一个高效的下拉单选选择器,可以采用 Vue 的组合式 API 和 TypeScript 来增强代码可读性和维护性[^2]: ```html <template> <view class="select-container"> <!-- 显示当前选项 --> <view @click="toggleDropdown()" :class="{ 'is-open': isOpen }">{{ selectedOption || placeholder }}</view> <!-- 下拉列表容器 --> <view v-if="isOpen" class="dropdown-list"> <view v-for="(option, index) in options" :key="index" @click="onSelect(option)" :class="{ 'selected-item': option === selectedOption }"> {{ option }} </view> </view> </view> </template> ``` #### 脚本逻辑编写 通过脚本来控制显示状态以及处理用户交互行为: ```javascript <script setup lang="ts"> import { ref, watch } from "vue"; // 定义 props 接收外部传入的数据 const props = defineProps({ modelValue: String, options: Array<string>, placeholder: { type: String, default: "请选择..." } }); let isOpen = ref(false); let selectedOption = ref(props.modelValue); watch(() => props.modelValue, (newValue) => { selectedOption.value = newValue; }, { immediate: true }); function toggleDropdown() { isOpen.value = !isOpen.value; } function onSelect(option: string) { selectedOption.value = option; emit('update:modelValue', option); // 更新父级绑定值 isOpen.value = false; // 关闭菜单 } </script> ``` #### 样式美化 为了让组件看起来更美观,还可以为其添加一些基础样式: ```css <style scoped> .select-container { position: relative; width: 100%; } .dropdown-list { border: 1px solid #ccc; max-height: 200px; overflow-y: auto; background-color: white; z-index: 999; } .selected-item { color: blue; } .is-open::after { content: "\25BC"; /* 向下的箭头 */ margin-left: 8px; } </style> ``` #### 使用说明 当集成此组件到页面时,只需按照如下方式进行调用即可: ```html <SelectComponent v-model="selectedItem" :options="['苹果','香蕉','橙子']"/> ``` 在此基础上可以根据实际项目调整和完善更多细节特性,比如增加搜索过滤等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值