Multiple select

This commit is contained in:
Yohann D'ANELLO 2020-03-11 22:51:46 +01:00
parent cebbe65eef
commit c4f54d9d5b
3 changed files with 59 additions and 27 deletions

View File

@ -132,6 +132,7 @@ class Transaction(PolymorphicModel):
if self.source.pk == self.destination.pk: if self.source.pk == self.destination.pk:
# When source == destination, no money is transfered # When source == destination, no money is transfered
super().save(*args, **kwargs)
return return
created = self.pk is None created = self.pk is None

View File

@ -18,7 +18,7 @@ class HistoryTable(tables.Table):
} }
model = Transaction model = Transaction
exclude = ("polymorphic_ctype", ) exclude = ("polymorphic_ctype", )
order_by = ('-created_at', ) order_by = ('-id', )
template_name = 'django_tables2/bootstrap4.html' template_name = 'django_tables2/bootstrap4.html'
sequence = ('...', 'total', 'valid') sequence = ('...', 'total', 'valid')

View File

@ -30,18 +30,15 @@
Sélection des émitteurs Sélection des émitteurs
</p> </p>
</div> </div>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush" id="note_list">
<li class="list-group-item py-1 d-flex justify-content-between align-items-center">
Cras justo odio
<span class="badge badge-dark badge-pill">14</span>
</li>
<li class="list-group-item py-1 d-flex justify-content-between align-items-center">
Dapibus ac facilisis in
<span class="badge badge-dark badge-pill">1</span>
</li>
</ul> </ul>
<div class="card-body"> <div class="card-body">
TODO: reimplement select2 here in JS <select name="source" data-placeholder="Note ..." data-minimum-input-length="1"
required id="note" data-autocomplete-light-language="fr"
data-autocomplete-light-url="/note/note-autocomplete/"
data-autocomplete-light-function="select2">
<option value="" selected>---------</option>
</select>
</div> </div>
</div> </div>
</div> </div>
@ -83,10 +80,12 @@
<div class="tab-pane" id="{{ category.grouper|slugify }}"> <div class="tab-pane" id="{{ category.grouper|slugify }}">
<div class="d-inline-flex flex-wrap justify-content-center"> <div class="d-inline-flex flex-wrap justify-content-center">
{% for button in category.list %} {% for button in category.list %}
{% if button.display %}
<button class="btn btn-outline-dark rounded-0 flex-fill" <button class="btn btn-outline-dark rounded-0 flex-fill"
id="button{{ button.id }}" name="button" value="{{ button.name }}"> id="button{{ button.id }}" name="button" value="{{ button.name }}">
{{ button.name }} ({{ button.amount | pretty_money }}) {{ button.name }} ({{ button.amount | pretty_money }})
</button> </button>
{% endif %}
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
@ -131,10 +130,23 @@
min-width: 100%; min-width: 100%;
} }
</style> </style>
<link href="/static/vendor/select2/dist/css/select2.css" type="text/css" media="screen" rel="stylesheet">
<link href="/static/admin/css/autocomplete.css" type="text/css" media="screen" rel="stylesheet">
<link href="/static/autocomplete_light/select2.css" type="text/css" media="screen" rel="stylesheet">
<script type="text/javascript" src="/static/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/select2.full.js"></script>
<script type="text/javascript" src="/static/vendor/select2/dist/js/i18n/fr.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/forward.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/select2.js"></script>
<script type="text/javascript" src="/static/autocomplete_light/jquery.post-setup.js"></script>
{% endblock %} {% endblock %}
{% block extrajavascript %} {% block extrajavascript %}
<script type="text/javascript"> <script type="text/javascript">
var consos = [];
$(document).ready(function() { $(document).ready(function() {
// If hash of a category in the URL, then select this category // If hash of a category in the URL, then select this category
// else select the first one // else select the first one
@ -149,26 +161,45 @@
location.hash = this.getAttribute("href"); location.hash = this.getAttribute("href");
}); });
$("#note").change(function(obj) {
let name = $("#note option:selected").text();
note_obj = $("#note");
note = note_obj.val();
note_obj.val(0);
note_obj.text("");
consos = consos.concat([[name, note]]);
note_list = $("#note_list");
note_list.html(note_list.html() +
"<li class=\"list-group-item py-1 d-flex justify-content-between align-items-center\">\n" +
" " + name + "\n" +
" <span class=\"badge badge-dark badge-pill\">1</span>\n" +
" </li>");
});
{% for button in transaction_templates %} {% for button in transaction_templates %}
{% if button.display %}
$("#button{{ button.id }}").click(function() { $("#button{{ button.id }}").click(function() {
$.post("/api/note/transaction/transaction/", consos.forEach(function(conso) {
{ console.log(conso);
"csrfmiddlewaretoken": "{{ csrf_token }}", $.post("/api/note/transaction/transaction/",
"quantity": 1, {
"amount": {{ button.amount }}, "csrfmiddlewaretoken": "{{ csrf_token }}",
"reason": "{{ button.name }} ({{ button.category.name }})", "quantity": 1,
"valid": true, "amount": {{ button.amount }},
"polymorphic_ctype": {{ polymorphic_ctype }}, "reason": "{{ button.name }} ({{ button.category.name }})",
"resourcetype": "TemplateTransaction", "valid": true,
"source": 6, "polymorphic_ctype": {{ polymorphic_ctype }},
"destination": 7, "resourcetype": "TemplateTransaction",
"category": {{ button.category.id }}, "source": conso[1],
"template": {{ button.id }} "destination": {{ button.destination.pk }},
}, "category": {{ button.category.id }},
function(data, status) { "template": {{ button.id }}
reloadWithTurbolinks(); }, reloadWithTurbolinks);
}); });
reloadWithTurbolinks();
}); });
{% endif %}
{% endfor %} {% endfor %}
}); });