Finished
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{% macro inline_table_row(person, is_editing) %}
|
{% macro inline_table_row(person, is_editing) %}
|
||||||
<tr hx-target="this" hx-swap="outerHTML">
|
|
||||||
{% if is_editing %}
|
{% if is_editing %}
|
||||||
|
<tr id="person-row-{{ person.pk }}" hx-target="this" hx-swap="outerHTML">
|
||||||
<td>
|
<td>
|
||||||
<input class="form-control" name="name" value="{{ person.name }}">
|
<input class="form-control" name="name" value="{{ person.name }}">
|
||||||
</td>
|
</td>
|
||||||
@ -13,8 +13,8 @@
|
|||||||
<td>
|
<td>
|
||||||
<button
|
<button
|
||||||
class="btn btn-outline-success"
|
class="btn btn-outline-success"
|
||||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
hx-post="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||||
hx-vals='{"action": "save"}'
|
hx-include="#person-row-{{ person.pk }} input"
|
||||||
>
|
>
|
||||||
<i class="bi bi-check-circle-fill"></i>
|
<i class="bi bi-check-circle-fill"></i>
|
||||||
</button>
|
</button>
|
||||||
@ -26,7 +26,9 @@
|
|||||||
<i class="bi bi-x-circle-fill"></i>
|
<i class="bi bi-x-circle-fill"></i>
|
||||||
</button>
|
</button>
|
||||||
</td>
|
</td>
|
||||||
|
</tr>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
<tr hx-target="this" hx-swap="outerHTML">
|
||||||
<td>{{ person.name }}</td>
|
<td>{{ person.name }}</td>
|
||||||
<td>{{ person.address }}</td>
|
<td>{{ person.address }}</td>
|
||||||
<td>{{ person.city }}</td>
|
<td>{{ person.city }}</td>
|
||||||
@ -34,10 +36,11 @@
|
|||||||
<button
|
<button
|
||||||
class="btn btn-outline-primary"
|
class="btn btn-outline-primary"
|
||||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||||
|
|
||||||
>
|
>
|
||||||
<i class="bi bi-pencil-square"></i>
|
<i class="bi bi-pencil-square"></i>
|
||||||
</button>
|
</button>
|
||||||
</td>
|
</td>
|
||||||
{% endif %}
|
|
||||||
</tr>
|
</tr>
|
||||||
|
{% endif %}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|||||||
@ -1,11 +1,20 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.http import Http404
|
from django.core.handlers.wsgi import WSGIRequest
|
||||||
|
from django.http import Http404, HttpRequest, HttpResponse
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
from project.main.models import Person
|
from project.main.models import Person
|
||||||
from project.main.views.demo_view_base import DemoViewBase
|
from project.main.views.demo_view_base import DemoViewBase
|
||||||
|
|
||||||
|
|
||||||
|
def get_person(pk: int) -> Person:
|
||||||
|
try:
|
||||||
|
return Person.objects.get(pk=pk)
|
||||||
|
except Person.DoesNotExist:
|
||||||
|
raise Http404("Person not found")
|
||||||
|
|
||||||
|
|
||||||
class TableInlineEditView(DemoViewBase):
|
class TableInlineEditView(DemoViewBase):
|
||||||
template_name = "main/table_inline_edit.html"
|
template_name = "main/table_inline_edit.html"
|
||||||
active_section = "table-inline-edit"
|
active_section = "table-inline-edit"
|
||||||
@ -30,17 +39,29 @@ class TableInlineEditRowView(DemoViewBase):
|
|||||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||||
context_data = super().get_context_data(**kwargs)
|
context_data = super().get_context_data(**kwargs)
|
||||||
|
|
||||||
try:
|
person = get_person(pk=kwargs.get("pk"))
|
||||||
person = Person.objects.get(pk=kwargs.get("pk"))
|
action = self.request.GET.get("action", "edit")
|
||||||
except Person.DoesNotExist:
|
|
||||||
raise Http404("Person not found")
|
|
||||||
|
|
||||||
action = self.request.GET.get("action")
|
|
||||||
|
|
||||||
context_data.update(
|
context_data.update(
|
||||||
{
|
{
|
||||||
"person": person,
|
"person": person,
|
||||||
"is_editing": action != "cancel",
|
"is_editing": action == "edit",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return context_data
|
return context_data
|
||||||
|
|
||||||
|
def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse:
|
||||||
|
person = get_person(pk=kwargs.get("pk"))
|
||||||
|
person.name = request.POST.get("name")
|
||||||
|
person.address = request.POST.get("address")
|
||||||
|
person.city = request.POST.get("city")
|
||||||
|
person.save()
|
||||||
|
|
||||||
|
return render(
|
||||||
|
context={
|
||||||
|
"person": person,
|
||||||
|
"is_editing": False,
|
||||||
|
},
|
||||||
|
template_name=self.template_name,
|
||||||
|
request=request,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user