最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove item from array in Vue - Stack Overflow

programmeradmin4浏览0评论

I have the following task list app. I am trying to implement a delete function. I get I need to use splice to remove the item from the array but how do I target only the item whose button I clicked on?

/

JS

new Vue({

    el: '#tasks',

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

    methods: {
        completeTask: function(task){
            // console.log(this.tasks.description);
            taskpleted = true;
        },
        newTask: function(){
            this.tasklist.push({description: this.newTaskName, completed: false});
        },
        removeTask: function(task){
            this.tasklist.splice(this.task.index, 1);
        }
    }

})  

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" >
            {{ task.description }}
            <!-- <button class="btn btn-primary btn-sm pull-right" v-if="!taskpleted" @click="completeTask(task)">Complete</button> -->
            <div class="btn-group btn-group-sm pull-right" role="group" v-if="!taskpleted">
              <button type="button" class="btn btn-primary" @click="completeTask(task)">Complete</button>
              <button type="button" class="btn btn-info">Edit</button>
              <button type="button" class="btn btn-danger" @click="removeTask(task)">Delete</button>
            </div>
            <button class="btn btn-default btn-sm completed text-muted pull-right disabled" v-else>Completed</button>
        </li>
        <li class="list-group-item clearfix">
            <input v-model="newTaskName" @keyup.enter="newTask" type="text" class="pull-left">
            <button class="btn btn-success btn-sm pull-right" @click="newTask">Add Task</button>
        </li>
      </ul>
    </div>

</div>

I have the following task list app. I am trying to implement a delete function. I get I need to use splice to remove the item from the array but how do I target only the item whose button I clicked on?

https://jsfiddle.net/clintongreen/txtrage5/1/

JS

new Vue({

    el: '#tasks',

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

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

})  

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" >
            {{ task.description }}
            <!-- <button class="btn btn-primary btn-sm pull-right" v-if="!task.completed" @click="completeTask(task)">Complete</button> -->
            <div class="btn-group btn-group-sm pull-right" role="group" v-if="!task.completed">
              <button type="button" class="btn btn-primary" @click="completeTask(task)">Complete</button>
              <button type="button" class="btn btn-info">Edit</button>
              <button type="button" class="btn btn-danger" @click="removeTask(task)">Delete</button>
            </div>
            <button class="btn btn-default btn-sm completed text-muted pull-right disabled" v-else>Completed</button>
        </li>
        <li class="list-group-item clearfix">
            <input v-model="newTaskName" @keyup.enter="newTask" type="text" class="pull-left">
            <button class="btn btn-success btn-sm pull-right" @click="newTask">Add Task</button>
        </li>
      </ul>
    </div>

</div>
Share Improve this question asked Mar 16, 2017 at 22:12 Clinton GreenClinton Green 9,97723 gold badges72 silver badges109 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

Use the index of the task in the v-for to decide which item to splice():

v-for="(task, index) in tasklist"

Your button:

<button type="button" class="btn btn-danger" @click="removeTask(index)">Delete</button>

And then simply:

removeTask: function(index) {
    this.tasklist.splice(index, 1);
}
removeTask: function(task){
    this.tasklist.splice(this.tasklist.indexOf(task), 1);
}

For Vue2 use delete. See API.

removeTask: function(index){
    this.$delete(this.tasklist, index)
}

You can use indexOf to get the current index from the array

remove: function (task){
      this.tasklist.splice(this.tasklist.indexOf(task),1);
    },
发布评论

评论列表(0)

  1. 暂无评论