1 回答
TA贡献1757条经验 获得超7个赞
我最终只是将 sqs 查询发布到排序的 Django 模板,然后重新组合并使用查询对每一列进行分页。
下面的示例:
<div class="col-lg-12">
<div class="row">
{% if query %}
{% regroup results|dictsort:"model_name" by model_name as grouped_objects %}
{% for ct in grouped_objects %}
<div class="col-6 col-lg-3">
<h3 class="text-capitalize">{{ ct.grouper|friendly_search_name }}</h3>
<ul id="pager_{{ ct.grouper }}" class="pagination-sm mt-1"></ul>
<div id="pager_content_{{ ct.grouper }}">
{% for result in ct.list %}
<div class="col-12 results item">
<div class="pt-4 border-bottom">
<a class="page-url h4 text-primary" href="{{ result.object.get_absolute_url }}">{{ result.object.get_search_title }}</a>
<p class="page-description mt-1 text-muted"> {{ result.object.get_search_text|safe }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
{% empty %}
<p class="lead">Although I am a guru, I am lacking sentience. One must ask a specific question to recieve a specific answer.</p>
{% endfor %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</div>
</div>
jQuery:
{% block extrajs %}
<script src="{% static 'scripts/twbs-pagingation/jquery.twbsPagination.js' %}" type="text/javascript"></script>
<script type="text/javascript">
window.jQuery(function(){
var items_per_page = 4;
function hide_all(child) {
child.removeClass('active');
}
function show_page(child, num, items_per_page) {
hide_all(child);
child.slice((num-1)*items_per_page, num*items_per_page).addClass('active');
}
{% regroup results|dictsort:"model_name" by model_name as grouped_objects %}
{% for ct in grouped_objects %}
var $content_{{ ct.grouper }} = $('#pager_content_{{ ct.grouper }}');
var $child_{{ ct.grouper }} = $content_{{ ct.grouper }}.children();
$('#pager_{{ ct.grouper }}').twbsPagination({
totalPages: Math.ceil($child_{{ ct.grouper }}.length/items_per_page),
visiblePages: 5,
onPageClick: function (event, page) {
show_page($child_{{ ct.grouper }}, page, items_per_page);
}
});
{% endfor %}
});
</script>
{% endblock extrajs %}
{% block extrastyle %}
<style type="text/css">
.item {
display: none;
}
.item.active {
display: block;
}
</style>
{% endblock extrastyle %}
添加回答
举报