Skip to content

Commit 4e46328

Browse files
authored
Tablesorter (#27)
* Page Sorter in component filter * bug resolved for page sorter * Search page Paginated * date sorter removed Co-authored-by: Saurabh <MetalBlazer>
1 parent 15edcb0 commit 4e46328

6 files changed

Lines changed: 152 additions & 30 deletions

File tree

static/website/templates/ajax-keyword-search.html

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% load helpers %}
2-
2+
{% load forumdata %}
33
{% if questions %}
44
<br>
55
<table class="table table-striped table-bordered table-hover">
@@ -79,6 +79,33 @@
7979
</tr>
8080
{% endfor %}
8181
</table>
82+
83+
<ul class="pagination pull-right">
84+
{% if questions %}
85+
{% if questions.has_previous %}
86+
<li><a href="#" onclick="make_ajax_call(1);">&laquo; <small>First</small></a></li>
87+
<li><a href="#" onclick="make_ajax_call('{{ questions.previous_page_number }}');">&laquo; <small>Prev</small></a></li>
88+
{% else %}
89+
<li class="disabled"><a href="#">&laquo;</a></li>
90+
{% endif %}
91+
{% for num in questions.paginator.page_range|paginator_page_cutter:questions.number %}
92+
{% ifequal num questions.number %}
93+
<li class="active"><a href="#" onclick="make_ajax_call();">{{ num }}<span class="sr-only">(current)</span></a></li>
94+
{% else %}
95+
<li><a href="#" onclick="make_ajax_call('{{ num }}');">{{ num }}</a></li>
96+
{% endifequal %}
97+
{% endfor %}
98+
{% if questions.has_next %}
99+
<li><a href="#" onclick="make_ajax_call('{{ questions.next_page_number }}');"><small>Next</small> &raquo;</a></li>
100+
{% if questions.paginator.page_range|length > 10 %}
101+
<li><a href="#" onclick="make_ajax_call('{{questions.paginator.num_pages}}');"><small>Last</small> &raquo;</a></li>
102+
{% endif %}
103+
{% else %}
104+
<li class="disabled"><a href="#">&raquo;</a></li>
105+
<li class="disabled"><a href="#" onclick="make_ajax_call('{{ questions.paginator.num_pages }}');"><small>Last</small> &raquo;</a></li>
106+
{% endif %}
107+
{% endif %}
108+
<ul>
82109
{% else %}
83110
<h4>No results found . . .</h4>
84111
{% endif %}
@@ -87,4 +114,23 @@ <h4>No results found . . .</h4>
87114
<script>
88115
$('span').tooltip();
89116
</script>
117+
<script type= "text/javascript" >
118+
function make_ajax_call(page){
119+
var key = $search_key.val();
120+
$keyword_search_results = $("#keyword-search-results");
121+
$.ajax({
122+
url: "/ajax-keyword-search/",
123+
type: "POST",
124+
data: {
125+
key: key,
126+
page:page,
127+
},
128+
dataType: "html",
129+
success: function(data) {
130+
$keyword_search_results.html(data);
131+
console.log(data);
132+
}
133+
});
134+
}
135+
</script>
90136
{% endblock %}

static/website/templates/filter.html

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
{% load helpers %}
33

44
{% block content %}
5-
<script>
6-
$(document).ready(function(){
7-
$("#listTable").tablesorter({
8-
headers:{ 3:{sorter:false }
9-
},
10-
11-
});
12-
13-
});
14-
</script>
5+
{% load forumdata %}
156
{% if questions %}
167
<h5>
178
These are the similar questions in:
@@ -37,17 +28,7 @@ <h5>
3728

3829
<table id = "listTable" class="tablesorter-blue">
3930
<thead>
40-
<tr>
41-
<th> FOSS </th>
42-
<th> Tutorial</th>
43-
<th> Min </th>
44-
<th> Sec </th>
45-
<th> Question</th>
46-
<th> Date</th>
47-
<th> Views</th>
48-
<th> Answers</th>
49-
<th> User</th>
50-
</tr>
31+
{% get_sortable_header header ordering request.GET %}
5132
</thead>
5233
<tbody>
5334
{% for question in questions %}
@@ -118,6 +99,33 @@ <h5>
11899
{% endfor %}
119100
</tbody>
120101
</table>
102+
103+
<ul class="pagination pull-right">
104+
{% if questions %}
105+
{% if questions.has_previous %}
106+
<li><a href="?{{ request.GET|combine_get_values }}page=1">&laquo; <small>First</small></a></li>
107+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.previous_page_number }}">&laquo; <small>Prev</small></a></li>
108+
{% else %}
109+
<li class="disabled"><a href="#">&laquo;</a></li>
110+
{% endif %}
111+
{% for num in questions.paginator.page_range|paginator_page_cutter:questions.number %}
112+
{% ifequal num questions.number %}
113+
<li class="active"><a href="#">{{ num }}<span class="sr-only">(current)</span></a></li>
114+
{% else %}
115+
<li><a href="?{{ request.GET|combine_get_values }}page={{ num }}">{{ num }}</a></li>
116+
{% endifequal %}
117+
{% endfor %}
118+
{% if questions.has_next %}
119+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.next_page_number }}"><small>Next</small> &raquo;</a></li>
120+
{% if questions.paginator.page_range|length > 10 %}
121+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.paginator.num_pages }}"><small>Last</small> &raquo;</a></li>
122+
{% endif %}
123+
{% else %}
124+
<li class="disabled"><a href="#">&raquo;</a></li>
125+
<li class="disabled"><a href="?{{ request.GET|combine_get_values }}page={{ questions.paginator.num_pages }}"><small>Last</small> &raquo;</a></li>
126+
{% endif %}
127+
{% endif %}
128+
<ul>
121129
{% else %}
122130
<h4>There are no similar questions.</h4>
123131
{% endif %}

static/website/templates/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<script>
99
$(document).ready(function(){
1010
$("#listTable").tablesorter({
11-
headers:{ 3:{sorter:false }
11+
headers:{
12+
5:{sorter:false}
1213
},
1314

1415
});

static/website/templates/questions.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,30 @@ <h5>
8686
</table>
8787
</div>
8888
<ul class="pagination pull-right">
89+
{% if questions %}
8990
{% if questions.has_previous %}
90-
<li><a href="?page={{ questions.previous_page_number }}">&laquo; <small>Prev</small></a></li>
91+
<li><a href="?{{ request.GET|combine_get_values }}page=1">&laquo; <small>First</small></a></li>
92+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.previous_page_number }}">&laquo; <small>Prev</small></a></li>
9193
{% else %}
9294
<li class="disabled"><a href="#">&laquo;</a></li>
9395
{% endif %}
94-
95-
{% for num in questions.paginator.page_range %}
96+
{% for num in questions.paginator.page_range|paginator_page_cutter:questions.number %}
9697
{% ifequal num questions.number %}
9798
<li class="active"><a href="#">{{ num }}<span class="sr-only">(current)</span></a></li>
9899
{% else %}
99100
<li><a href="?{{ request.GET|combine_get_values }}page={{ num }}">{{ num }}</a></li>
100101
{% endifequal %}
101102
{% endfor %}
102-
103103
{% if questions.has_next %}
104-
<li><a href="?page={{ questions.next_page_number }}"><small>Next</small>&raquo;</a></li>
104+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.next_page_number }}"><small>Next</small> &raquo;</a></li>
105+
{% if questions.paginator.page_range|length > 10 %}
106+
<li><a href="?{{ request.GET|combine_get_values }}page={{ questions.paginator.num_pages }}"><small>Last</small> &raquo;</a></li>
107+
{% endif %}
105108
{% else %}
106109
<li class="disabled"><a href="#">&raquo;</a></li>
110+
<li class="disabled"><a href="?{{ request.GET|combine_get_values }}page={{ questions.paginator.num_pages }}"><small>Last</small> &raquo;</a></li>
107111
{% endif %}
112+
{% endif %}
108113
<ul>
109114

110115
{% endblock %}

website/templatetags/forumdata.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,28 @@ def combine_get_values(getValue, excludes = ['page']):
4040
values += k+'='+v+'&'
4141
return values
4242

43+
def paginator_page_cutter(page_range, current_page):
44+
page_count = len(page_range)
45+
if page_count <= 11:
46+
return page_range
47+
start_page = current_page - 5
48+
end_page = current_page + 5
49+
if current_page <= 11:
50+
if current_page < 6:
51+
start_page = 1
52+
end_page = 11
53+
else:
54+
tmp = current_page - 5
55+
start_page = tmp
56+
end_page = current_page + 5
57+
if page_count < end_page:
58+
end_page = page_count
59+
tmp = end_page - current_page
60+
start_page = start_page - (5 - tmp)
61+
return list(range(start_page, end_page+1))
4362

4463
register.filter('reset_get_values', reset_get_values)
4564
register.filter('reset_get_value', reset_get_value)
4665
register.inclusion_tag('website/templates/sortable_header.html')(get_sortable_header)
4766
register.filter('combine_get_values', combine_get_values)
67+
register.filter('paginator_page_cutter', paginator_page_cutter)

website/views.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,37 @@ def filter(request, category=None, tutorial=None, minute_range=None, second_rang
250250
if 'qid' in request.GET:
251251
context['qid'] = int(request.GET['qid'])
252252

253-
context['questions'] = questions.order_by('category', 'tutorial', 'minute_range', 'second_range')
253+
#context['questions'] = questions.order_by('category', 'tutorial', 'minute_range', 'second_range')
254+
questions = questions.annotate(total_answers=Count('answer'))
255+
raw_get_data = request.GET.get('o', None)
256+
257+
header = {
258+
1: SortableHeader('category', True, 'Foss'),
259+
2: SortableHeader('tutorial', True, 'Tutorial Name'),
260+
3: SortableHeader('minute_range', True, 'Mins'),
261+
4: SortableHeader('second_range', True, 'Secs'),
262+
5: SortableHeader('title', True, 'Title'),
263+
6: SortableHeader('date_created', True, 'Date'),
264+
7: SortableHeader('views', True, 'Views'),
265+
8: SortableHeader('total_answers', 'True', 'Answers'),
266+
9: SortableHeader('username', False, 'User')
267+
}
268+
269+
tmp_recs = get_sorted_list(request, questions, header, raw_get_data)
270+
ordering = get_field_index(raw_get_data)
271+
paginator = Paginator(tmp_recs, 20)
272+
page = request.GET.get('page')
273+
try:
274+
questions = paginator.page(page)
275+
except PageNotAnInteger:
276+
questions = paginator.page(1)
277+
except EmptyPage:
278+
questions = paginator.page(paginator.num_pages)
279+
context = {
280+
'questions': questions,
281+
'header': header,
282+
'ordering': ordering
283+
}
254284
return render(request, 'website/templates/filter.html', context)
255285

256286

@@ -560,7 +590,19 @@ def ajax_keyword_search(request):
560590
key = request.POST['key']
561591
questions = Question.objects.filter(
562592
Q(title__icontains=key) | Q(category__icontains=key) |
563-
Q(tutorial__icontains=key) | Q(body__icontains=key), status=1)
593+
Q(tutorial__icontains=key) | Q(body__icontains=key), status=1
594+
).order_by('-date_created')
595+
paginator = Paginator(questions, 20)
596+
page = request.POST.get('page')
597+
if page:
598+
page = int(request.POST.get('page'))
599+
questions = paginator.page(page)
600+
try:
601+
questions = paginator.page(page)
602+
except PageNotAnInteger:
603+
questions = paginator.page(1)
604+
except EmptyPage:
605+
questions = paginator.page(paginator.num_pages)
564606
context = {
565607
'questions': questions
566608
}

0 commit comments

Comments
 (0)