BabDev / Pagerfanta

Pagination library for PHP applications with support for several data providers
Other
383 stars 170 forks source link

Update default.html.twig and add page twig variable where missing #50

Closed rufinus closed 1 year ago

rufinus commented 1 year ago

So I was playing around with symfony/ux-live-components and modified a page with a pagerfanta grid to display some infos.

the only changs needed was to overwrite the template for the links. but unfortunatly the pager block doesnt have the page variable set in next/previous links. it would not hurt to have them in.

{%- extends '@Pagerfanta/twitter_bootstrap5.html.twig' -%}

{%- block page_link -%}
    <li class="page-item"><a class="page-link" data-action="live#update" data-value="{{ page }}" data-model="page">{{- page -}}</a></li>
{%- endblock page_link -%}

{%- block previous_page_link -%}
    <li class="page-item"><a class="page-link"  data-action="live#update" data-value="{{ page }}" data-model="page" rel="prev">{{- block('previous_page_message') -}}</a></li>
{%- endblock previous_page_link -%}

{%- block next_page_link -%}
    <li class="page-item"><a class="page-link"  data-action="live#update" data-value="{{ page }}" data-model="page" rel="next">{{- block('next_page_message') -}}</a></li>
{%- endblock next_page_link -%}

For reference if someone want to try this:

#[AsLiveComponent('candidates')]
class CandidatesComponent
{
    use DefaultActionTrait;

    #[LiveProp(writable: true)]
    public int $page = 1;

    #[LiveProp()]
    public ?Pagerfanta $pagerfanta = null;

    public function getCandidates(): Collection|iterable
    {
        $queryBuilder = $this->getCandidatesQueryBuilder();
        $this->pagerfanta = new Pagerfanta(new QueryAdapter($queryBuilder));
        $this->pagerfanta->setCurrentPage($this->page);

        return $this->pagerfanta->getCurrentPageResults();
    }
}
<table>
            {% for candidate in this.candidates %}
               // print data
            {% endfor %}
</table>
{{ pagerfanta(this.pagerfanta, {'omitFirstPage': true }) }}
mbabker commented 1 year ago

Thanks, released in 4.2.0.