Vuejs $remove 按日期分组的数据

Vuejs $remove on data grouped by date

我有这样的数据:

{
  "2015-10-29": [
    {
      "updated_at": "2015-10-29 07:36:54",
      "created_at": "2015-10-29 07:15:45",
      "due_at": "2015-10-29 00:00:00",
      "complete": false,
      "description": "Task 1",
      "user_id": "3",
      "tenant_id": "1",
      "id": "28"
    },
    {
      "updated_at": "2015-10-29 07:36:58",
      "created_at": "2015-10-29 07:15:45",
      "due_at": "2015-10-29 00:00:00",
      "complete": false,
      "description": "Task 2",
      "user_id": "3",
      "tenant_id": "1",
      "id": "29"
    }
  ],
  "2015-10-16": [
    {
      "updated_at": "2015-10-16 00:08:39",
      "created_at": "2015-10-15 23:55:59",
      "due_at": "2015-10-16 00:00:00",
      "complete": false,
      "description": "Mow and spray at Home Block",
      "user_id": "2",
      "tenant_id": "1",
      "id": "2"
    },
    {
      "updated_at": "2015-10-16 00:34:03",
      "created_at": "2015-10-16 00:34:03",
      "due_at": "2015-10-16 00:00:00",
      "complete": false,
      "description": "Another new task",
      "user_id": "2",
      "tenant_id": "1",
      "id": "5"
    },
    {
      "updated_at": "2015-10-29 07:37:10",
      "created_at": "2015-10-16 06:18:54",
      "due_at": "2015-10-16 00:00:00",
      "complete": false,
      "description": "Task 3",
      "user_id": "3",
      "tenant_id": "1",
      "id": "10"
    }
  ]
}

它基本上是一个按截止日期分组的任务列表。

我在前端显示它们是这样的:

<div class="tasks-wrapper">
            <button class="btn btn-sm btn-success"
                    v-on:click="addTask()">
                    Add Task
            </button>
            <template v-for="(due_at, tasks) in tasks">
                <h3 class="task-date">@{{ due_at }}</h3>
                <div class="task-item clearfix" v-for="task in tasks">
                    <button class="btn btn-sm btn-success"
                            v-on:click="toggleTaskCompletion(task)">
                            Complete @{{ task.complete }}
                    </button>
                    @{{ task.description }}
                    <div class="btn-group pull-right">
                        <button class="btn btn-sm btn-danger"
                                v-on:click="deleteTask(task)">
                            Remove
                        </button>
                    </div>
                </div>
            </template>
        </div>

这是 Vuejs 文件:

var tasks = new Vue({
    el: '#app',

    data: {
        tasks: [],
    },

    ready: function() {
        this.fetchData();
    },

    methods: {
        fetchData: function () {
            this.$http.get('api/tasks').success(function(tasks) {
                this.$set('tasks', tasks);
            }).error(function(error) {
                console.log(error);
            });
        },
        toggleTaskCompletion: function(task) {
            task.complete = ! task.complete;
            this.$http.patch('api/tasks/'+ task.id, task);
        },
        deleteTask: function(task) {
            alert(this.tasks);
            this.tasks.$remove(due_at.task);
            this.$http.delete('api/tasks/'+ task.id, task);
        },
        addTask: function() {
            this.tasks.push({description: 'New'})
        }
    }
})

当任务按日期分组时,我的 deleteTask 函数不起作用,但如果我 return 数据未分组,它可以正常工作。

我明白为什么它不起作用,只是不确定如何修复它。任何帮助将不胜感激。

我很确定你会想这样做:

this.tasks[due_at].$remove(task);

另外,应该注意的是您实际上并没有将 due_at 传递给您的删除函数。该按钮应如下所示:

<button class="btn btn-sm btn-danger"
    v-on:click="deleteTask(due_at, task)">
    Remove
</button>

函数应该如下所示:

deleteTask: function(due_at, task) {
    alert(this.tasks);
    this.tasks[due_at].$remove(task);
    this.$http.delete('api/tasks/'+ task.id, task);
},

这是我解决这个问题的方法:

自定义过滤器

groupByDate: function(tasks) {

        var result = {};

        for (var i = 0; i < tasks.length; i++) {
            var task = tasks[i];

            //Skip if task is complete
            if (task.complete) { continue; }

            // Convert due_at date to be used as array key
            var due_at = moment(task.due_at, 'YYYY-MM-DD').format('DD-MM-YYYY');

            // Push task into results array
            if (result[due_at]) {
                result[due_at].push(task);
            } else {
                result[due_at] = [task];
            }
        }

        return result;
    }

和 HTML:

<template v-for="(due_at, groupedTasks) in tasks | groupByDate">
                <h3 class="task-date">@{{due_at}}</h3>
                <div class="task-item clearfix"
                    v-for="task in groupedTasks | inProcess"
                    track-by="id"
                    :class="{editing: task == editingTask}">
                    <div class="form-inline" v-show="editingTask == task">
                        <input type="text" class="form-control"
                            v-model="task.description"
                            v-task-focus="task == editingTask" />
                        <button class="btn btn-sm btn-success"
                                v-on:click="updateTask(task)">
                            Update
                        </button>
                        <button class="btn btn-sm btn-default"
                                v-on:click="cancelEdit()">
                            Cancel
                        </button>
                    </div>
                    <button class="btn btn-sm btn-default"
                            v-if="!task.complete"
                            v-show="editingTask != task" 
                            v-on:click="toggleTaskCompletion(task)">
                            <span class="fa fa-check"></span>
                    </button>
                    <button class="btn btn-sm btn-success"
                            v-show="task.complete"
                            v-if="editingTask != task" 
                            v-on:click="toggleTaskCompletion(task)">
                            <span class="fa fa-check"></span>
                    </button>
                    <div class="task-description" v-show="editingTask != task" >
                        <span v-if="!task.complete" v-on:dblclick="editTask(task)">@{{ task.description }}</span>
                    </div>
                    <div class="btn-group pull-right">
                        <button class="btn btn-sm btn-primary"
                                v-on:click="editTask(task)">
                            <span class="fa fa-pencil"></span>
                        </button>
                        <button class="btn btn-sm btn-danger"
                                v-on:click="deleteTask(due_at, task)">
                            <span class="fa fa-trash"></span>
                        </button>
                    </div>
                </div>
            </template>