0
名前、型、所有者などのいくつかの属性を持つモデルItemがあります。私のテンプレートでは、Itemsのリストは反復され、テーブルに表示されます。私がしたいのは、テーブルヘッドの隣のボタンがクリックされたとき、つまり名前の隣にあるボタンがクリックされたときに、テーブルの要素がアルファベット順にソートされるときに、テーブルの項目を並べ替えることです。ここに私のテンプレートは次のとおりです。テンプレートの属性別にDjangoオブジェクトをソートする
{% extends 'inventory/base.html' %}
{% block content %}
<a class="btn btn-default" href="{% url 'inventory:entry' %}">New Item</a>
<script>
jQuery(document).ready(function($) {
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
});
</script>
{% if item_list %}
<table>
<tr>
<th>Name <button class="btn btn-default" href="order_table"></button></th>
<th>Type <button class="btn btn-default" href="order_table"></button></th>
<th>Barcode/Model No. <button class="btn btn-default" href="order_table"></button></th>
<th>Owner <button class="btn btn-default" href="order_table"></button></th>
<th>Edit <button class="btn btn-default" href="order_table"></button></th>
</tr>
{% for item in item_list %}
<tr class="clickable-row itemrow" data-href="{% url 'inventory:details' item.id %}">
<td>{{ item.name }}</td>
<td>{{ item.itemType }}</td>
<td>{{ item.barcode }}</td>
<td>{{ item.owner.username }}</td>
<td><a href="{% url 'inventory:edit' item.id %}" class="btn btn-default">Edit</a></td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}
さて、あなたが持っている2つのオプションが役立ちます。 Javascriptを使用して項目をページ上で直接ソートするか、sortパラメータをサーバに渡してソートしてそこにクエリをやり直してください。 –
サーバにソートパラメータを渡すと、ページ全体ではなくページのテーブル部分だけをリロードできるということはありますか? – Stalfurion
もちろん、Ajaxでそれを行うこともできます。 –