javascript - vue新手求助。v-for和v-if在同级渲染出多个元素,如何单独更改其中一个元素的显示/隐藏

查看:256
本文介绍了javascript - vue新手求助。v-for和v-if在同级渲染出多个元素,如何单独更改其中一个元素的显示/隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在自制一个能增删改的todolist练手,模仿www.todolist.cn,做到重新编辑todo内容时卡住了,在官方没总结出解决方案,所以在这里求助。以下。

点击文字变成input修改,但效果是这样的
因为我v-if绑定的是同一个变量,所以肯定会全局改掉,但对vue的理解太浅,想不到有效的办法。
代码:`<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>NBtodoList</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/vue/2.3.3/vue.min.js"></script>
<style>
    body{
        background: #CDCDCD;
    }
    header{
        height: 50px;
        line-height: 50px;
        background: #323232;
    }
    .headerForm label{
        color: #DDDDDD;
        font-size: 20px;
        cursor: pointer;;
    }
    .headerForm input{
        margin-top: 8px;
    }
    .h2{
        font-weight: bold;
        margin: 20px 0;
    }
    section{
    }
    li,ul,ol{
        list-style: none;
    }
    .oLi{
        height: 40px;
        background: #fff;
        border-radius: 5px;
        line-height: 40px;
        border-left: 5px solid #629A9C;
        margin-bottom: 10px;
    }
    .oLi .check{
        width: 22px;
        height: 22px;
        margin: 9px;
    }
    .oLi p{
        height: 40px;
    }
    .oLi *{
        padding: 0;
    }
    .oLi i{
        text-align: center;
        line-height: 40px;
        font-size: 18px;
    }
    .oLi span{
        cursor: pointer;
    }
    .oLi .txt{
        line-height: 50px;
        display: inline-block;
        padding-left: 5px;
    }
    ul{
        padding-left: 0;
    }
</style>

</head>
<body>

<div id="app">
    <header class="container-fluid">
        <div class="container">
            <div class="headerForm form-group center-block clearfix">
                <label for="todocontent" class="pull-left col-xs-3">ToDoList</label>
                <div class="col-xs-9 pull-right">
                    <input autofocus type="text" id="todocontent" class="form-control" v-model='todoText' v-on:keyup.enter='addTodo' placeholder="添加ToDo">
                </div>
            </div>
        </div>
    </header>
    <section class="container">
        <div class="center-block" style="padding: 0 15px">
            <my-h2 v-bind:num='noNum' status='正在进行'></my-h2>
            <ul>
                <my-li v-for='(todo,index) in todoList' v-bind:to='todo' v-on:deltodo='deltodos(index)' v-on:todook='dotoDown(index)' v-on:childrevamp="revamp" v-bind:childisrevamp="isRevamp"></my-li>
            </ul>
            <my-h2 v-bind:num='okNum' status='已经完成'></my-h2>
            <ul>
                <my-li style="opacity: .5" v-for='(oktodo,index) in oktodoList' v-bind:childchecked="checked" v-bind:okto='oktodo' v-on:deltodo='deloktodos(index)' v-on:todook='dotoUp(index)' childisrevamp="ture"></my-li>
            </ul>
        </div>
    </section>
</div>

</body>
<script>

Vue.component('my-li',{
    props:['to','okto','childchecked','childisrevamp'],
    template: '<li class="oLi"><input type="checkbox" v-model="childchecked"  v-on:change="todook" class="check col-xs-1"/><p class="pull-left col-xs-10"><span v-on:click="childrevamp" v-if="childisrevamp">{{to}}{{okto}}</span><input v-on:blur="childrevamp"  v-if="!childisrevamp" class="txt form-control"></input></p><i href="#" v-on:click="deltodo" class="pull-right col-xs-1 glyphicon glyphicon-remove"></i></li>',
    methods: {
        childAddTodo: function(){
            this.$emit('childAddTodo')
        },
        deltodo: function(){
            this.$emit('deltodo');
        },
        todook: function(){
            this.$emit('todook');
        },
        childrevamp: function(){
            this.$emit('childrevamp');
        }
    }
})
Vue.component('my-h2',{
    props: ['num','status'],
    template: '<h2 class="h2">{{status}}<span class="pull-right badge" style="margin-top: 8px;">{{num}}</span></h2>'
})
new Vue({
    el: '#app',
    data: {
        todoText: '',
        todoList: [],
        okTodoText: '',
        oktodoList: [],
        noNum: 0,
        okNum: 0,
        checked: true,
        isRevamp: true
    },
    methods: {
        addTodo: function(){
            this.todoText.trim() ? this.todoList.push((this.todoText).trim()) : alert('你输嘞不对~');
            localStorage.notodo = JSON.stringify(this.todoList);
            this.noNum = this.todoList.length;
            this.todoText = '';
        },
        deltodos: function(index){
            this.todoList.splice(index,1);
            localStorage.notodo = JSON.stringify(this.todoList);
            this.noNum = this.todoList.length;
        },
        deloktodos: function(index){
            this.oktodoList.splice(index,1);
            localStorage.oktodo = JSON.stringify(this.oktodoList);
            this.okNum = this.oktodoList.length;
        },
        dotoDown: function(index){
            this.oktodoList.push(this.todoList[index]);
            localStorage.oktodo = JSON.stringify(this.oktodoList);
            this.deltodos(index);
            this.okNum = this.oktodoList.length;
        },
        dotoUp: function(index){
            this.todoList.push(this.oktodoList[index]);
            localStorage.notodo = JSON.stringify(this.todoList);
            this.deloktodos(index);
            localStorage.oktodo = JSON.stringify(this.oktodoList);
            this.noNum = this.todoList.length;
        },
        revamp: function(){
            this.isRevamp = !this.isRevamp;
        }
    },
    created:function(){
        this.todoList = localStorage.notodo ? JSON.parse(localStorage.notodo) : [];
        this.oktodoList = localStorage.oktodo ? JSON.parse(localStorage.oktodo) : [];
        this.noNum = this.todoList.length;
        this.okNum = this.oktodoList.length;
    }
})

</script>
</html>`
有什么办法能够在触发revamp事件后只让当前的span隐藏,input显示呢?求大牛指教!

解决方案

this.isRevamp = !this.isRevamp;

这种方式会让所有的todo一起显示或隐藏,

要做到只针对某个todo 的话,你可以为每个todo添加一个isRevamp,然后变成:

todo.isRevamp = !todo.isRevamp;

但这就要求你的数据中一开始每个todo都包涵isRevamp,如果你想动态地添加,会比较麻烦一些,请看文档中有关 响应式原理部分:https://cn.vuejs.org/v2/guide...

但有一个简单的处理方式:data中添加一个index变量,
大致是这样的:

<li v-for="(v,i) in list" v-show="i==index" @click="show(i)">{{v}}</li>

show:function(i){
this.index=i;
}

这篇关于javascript - vue新手求助。v-for和v-if在同级渲染出多个元素,如何单独更改其中一个元素的显示/隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆