-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-table.php
90 lines (74 loc) · 2.54 KB
/
sort-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<table class="table table-responsive-sm">
<tbody>
@foreach ($tasks as $task)
<tr>
<td>
@if ($task->position > 1)
<a wire:click.prevent="task_up({{ $task->id }})" href="#">
↑
</a>
@endif
@if ($task->position < $tasks->max('position'))
<a wire:click.prevent="task_down({{ $task->id }})" href="#">
↓
</a>
@endif
</td>
<td>{{ $task->name }}</td>
<td>
<a class="btn btn-sm btn-primary"
href="{{ route('admin.checklists.tasks.edit', [$checklist, $task]) }}">{{ __('Edit') }}</a>
<form style="display: inline-block"
action="{{ route('admin.checklists.tasks.destroy', [$checklist, $task]) }}"
method="POST">
@csrf
@method('DELETE')
<button class="btn btn-sm btn-danger" type="submit"
onclick="return confirm('{{ __('Are you sure?') }}')"> {{ __('Delete') }}</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
//BLADE CODE to call livewire
<div class="card">
<div class="card-header"><i class="fa fa-align-justify"></i> {{ __('List of Tasks') }}</div>
<div class="card-body">
@livewire('tasks-table', ['checklist' => $checklist])
</div>
</div>
// Livewire component
<?php
namespace App\Http\Livewire;
use App\Models\Task;
use Livewire\Component;
class TasksTable extends Component
{
public $checklist;
public function render()
{
$tasks = $this->checklist->tasks()->where('user_id', NULL)->orderBy('position')->get();
return view('livewire.tasks-table', compact('tasks'));
}
public function task_up($task_id)
{
$task = Task::find($task_id);
if ($task) {
Task::whereNull('user_id')->where('position', $task->position - 1)->update([
'position' => $task->position
]);
$task->update(['position' => $task->position - 1]);
}
}
public function task_down($task_id)
{
$task = Task::find($task_id);
if ($task) {
Task::whereNull('user_id')->where('position', $task->position + 1)->update([
'position' => $task->position
]);
$task->update(['position' => $task->position + 1]);
}
}
}