uniapp制作app与小程序前端——标签

该文章展示了一个使用Vue.js框架编写的多选标签组件。通过循环遍历数据并绑定点击事件,实现标签的选中与取消选中状态切换。选中的标签会显示在单独的区域。样式通过CSS进行定制,包括已选和未选中的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、写入代码

<template>
	<view class="">
		<view class="page index">
		        <!-- 多选,循环遍历填充数据 -->
		        <view class="list-box">
		            <view v-for="(item,index) in list" :key="index" @click="choice(index)" :class="[item.selected?'selde':'noselde']">
		                {{item.selected?item.title:item.title}}
		            </view>
		        </view>
				<!-- 选中的值 -->
				<view class="valueList">
					{{selectList}}
				</view>
		    </view>
	</view>
</template>

<script>
    export default {
        data() {
            return {
				 //定义前端选中的值
				selectList:"",
				//给标签赋值
                list: [{
                        selected: false,
                        title: '标签1'
                    },
                    {
                        selected: false,
                        title: '标签2'
                    },
                    {
                        selected: false,
                        title: '标签3'
                    },
                    {
                        selected: false,
                        title: '标签4'
                    },
                    {
                        selected: false,
                        title: '标签5'
                    }, {
                        selected: false,
                        title: '标签6'
                    }
                ],
                selectId: [],
            };
        },
        methods: {
            //选择按钮
            choice(index) {
				//当再次被选中时,取消当前选中项
                if (this.list[index].selected == true) {
                    this.list[index].selected = false;
                    //取消选中时删除数组中的值
                    for (var i = 0; i < this.selectId.length; i++) {
                        if (this.selectId[i] === this.list[index].title) {
                            this.selectId.splice(i, 1);
							
                        }
                    }
					this.selectList=this.selectId
                    console.log("选中的值", this.selectId)
                } else {
                    this.list[index].selected = true;
                    this.selectId.push(this.list[index].title)
					for (var i=0;i<this.selectId.length;i++) {
						console.log("选中的值", this.selectId[i])
					}
					this.selectList=this.selectId
                    console.log("选中的值", this.selectId)
                }
            }
        }
    };
</script>

<!-- 样式表 -->
<style lang="scss">
    .list-box {
        display: flex;
        flex-wrap: wrap;
        padding: 16upx;
        border-radius: 10upx;

        view {
            width: 25%;
            height: 60upx;
            line-height: 60upx;
            text-align: center;
            margin-top: 10upx;

            &:not(:nth-child(3n)) {
                margin-right: calc(10% / 2);
            }
        }
    }

    /* 已选择 */
    .selde {
        border: 1px solid red;
        background: red;
        color: #FFFFFF;
        border-radius: 20upx;
        font-size: 20upx;
        padding: 0 10upx;
    }

    /* 未选择 */
    .noselde {
        border: 1px solid #959595;
        background: #FFFFFF;
        color: #959595;
        border-radius: 20upx;
        font-size: 20upx;
        padding: 0 10upx;
    }
	.valueList{
		margin-top: 20rpx;
		padding: 20rpx;
		display: flex;
		justify-content: center;
	}
</style>

二、效果图

在这里插入图片描述
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值