I know that there are Django-datatable, django_tables2, and even django_filter to perform search and sorting to the table. I have tried using Django-datatable, django_tables2 and even django_filter, but none of them work. I have attached my code for the template. I am rendering two different tables using the code below, one is with action and status column while the other is without these two columns.
{% 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 %}
This is the search bar that i am after. Search bar to find by studentID,studentName,startDate,endDate,status
LATEST : EDITED CODE USING THIS LINK TO IMPLEMENT SEARCH BAR
i follow the tutorial in this link, but when i enter the studentID in the search bar, all of the data being displayed but not the particular entered data.
template .html file
<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})
After the table displays some data correctly, you must also make sure the filter formset is displayed. I usually use something like this (in this case using {% 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 %}
In case the filterset is assigned to the template context as filter
, which should be the case when using the example from django-tables2 filtering page