繁体   English   中英

如何在Django表中实现搜索和排序功能

[英]how to implement search and sorting function in Django table

我知道有Django数据表,django_tables2甚至django_filter可以对表执行搜索和排序。 我尝试使用Django-datatable,django_tables2甚至django_filter,但是它们都不起作用。 我已经附上了模板的代码。 我使用下面的代码呈现两个不同的表,一个表包含动作和状态列,而另一个表不包含这两列。

{% if some %}
    <table  id="example" class="display" cellspacing="0" width="100%" border="1.5px">
        <tr align="center">
            <th style="font-family: Calibri" > Student ID </th>
            <th style="font-family: Calibri" > Student Name </th>
            <th style="font-family: Calibri" > Start Date </th>
            <th style="font-family: Calibri" > End Date </th>
            <th style="font-family: Calibri" > Action </th>
            <th style="font-family: Calibri" > Status </th>
        </tr>
        {% for item in query_results %}
            <tr align="center">
                <td style="font-family: Calibri" > {{item.studentID}} </td>
                <td style="font-family: Calibri" > {{item.studentName}} </td>
                <td style="font-family: Calibri" > {{item.startDate|date:'d-m-Y'}} </td>
                <td style="font-family: Calibri" > {{item.endDate|date:'d-m-Y'}} </td>
                <td style="font-family: Calibri" >
                    {% if item.status == 'Approved' %}

                    {% else %} 
                        <a href="{% url 'timesheet:edit' id=item.id status='a' %}"><button onclick="alert('timesheet accepted')">Approve</button></a> <a href="{% url 'timesheet:edit' id=item.id status='d' %}"><button onclick="alert('timesheet denied')")>Deny</button></a>
                    {% endif %}
                </td>
                <td style="font-family: Calibri" > 
                    {% if item.status %}
                    {{item.status}}
                    {% else %}
                        Pending
                    {% endif %}       
                </td>
            </tr>
        {% endfor %}
    </table>
{% else %}
    <table  id="example" class="display" cellspacing="0" width="100%" border="1.5px">
        <tr align="center">
            <th style="font-family: Calibri" > Student ID </th>
            <th style="font-family: Calibri" > Student Name </th>
            <th style="font-family: Calibri" > Start Date </th>
            <th style="font-family: Calibri" > End Date </th>
        </tr>
        {% for item in query_results %}
            <tr align="center">
                <td style="font-family: Calibri" > {{item.studentID}} </td>
                <td style="font-family: Calibri" > {{item.studentName}} </td>
                <td style="font-family: Calibri" > {{item.startDate|date:'d-m-Y'}} </td>
                <td style="font-family: Calibri" > {{item.endDate|date:'d-m-Y'}} </td>
            </tr>
        {% endfor %}
    </table>
{% endif %}

这是我所追求的搜索栏。 搜索栏按学生ID,学生姓名,startDate,endDate,状态查找

最新:使用此链接到实施搜索栏的已编辑代码

我按此链接中的教程进行操作,但是当我在搜索栏中输入StudentID时,将显示所有数据,但不显示特定输入的数据。

模板.html文件

<form method="get" action="">
    <button type="submit" style="float: right;"> Search </button>
    <input type="text" name="q" placeholder="Search" style="float: right;">
</form>
<br></br>

{% if timesheets %}               
     {% for timesheet in timesheets %}               
        <li>{{ timesheet.studentID }} - {{ timesheet.studentName }} - {{ timesheet.startDate }} - {{ timesheet.endDate }} - {{ timesheet.status }} </li>           
     {% endfor %} 
{% else %}
    //display all the data
{% endif %}

views.py

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        timesheets = Timesheet.objects.filter(studentID__icontains=q)
        return render(request, 'timesheet/supervisor_list_timesheet.html',
                  {'timesheets': timesheets})

该表正确显示一些数据后,还必须确保显示了过滤器表单集。 我通常使用类似的方法(在这种情况下,使用{% load bootstrap3 %}

{% if filter %}
    <div class="col-sm-10">
        <form action="" method="get" class="form form-inline">
            {% bootstrap_form filter.form layout='inline' %}
            {% bootstrap_button 'filter' %}
        </form>
   </div>
{% endif %}

如果将filterset作为filter分配给模板上下文,则使用django-tables2过滤页面中的示例时应该是这种情况

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM