在Vue JS中编辑数组中的项目描述

时间:2017-03-17 02:14:09

标签: javascript arrays vue.js vuejs2

我正在尝试使用Vue JS向任务应用添加编辑功能。

  1. 我在编辑按钮上有一个点击事件 - @click="editShow",显示所有项目的编辑输入。我需要这个只显示相应的输入。
  2. 然后我无法将编辑值保存到项目描述 - @keyup.enter="editTask"。由于某种原因,任务引用的是keyup事件而不是对象。我认为这是造成问题的原因。
  3. 这是我到目前为止所拥有的,https://jsfiddle.net/clintongreen/0p6bvd4j/

    HTML

        <div class="container" id="tasks">
    
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">
                {{ message }}
            </h3>
          </div>
          <ul class="list-group">
            <li class="list-group-item clearfix" v-for="task in tasklist" >
                <strong v-if="!editActive">{{ task.description }}</strong>
                <input v-model="editTaskName" v-bind:placeholder="task.description" v-if="editActive" @keyup.enter="editTask" type="text" class="form-control input-height pull-left">
                <div class="btn-group btn-group-sm pull-right" role="group" v-if="!task.completed">
                  <button type="button" class="btn btn-default" @click="completeTask(task)">Complete</button>
                  <button type="button" @click="editShow" class="btn btn-default">Edit</button>
                  <button type="button" class="btn btn-default" @click="removeTask(task)">Remove</button>
                </div>
                <button class="btn btn-default btn-sm completed text-muted pull-right disabled btn-width" v-else>Completed</button>
            </li>
            <li class="list-group-item clearfix">
                <input v-model="newTaskName" @keyup.enter="newTask" type="text" class="form-control input-height pull-left">
                <button class="btn btn-success btn-sm pull-right btn-width" @click="newTask">Add Task</button>
            </li>
          </ul>
        </div>
    
    </div>
    

    JS

        new Vue({
    
        el: '#tasks',
    
        data: {
            message: 'Tasks',
            completed: null,
            newTaskName: '',
            editTaskName: '',
            editActive: false,
            tasklist: [
                { description: 'Read', completed: true },
                { description: 'Write', completed: true },
                { description: 'Edit', completed: false },
                { description: 'Publish', completed: false }
            ]
        },
    
        methods: {
            completeTask: function(task){
                task.completed = true;
            },
            newTask: function(){
                this.tasklist.push({description: this.newTaskName, completed: false});
            },
            removeTask: function(task){
                this.tasklist.splice(this.tasklist.indexOf(task), 1);
                console.log(task);
            },
            editShow: function(task){
                this.editActive = true // should only show the corresponding edit input
                console.log(task);
            },
            editTask: function(task){
                console.log(task);
            }
        }
    
    })
    

1 个答案:

答案 0 :(得分:5)

我在您的任务中添加了editing布尔值。您可以切换它以将各个任务交换到编辑模式。此外,我将任务通过editTask上的keyup.enter。最后,因为在您的输入上使用v-model,所以不需要进行保存,因为描述是双向约束的。您的回车键处理程序只需要切换回模式。

new Vue({

    el: '#tasks',

    data: {
        message: 'Tasks',
        completed: null,
        newTaskName: '',
        tasklist: [
            { description: 'Read', completed: true, editing: false },
            { description: 'Write', completed: true, editing: false  },
            { description: 'Edit', completed: false, editing: false  },
            { description: 'Publish', completed: false, editing: false  }
        ]
    },

    methods: {
        completeTask: function(task){
            task.completed = true;
        },
        newTask: function(){
            this.tasklist.push({description: this.newTaskName, completed: false, editing: false});
        },
        removeTask: function(task){
            this.tasklist.splice(this.tasklist.indexOf(task), 1);
            console.log(task);
        },
        editTask: function(task){
            task.editing = false;
            console.log(task);
        }
    }

})

模板

<div class="container" id="tasks">

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">
            {{ message }}
        </h3>
      </div>
      <ul class="list-group">
        <li class="list-group-item clearfix" v-for="task in tasklist" >
            <strong v-if="!task.editing">{{ task.description }}</strong>
            <input v-model="task.description" v-if="task.editing" @keyup.enter="editTask(task)" type="text" class="form-control input-height pull-left">
            <div class="btn-group btn-group-sm pull-right" role="group" v-if="!task.completed">
              <button type="button" class="btn btn-default" @click="completeTask(task)">Complete</button>
              <button type="button" @click="task.editing = true" class="btn btn-default">Edit</button>
              <button type="button" class="btn btn-default" @click="removeTask(task)">Remove</button>
            </div>
            <button class="btn btn-default btn-sm completed text-muted pull-right disabled btn-width" v-else>Completed</button>
        </li>
        <li class="list-group-item clearfix">
            <input v-model="newTaskName" @keyup.enter="newTask" type="text" class="form-control input-height pull-left">
            <button class="btn btn-success btn-sm pull-right btn-width" @click="newTask">Add Task</button>
        </li>
      </ul>
    </div>

</div>

修改了fiddle