Drag & drop on ticket checklist tasks edition page + Use an empty form in order to better generate new task row dynamically

This commit is contained in:
Benbb96 2023-04-30 03:56:42 +02:00
parent ff61f85d59
commit 3235333873
No known key found for this signature in database
4 changed files with 37 additions and 24 deletions

View File

@ -45,28 +45,14 @@
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th class="text-right">{% trans "Position" %}</th>
<th>{% trans "Description" %}</th> <th>{% trans "Description" %}</th>
<th>{% trans "Position" %}</th>
<th class="text-center">{% trans "Delete?" %}</th> <th class="text-center">{% trans "Delete?" %}</th>
</tr> </tr>
</thead> </thead>
<tbody id="tasks"> <tbody id="tasks">
{% for form in formset %} {% for form in formset %}
<tr> {% include 'helpdesk/include/task_form_row.html' %}
{{ form.id }}
<td>
{{ form.description }}
{{ form.description.errors }}
</td>
<td>
{{ form.position }}
{{ form.position.errors }}
</td>
<td>
{{ form.DELETE }}
{{ form.DELETE.errors }}
</td>
</tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
@ -90,17 +76,28 @@
{% endblock %} {% endblock %}
{% block helpdesk_js %} {% block helpdesk_js %}
<script src="https://unpkg.com/sortablejs-make/Sortable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-sortablejs@latest/jquery-sortable.js"></script>
<script> <script>
const updatePosition = () => {
$('#tasks tr').each((index, taskRow) => {
$(taskRow).find('input[id$=position]').val(index + 1)
})
}
$(() => { $(() => {
const idTasksTotalForms = $('#id_tasks-TOTAL_FORMS') const idTasksTotalForms = $('#id_tasks-TOTAL_FORMS')
$('#addRow').on('click', function() { $('#addRow').on('click', function() {
const formCount = idTasksTotalForms.val() const formCount = idTasksTotalForms.val()
const description = '{{ formset.empty_form.description|escapejs }}'.replace(/__prefix__/g, formCount); $('#tasks').append(`{% include 'helpdesk/include/task_form_row.html' with form=formset.empty_form %}`.replace(/__prefix__/g, formCount))
const position = '{{ formset.empty_form.position|escapejs }}'.replace(/__prefix__/g, formCount);
const delete_checkbox = '{{ formset.empty_form.DELETE|escapejs }}'.replace(/__prefix__/g, formCount);
$('#tasks').append(`<tr><td>${description}</td><td>${position}</td><td>${delete_checkbox}</td></tr>`)
idTasksTotalForms.val(parseInt(formCount) + 1); idTasksTotalForms.val(parseInt(formCount) + 1);
updatePosition()
}); });
$('#tasks').sortable({
handle: '.handle',
onChange: updatePosition
})
}) })
</script> </script>
{% endblock %} {% endblock %}

View File

@ -0,0 +1,16 @@
<tr>
{{ form.id }}
<td title="Drag & Drop" class="text-right handle">
<i class="fas fa-grip-vertical"></i>
{{ form.position }}
{{ form.position.errors }}
</td>
<td>
{{ form.description }}
{{ form.description.errors }}
</td>
<td>
{{ form.DELETE }}
{{ form.DELETE.errors }}
</td>
</tr>

View File

@ -175,7 +175,7 @@
<th class="table-active">{% trans "Checklists" %}</th> <th class="table-active">{% trans "Checklists" %}</th>
<td colspan="3"> <td colspan="3">
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row align-items-baseline">
{% for checklist in ticket.checklists.all %} {% for checklist in ticket.checklists.all %}
<div class="col-sm-4 col-xs-12"> <div class="col-sm-4 col-xs-12">
<div class="card mb-4"> <div class="card mb-4">

View File

@ -20,7 +20,7 @@ from django.core.exceptions import PermissionDenied, ValidationError
from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.wsgi import WSGIRequest
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Q from django.db.models import Q
from django.forms import inlineformset_factory, TextInput, NumberInput from django.forms import inlineformset_factory, TextInput, HiddenInput
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
@ -454,11 +454,11 @@ def edit_ticket_checklist(request, ticket_id, checklist_id):
formset=FormControlDeleteFormSet, formset=FormControlDeleteFormSet,
fields=['description', 'position'], fields=['description', 'position'],
widgets={ widgets={
'position': HiddenInput(),
'description': TextInput(attrs={'class': 'form-control'}), 'description': TextInput(attrs={'class': 'form-control'}),
'position': NumberInput(attrs={'class': 'form-control'}),
}, },
can_delete=True, can_delete=True,
extra=1 extra=0
) )
formset = TaskFormSet(request.POST or None, instance=checklist) formset = TaskFormSet(request.POST or None, instance=checklist)
if form.is_valid() and formset.is_valid(): if form.is_valid() and formset.is_valid():