Compare commits
1 Commits
validate-t
...
382e514d03
| Author | SHA1 | Date | |
|---|---|---|---|
| 382e514d03 |
@ -1,4 +1,22 @@
|
|||||||
{% macro inline_table_row(person, is_editing, errors={}) %}
|
{% macro inline_table_row(person) %}
|
||||||
|
<tr hx-target="this" hx-swap="outerHTML">
|
||||||
|
<td>{{ person.name }}</td>
|
||||||
|
<td>{{ person.address }}</td>
|
||||||
|
<td>{{ person.city }}</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
class="btn btn-outline-primary"
|
||||||
|
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||||
|
|
||||||
|
>
|
||||||
|
<i class="bi bi-pencil-square"></i>
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
|
||||||
|
{% macro inline_table_row_edit(person, cities, errors={}) %}
|
||||||
{% macro render_input(field_name, value) %}
|
{% macro render_input(field_name, value) %}
|
||||||
{% set has_error = field_name in errors %}
|
{% set has_error = field_name in errors %}
|
||||||
<input
|
<input
|
||||||
@ -9,48 +27,52 @@
|
|||||||
>
|
>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% if is_editing %}
|
{% macro render_select(field_name, value, options) %}
|
||||||
<tr id="person-row-{{ person.pk }}" hx-target="this" hx-swap="outerHTML">
|
{% set has_error = field_name in errors %}
|
||||||
<td>
|
<select
|
||||||
{{ render_input(field_name="name", value=person.name) }}
|
class="form-select {% if has_error %}is-invalid{% endif %}"
|
||||||
</td>
|
name="{{ field_name }}"
|
||||||
<td>
|
{% if has_error %}title="{{ errors[field_name] }}"{% endif %}
|
||||||
{{ render_input(field_name="address", value=person.address) }}
|
>
|
||||||
</td>
|
{% for option in options %}
|
||||||
<td>
|
{% set selected = value == option %}
|
||||||
{{ render_input(field_name="city", value=person.city) }}
|
<option value="{{ option }}" {% if selected %}selected{% endif %}>
|
||||||
</td>
|
{{ option }}
|
||||||
<td>
|
</option>
|
||||||
<button
|
{% endfor %}
|
||||||
class="btn btn-outline-success"
|
</select>
|
||||||
hx-post="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
{% endmacro %}
|
||||||
hx-include="#person-row-{{ person.pk }} input"
|
|
||||||
>
|
|
||||||
<i class="bi bi-check-circle-fill"></i>
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
class="btn btn-outline-danger"
|
|
||||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
|
||||||
hx-vals='{"action": "cancel"}'
|
|
||||||
>
|
|
||||||
<i class="bi bi-x-circle-fill"></i>
|
|
||||||
</button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
{% else %}
|
|
||||||
<tr hx-target="this" hx-swap="outerHTML">
|
|
||||||
<td>{{ person.name }}</td>
|
|
||||||
<td>{{ person.address }}</td>
|
|
||||||
<td>{{ person.city }}</td>
|
|
||||||
<td>
|
|
||||||
<button
|
|
||||||
class="btn btn-outline-primary"
|
|
||||||
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
|
||||||
|
|
||||||
>
|
<tr
|
||||||
<i class="bi bi-pencil-square"></i>
|
id="person-row-{{ person.pk }}"
|
||||||
</button>
|
hx-target="this"
|
||||||
</td>
|
hx-swap="outerHTML"
|
||||||
</tr>
|
>
|
||||||
{% endif %}
|
<td>
|
||||||
|
{{ render_input(field_name="name", value=person.name) }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ render_input(field_name="address", value=person.address) }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{# {{ render_input(field_name="city", value=person.city) }}#}
|
||||||
|
{{ render_select(field_name="city", value=person.city, options=cities) }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
class="btn btn-outline-success"
|
||||||
|
hx-post="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||||
|
hx-include="#person-row-{{ person.pk }} input, #person-row-{{ person.pk }} select"
|
||||||
|
>
|
||||||
|
<i class="bi bi-check-circle-fill"></i>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-outline-danger"
|
||||||
|
hx-get="{{ url("table-inline-edit-row", pk=person.pk) }}"
|
||||||
|
hx-vals='{"action": "cancel"}'
|
||||||
|
>
|
||||||
|
<i class="bi bi-x-circle-fill"></i>
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for person in persons %}
|
{% for person in persons %}
|
||||||
{{ inline_table_row(person, is_editing=False) }}
|
{{ inline_table_row(person) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
{% from "main/components/inline_table_row.html" import inline_table_row %}
|
{% from "main/components/inline_table_row.html" import inline_table_row, inline_table_row_edit %}
|
||||||
|
|
||||||
{{ inline_table_row(person, is_editing=is_editing, errors=errors) }}
|
{% if is_editing %}
|
||||||
|
{{ inline_table_row_edit(person, cities=cities, errors=errors) }}
|
||||||
|
{% else %}
|
||||||
|
{{ inline_table_row(person) }}
|
||||||
|
{% endif %}
|
||||||
|
|||||||
@ -8,6 +8,15 @@ 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
|
||||||
|
|
||||||
|
CITIES: list[str] = [
|
||||||
|
"",
|
||||||
|
"Zagreb",
|
||||||
|
"Split",
|
||||||
|
"Pula",
|
||||||
|
"Rijeka",
|
||||||
|
"Kozari bok",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_person(pk: int) -> Person:
|
def get_person(pk: int) -> Person:
|
||||||
try:
|
try:
|
||||||
@ -46,6 +55,7 @@ class TableInlineEditRowView(DemoViewBase):
|
|||||||
context_data.update(
|
context_data.update(
|
||||||
{
|
{
|
||||||
"person": person,
|
"person": person,
|
||||||
|
"cities": CITIES,
|
||||||
"is_editing": action == "edit",
|
"is_editing": action == "edit",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -66,12 +76,11 @@ class TableInlineEditRowView(DemoViewBase):
|
|||||||
else:
|
else:
|
||||||
person.save()
|
person.save()
|
||||||
|
|
||||||
print(errors)
|
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
context={
|
context={
|
||||||
"person": person,
|
"person": person,
|
||||||
"errors": errors,
|
"errors": errors,
|
||||||
|
"cities": CITIES,
|
||||||
"is_editing": errors is not None,
|
"is_editing": errors is not None,
|
||||||
},
|
},
|
||||||
template_name=self.template_name,
|
template_name=self.template_name,
|
||||||
|
|||||||
Reference in New Issue
Block a user