Skip to content

Commit

Permalink
added pagination template, ajax-enabled js and instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
joonsp committed Aug 27, 2012
1 parent 5c34392 commit 4ed1076
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,28 @@ class App.AjaxForm.YourCustomClass extends App.AjaxForm.Default

Probably most easiest way to use this bundle is to use it with ElasticSearch because XiSearchBundle provides you
premade implementation for it. This however requires [FOQElasticaBundle](https://github.com/Exercise/FOQElasticaBundle) to work.

## Pagination
Pagination uses knp-pagination bundle, and it's on by default

Suppose the search is retreived via ajax, there's a jquery plugin included that binds the received pagination's logic to the actual search form with javascript.

As the pagination generally uses the search forms default fields, only the indices need configuration. page, term and submit button values can also be overridden
``` coffee
$('#search-result-container').xiSearchPaginate
indices: ['#xi_searchbundle_searchtype_index_0', '#xi_searchbundle_searchtype_index_1', ...]
```

For these bindigs to work, configure the knp pagination to use the proper pagination template
```yml
knp_paginator:
page_range: 9 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: XiSearchBundle:Pagination:sliding.html.twig # sliding pagination controls template
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template
```
28 changes: 28 additions & 0 deletions Resources/coffee/pagination.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$ = jQuery
$.fn.xiSearchPaginate = (options) ->
defaultOptions =
page: '#xi_searchbundle_searchtype_page'
submit: '#xi_searchbundle_searchtype_submit'
term: '#xi_searchbundle_searchtype_term'
indices: []

options = $.extend true, {}, defaultOptions, options

# change page
@.on "click", ".pagination a", ->
pageValue = $(this).attr("data-page")
$(options.page).val pageValue
$(options.submit).find("button").trigger "click", silent: true

# reset page
$(options.term).change ->
$(options.page).val 1

if options.indices
for index in options.indices
$(index).change ->
$(options.page).val 1

$(options.submit).find("button").click (event, data) ->
unless data and data.silent
$(options.page).val 1
40 changes: 40 additions & 0 deletions Resources/views/Pagination/sliding.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{# default Sliding pagination control implementation #}

{% if pageCount > 1 %}
<div class="pages">
{% if first is defined and current != first %}
<span class="first">
<a href="#main-content" data-page="{{first}}">&lt;&lt;</a>
</span>
{% endif %}

{% if previous is defined %}
<span class="previous">
<a href="#main-content" data-page="{{previous}}">&lt;</a>
</span>
{% endif %}

{% for page in pagesInRange %}
{% if page != current %}
<span class="page">
<a href="#main-content" data-page="{{ page }}">{{ page }}</a>
</span>
{% else %}
<span class="current">{{ page }}</span>
{% endif %}

{% endfor %}

{% if next is defined %}
<span class="next">
<a href="#main-content" data-page="{{next}}">&gt;</a>
</span>
{% endif %}

{% if last is defined and current != last %}
<span class="last">
<a href="#main-content" data-page="{{last}}">&gt;&gt;</a>
</span>
{% endif %}
</div>
{% endif %}
2 changes: 1 addition & 1 deletion Resources/views/SearchForm/searchform.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<form action="{{ path('XiSearchBundle_search') }}" class="ajax-form article-form" method="post" {{ form_enctype(form) }}>
{{ form_widget(form.term, { 'attr': {'placeholder' : ('search.form.term.placeholder')|trans} }) }}
{{ form_rest(form) }}
<p>
<p id="xi_searchbundle_searchtype_submit">
<button type="submit">{{'search.form.submit_button'|trans}}</button>
</p>
</form>

0 comments on commit 4ed1076

Please sign in to comment.