【发布时间】:2016-01-29 06:06:50
【问题描述】:
我的数据如下所示:
{
"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 函数不起作用,但如果我返回未分组的数据,它可以正常工作。
我明白它为什么不起作用,只是不知道如何解决它。任何帮助将不胜感激。
【问题讨论】:
-
将数据取消分组然后通过 Vue 进行分组是否更有意义?
标签: javascript laravel vue.js