vue,uniapp,js手写单选和多选效果

单选

您将看到以下效果 思路是给要设置的元素绑定一个值,并判断这个值是不是和我们赋予(传参)的值相同(使用uview组件 您可以尝试换成class等)

单选

<template>
  <view>
	  <u-button @click="setClass(1)" :type="setClassIndex==1?'primary':'default'">设置1</u-button>
	  <u-button @click="setClass(2)" :type="setClassIndex==2?'primary':'default'">设置2</u-button>
	  <u-button @click="setClass(3)" :type="setClassIndex==3?'primary':'default'">设置3</u-button>
	  <u-button @click="setClass(4)" :type="setClassIndex==4?'primary':'default'">设置4</u-button>
	  <u-button @click="setClass(5)" :type="setClassIndex==5?'primary':'default'">设置5</u-button>
  </view>
</template>
<script>
export default {
	name:'index',
	data() {
		return {
			setClassIndex:1,
		};
	},
	methods:{
		setClass(index){
			this.setClassIndex = index
		}
	}
}
</script>
<style lang="scss">
</style>

多选

思路同单选略有不同,主要是判断我们给的值在已知列表中是否存着,不存在就添加,存着反而取消。效果如下

在这里插入图片描述

<template>
  <view>
	  <u-button @click="setClass(1)" :type="setList.includes(1)?'primary':'default'">设置1</u-button>
	  <u-button @click="setClass(2)" :type="setList.includes(2)?'primary':'default'">设置2</u-button>
	  <u-button @click="setClass(3)" :type="setList.includes(3)?'primary':'default'">设置3</u-button>
	  <u-button @click="setClass(4)" :type="setList.includes(4)?'primary':'default'">设置4</u-button>
	  <u-button @click="setClass(5)" :type="setList.includes(5)?'primary':'default'">设置5</u-button>
  </view>
</template>
<script>
export default {
	name:'index',
	data() {
		return {
			setList:[],
		};
	},
	methods:{
		setClass(index){
			if(this.setList.includes(index)){ //es6 判断是否包含
				this.setList=this.setList.filter(function (item){return item !== index;});
			}else{
				this.setList.push(index)
			}
		}
	}
}
</script>
<style lang="scss">
</style>

很简单的一个demo,学习思路为主,cv代码为辅

### UniApp 下拉择框实现单选功能 #### 使用 `picker` 组件实现下拉择框的单选功能 在 UniApp 中,可以通过内置的 `picker` 组件来轻松实现下拉择框的功能。对于单选模式,配置相对简单。 ```html <template> <view> <!-- 单选 --> <picker mode="selector" :range="array" @change="bindPickerChange"> <view class="picker"> 当前择:{{ array[index] }} </view> </picker> </view> </template> <script> export default { data() { return { array: ['项1', '项2', '项3'], index: 0, }; }, methods: { bindPickerChange(e) { this.index = e.detail.value; } } }; </script> ``` 此代码展示了如何通过 `picker` 组件设置一个简单的单选下拉菜单[^1]。 #### 实现带有功能的择器 为了支持操作,在 UniApp 中通常会采用更复杂的逻辑处理方式。下面是一个带有底部弹出样式并允许用户进行的例子: ```html <template> <view> <!-- 显示已中的项 --> <view v-for="(item, idx) in selectedItems" :key="idx">{{ item }}</view> <!-- 打开对话框按钮 --> <button type="default" @click="showMultiSelector">打开</button> <!-- 底部弹出层用于展示所有可项 --> <uni-popup ref="popup" type="bottom"> <scroll-view scroll-y style="height: 300px;"> <checkbox-group @change="multiSelectChange"> <label v-for="(option, i) in options" :key="i"> <checkbox :value="option" :checked="selectedItems.includes(option)" /> {{ option }} </label> </checkbox-group> </scroll-view> <button @click="confirmSelection">确认</button> </uni-popup> </view> </template> <script> import uniPopup from '@/components/uni-popup/uni-popup.vue'; export default { components: { uniPopup }, data() { return { options: ['苹果', '香蕉', '橙子'], // 可供择的数据列表 selectedItems: [], // 用户已经择了哪些项目 }; }, methods: { showMultiSelector() { this.$refs.popup.open(); }, multiSelectChange(event) { const values = event.detail.value; this.selectedItems = [...values]; }, confirmSelection() { console.log('最终定:', this.selectedItems); this.$refs.popup.close(); } } }; </script> ``` 这段代码实现了当用户点击“打开”按钮时,从页面下方滑动出来的窗口中可以个条目,并且这些被中的条目会在界面上实时更新显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值