资讯专栏INFORMATION COLUMN

表单选项互斥问题(vue)

shiyang6017 / 2568人阅读

摘要:最近有一个需求表单中有个多选框,而他们的选项是一样的,但是如果其中一个选项被选择之后,在另外个多选框里面就不能再选了。上代码乒乓球放入不同的盒子选项互斥每个对象都有一个属性,用来表示它属于哪个盒子。

最近有一个需求:
表单中有3个多选框,而他们的选项是一样的,但是如果其中一个选项被选择之后,在另外2个多选框里面就不能再选了。
这样的问题,让我想到了“将乒乓球放入不同盒子”的例子。

上代码

// index.html



    
        
        
        
        Document

        
        
    
    
        

乒乓球放入不同的盒子(选项互斥)

// main.js

const balls = Array(12)
    .fill(undefined)
    .map((v, i) => ({ id: i < 9 ? `0${i + 1}` : i + 1, category: null }))

const vm = new window.Vue({
    el: "#app",

    data: () => ({
        msg: "hahahhaha",
        balls,
        categories: ["red", "green", "blue"],
        currCategory: "red"
    }),

    computed: {
        ballList() {
            return this.balls.map(v => {
                let className = ["ping-pong-ball"]
                if (v.category) {
                    className.push(v.category)
                    if (v.category !== this.currCategory) {
                        className.push("disable")
                    }
                }
                return { ...v, className }
            })
        }
    },

    methods: {
        onBoxClick(category) {
            if (category !== this.currCategory) {
                this.currCategory = category
            }
        },

        onBallClick(id) {
            const ball = this.balls.find(v => v.id === id)
            if (ball) {
                ball.category = ball.category ? null : this.currCategory
            }
        }
    }
})
// styles.css

#app {
    user-select: none;
}

.ping-pong-list {
    display: flex;
    margin: 20px;
    justify-content: center;
    flex-wrap: wrap;
}

.ping-pong-ball {
    width: 50px;
    height: 50px;
    margin: 5px;
    border-radius: 50%;
    box-shadow: 0 0 10px 0 #aaa;
    text-align: center;
    line-height: 50px;
}

.ping-pong-list .ping-pong-ball {
    cursor: pointer;
}

.box-wrapper {
    display: flex;
    justify-content: space-around;
    margin-top: 30px;
}

.box {
    display: flex;
    align-content: flex-start;
    flex-wrap: wrap-reverse;
    width: 300px;
    height: 200px;
    border: 10px solid;
    border-top-width: 0;
    cursor: pointer;
    transition: all 0.25s;
}

.box.red {
    border-color: rgb(238, 97, 97);
}

.box.green {
    border-color: rgb(97, 238, 156);
}

.box.blue {
    border-color: rgb(97, 146, 238);
}

.box.active {
    box-shadow: 0 10px 20px 0 #aaa;
}

.ping-pong-ball.red,
.box.red .ping-pong-ball {
    background-color: rgb(238, 97, 97);
}

.ping-pong-ball.green,
.box.green .ping-pong-ball {
    background-color: rgb(97, 238, 156);
}

.ping-pong-ball.blue,
.box.blue .ping-pong-ball {
    background-color: rgb(97, 146, 238);
}

.ping-pong-ball.disable {
    opacity: 0.25;
    pointer-events: none;
}

每个ball对象都有一个category属性,用来表示它属于哪个盒子。然后在渲染的时候,根据category来计算使用的类名。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101994.html

相关文章

  • Vue.js-表单与v-model

    摘要:学习笔记表单与表单与提供了指令,用于在表单类元素上双向绑定数据。事实上,也是一个特殊的语法糖,只不过它会在不同的表单上智能处理。选择的项复选框复选框单独使用时,也是用绑定一个布尔值。复选框选择列表当选中时,是一个,所以。 学习笔记:表单与v-model 表单与v-model Vue.js提供了v-model指令,用于在表单类元素上双向绑定数据。 使用v-model后,表单控件显示的值...

    jollywing 评论0 收藏0
  • Vue2.0 实现互斥

    摘要:需要实现如上图的功能首次加载页面,根据里的高亮对应的选项点击某个选项,该选项高亮,其他去掉高亮代码结构自我理解参考链接 showImg(https://segmentfault.com/img/bV8k6F?w=929&h=182); 需要实现如上图的功能 1. 首次加载页面,根据data里的catgoryId高亮对应的选项 2. 点击某个选项,该选项高亮,其他去掉高亮 代码结构: ...

    endiat 评论0 收藏0
  • 使用Vue渲染可配置表单--记一次问卷平台项目

    摘要:相当于可以编辑问卷并提供问卷展示,数据统计的这么一个平台。极大的节省了需要进行表单样式修改的时间,同时,让动态渲染表单成为一件可能且容易的事情。表单动态渲染刚好在项目之前,有过一次动态配置表单的尝试通过字段自动生成表单及验证。 近几天来了个紧急项目,想要做一个内部版本的问卷星。相当于可以编辑问卷并提供问卷展示,数据统计的这么一个平台。整个项目耗时不长,本着积淀和积累的原则,将过程中的...

    mcterry 评论0 收藏0

发表评论

0条评论

shiyang6017

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<