sorting works, pagination works.

This commit is contained in:
Sanj 2011-09-10 07:10:47 +05:30
parent 505656c3f3
commit 152d7ed9dc
6 changed files with 178 additions and 19 deletions

View File

@ -205,16 +205,16 @@ class ItfModel(models.Model):
@classmethod @classmethod
def get_list(kls, data): def get_list(kls, data):
options = { options = {
'page_no': 1, 'page': 1,
'list_size': 12, 'count': 12,
'search': '', 'search': '',
'sort': [], 'sort': [],
'range': [0,50] 'range': [0,50]
} }
options.update(data) options.update(data)
ret = [] ret = []
page_no = options['page_no'] page_no = options['page']
list_size = options['list_size'] list_size = options['count']
try: try:
qset = kls.get_qset() qset = kls.get_qset()
except: except:
@ -225,6 +225,7 @@ class ItfModel(models.Model):
qset = kls.fts(qset, search) qset = kls.fts(qset, search)
sort = options['sort'] sort = options['sort']
if sort != []: if sort != []:
for s in sort: for s in sort:
if s['operator'] == '-': if s['operator'] == '-':
@ -249,7 +250,11 @@ class ItfModel(models.Model):
for r in results.object_list: for r in results.object_list:
ret.append(r.list_dict()) ret.append(r.list_dict())
return ret return {
'page_no': page_no,
'num_pages': paginator.num_pages,
'items': ret
}
@classmethod @classmethod
def get_qset(kls): def get_qset(kls):

View File

@ -32,9 +32,22 @@ class ModuleTab(models.Model):
def model_class(self): def model_class(self):
return self.model.model.model_class() return self.model.model.model_class()
def get_absolute_url(self): #FIXME!!
return self.slug
def get_list(self, options): def get_list(self, options):
return self.model_class().get_list(options) return self.model_class().get_list(options)
def get_dict(self):
return {
'id': self.id,
'slug': self.slug,
'title': self.title,
'text': self.text,
'has_list': self.model.has_list,
'page': self.get_list({}),
'sorts': [s.get_dict() for s in self.model.modelsort_set.all()]
}
class ModelExtra(models.Model): class ModelExtra(models.Model):
model = models.ForeignKey(ContentType, help_text="Chose from the list of death at your own peril.") model = models.ForeignKey(ContentType, help_text="Chose from the list of death at your own peril.")
@ -57,6 +70,12 @@ class ModelSort(models.Model):
def __unicode__(self): def __unicode__(self):
return self.friendly_name return self.friendly_name
def get_dict(self):
return {
'operator': self.operator,
'field_name': self.field_name,
'friendly_name': self.friendly_name
}
class ModelButton(models.Model): class ModelButton(models.Model):
name = models.CharField(max_length=64, help_text="Name of button, eg. Download") name = models.CharField(max_length=64, help_text="Name of button, eg. Download")

View File

@ -4,5 +4,6 @@ import views
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^get_list$', views.get_list), (r'^get_list$', views.get_list),
(r'^get_details$', views.get_details), (r'^get_details$', views.get_details),
(r'^get_tab$', views.get_tab),
(r'^(?P<module_slug>.*)$', views.main), (r'^(?P<module_slug>.*)$', views.main),
) )

View File

@ -18,14 +18,32 @@ def main(request, module_slug):
}) })
return render_to_response("noel/insidepage.html", context) return render_to_response("noel/insidepage.html", context)
def get_tab(request):
tab_id = request.GET.get("tab_id", 0)
tab = get_object_or_404(ModuleTab, pk=tab_id)
return render_to_json_response(tab.get_dict())
def get_list(request): def get_list(request):
tab_id = request.GET.get("tab_id", 0) tab_id = request.GET.get("tab_id", 0)
tab = get_object_or_404(ModuleTab, pk=tab_id) tab = get_object_or_404(ModuleTab, pk=tab_id)
sortString = request.GET.get("sort", "")
if sortString == "":
sortArray = []
else:
sortOperator = sortString[0]
sortField = sortString[1:]
sortArray = [{
'operator': sortOperator,
'key': sortField
}]
list_options = { list_options = {
'search': request.GET.get("search", ""), 'search': request.GET.get("search", ""),
'sort': request.GET.get("sort", ""), 'sort': sortArray,
'page': request.GET.get("page_no", 1) 'page': request.GET.get("page", 1),
# 'count': request.GET.get("count", 12) #FIXME: make list_length either in settings.py or config per model
} }
object_list = tab.get_list(list_options) object_list = tab.get_list(list_options)
return render_to_json_response(object_list) return render_to_json_response(object_list)

View File

@ -19,6 +19,13 @@ $('#listLeft ul li a').live("click", function() {
return false; return false;
}); });
/* Get a jquery <option> row for a sort field, requires s.operator, s.field_name, s.friendly_name */
function getOption(s) {
return $('<option />').val(s.operator + s.field_name).text(s.friendly_name);
}
/* Get a Jquery List Row object for an item - requires item.id and item.title*/
function getLi(item) { function getLi(item) {
var titleLength = 30; var titleLength = 30;
var $li = $('<li />').addClass("tabListItem"); var $li = $('<li />').addClass("tabListItem");
@ -29,8 +36,20 @@ function getLi(item) {
} }
$(function() { $(function() {
/* search button toggle */
$('.searchInnerField').hide();
$('#searchInner').toggle(function() {
$('.searchInnerField').slideDown();
}, function() {
$('.searchInnerField').slideUp();
});
/* search button end */
$('#listLeft ul li a').eq(0).click(); $('#listLeft ul li a').eq(0).click();
// alert("hi"); // alert("hi");
$('.tabCategory').click(function() { $('.tabCategory').click(function() {
var $this = $(this); var $this = $(this);
if ($this.hasClass('.innerSelected')) { if ($this.hasClass('.innerSelected')) {
@ -42,19 +61,112 @@ $(function() {
$('.displayedTab').removeClass("displayedTab"); $('.displayedTab').removeClass("displayedTab");
var tabId = $this.attr("data-id"); var tabId = $this.attr("data-id");
$('#tabText_' + tabId).addClass("displayedTab"); $('#tabText_' + tabId).addClass("displayedTab");
$('#loadingList').show(); doListLoading();
$('.tabListItem').remove(); $.getJSON("/m/get_tab", {
$.getJSON("/m/get_list", {
'tab_id': tabId, 'tab_id': tabId,
'page_no': 1 // 'page_no': 1
}, function(data) { }, function(data) {
$('#loadingList').hide(); // $('#loadingList').hide();
stopListLoading(data.page);
/* begin if for has_list, ideally make into separate render function */
if (data.has_list) {
/* handle sort options on list */
$select = $('#orderBySelect');
$select.empty();
var sorts = data.sorts;
if (sorts.length == 0) { $select.hide(); } else { $select.show(); }
for (var j=0; j<sorts.length; j++) {
var s = sorts[j];
var $opt = getOption(s);
$select.append($opt);
}
/* end handle sort */
for (var i=0; i<data.length; i++) { /* handle displaying initial list on page */
var $li = getLi(data[i]); displayList(data.page.items);
/* end display initial list */
}
/* end if has_list */
});
});
$('#orderBySelect').change(function(e) {
var sortString = $(this).val();
doListLoading();
$.getJSON("/m/get_list", {
'tab_id': $('.innerSelected').attr("data-id"),
'sort': sortString
}, function(data) {
stopListLoading(data);
displayList(data.items);
});
});
$('#triangleUp').click(function() {
goPreviousPage();
});
$('#triangleDown').click(function() {
goNextPage();
});
});
function displayList(items) {
for (var i=0; i<items.length; i++) {
var $li = getLi(items[i]);
$('#listLeft ul').append($li); $('#listLeft ul').append($li);
} }
$('#listLeft ul li a').eq(0).click(); $('#listLeft ul li a').eq(0).click();
}
function doListLoading() {
$('#loadingList').show();
$('.tabListItem').remove();
}
function stopListLoading(page) {
$('#currPageNo').text(page.page_no);
$('#totalPages').text(page.num_pages);
$('#loadingList').hide();
if (page.page_no == 1) {
$('#triangleUp').hide();
} else {
$('#triangleUp').show();
}
if (page.page_no == page.num_pages) {
$('#triangleDown').hide();
} else {
$('#triangleDown').show();
}
}
function goNextPage() {
var currPage = parseInt($('#currPageNo').text());
var totalPages = parseInt($('#totalPages').text());
if (currPage < totalPages) {
getPage(currPage + 1);
}
}
function goPreviousPage() {
var currPage = parseInt($('#currPageNo').text());
if (currPage > 1) {
getPage(currPage - 1);
}
}
function getPage(pageNo) {
doListLoading();
$.getJSON("/m/get_list", {
'tab_id': $('.innerSelected').attr("data-id"),
'page': pageNo,
'sort': $('#orderBySelect').val()
}, function(page) {
stopListLoading(page);
displayList(page.items);
}); });
}); }
});

View File

@ -51,7 +51,7 @@
{% endfor %} {% endfor %}
<!--is this select okay, my forms are a bit weak, do we need action and method here?--><form class="selectMenu" action="" method="post"> <!--is this select okay, my forms are a bit weak, do we need action and method here?--><form class="selectMenu" action="" method="post">
<span>Order by</span><!--Can i avoid a p tag here to keep it inline--> <span>Order by</span><!--Can i avoid a p tag here to keep it inline-->
<select>Select <select id="orderBySelect">Select
<option value="atoz">A-Z</option> <option value="atoz">A-Z</option>
<option value="ztoa">Z-A</option> <option value="ztoa">Z-A</option>
<option value="title">Title</option> <option value="title">Title</option>
@ -76,6 +76,10 @@
<div id="triangleDown">&#x25BC;</div> <div id="triangleDown">&#x25BC;</div>
</div> </div>
<div id="pagination" style="display:none;"> <!-- Please, please let me display pagination info -->
<span id="currPageNo">1</span>
<span id="totalPages"></span>
</div>
</div> </div>