chicken burger
chicken burger

Reputation: 774

sorting with KnpPaginator

I have an issue trying to make my sorting with KnpPaginator works. I followed many links and try a lot of iteration in my code, it can't seem to work properly. i'm missing something but can't locate it The only thing that works at the moment si the table headers where i can click on it but nothing happens except refreshing the page and refreshing me back on pagination 1. I'm working on symfony 2.3

Here is my bundle configuration

knp_paginator:
page_range: 5                      # 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: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig'     # sliding pagination controls template
    sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

Here is my controller where I actually setted up Knp with the getRepository

private function resultsAction(Request $request, User $user, $type, $archive)
{
    $em = $this->getDoctrine()->getManager();

    $query = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);

    $paginator = $this->get('knp_paginator');
    $results = $paginator->paginate(
        $query,
        $request->query->getInt('page',1),
        $request->query->getInt('limit',50)
    );

    return array("results" => $results, "archive" => $archive);
}

public function offreAction(Request $request, User $user, $archive = false)
    {
        return $this->resultsAction($request, $user, Operation::OFFRE_COMMERCIALE, $archive);
    }

here is my repo where the query is done:

public function getQueryByTypeAndPro($type, User $user, $archive)
    {
        return $this->createQueryBuilder("opn")
            ->andWhere("opn.type = :type")
            ->setParameter("type", $type)
            ->andWhere("opn.resellerId = :reseller")
            ->setParameter("reseller", $user->getId())
            ->andWhere("opn.archive = :archive")
            ->setParameter('archive', $archive)
            ->orderBy("opn.dateCreation", "DESC")
            ->getQuery()
        ;
    }

And here is my view where I try to make things work with Knp

  <tr>
       <th>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.type') }}</th>
       <th>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.resellerId') }}</th>
      <th>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.??') }}</th>
      <th>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.??') }}</th>
      <th>{{ knp_pagination_sortable(results, 'commerce.achat.encours.etat'|trans, 'opn.??') }}</th>
  </tr>

And in my table, when I click on it refresh the page but doesn't sort it. I'm having a hard time understand how to make that work?

The place where I putted 'opn.??' its because I don't know what to put at that particular place, I don't seem to understand the query

I would like the last dated item to be first, but being able to sort it with Knp

Upvotes: 2

Views: 10769

Answers (2)

chicken burger
chicken burger

Reputation: 774

Okay i succeeded in making that work, here is what I've done.

config.yml

knp_paginator:
page_range: 5                      # 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: false                # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
    pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig'     # sliding pagination controls template
    sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

repo

public function getQueryByTypeAndPro($type, User $user, $archive)
    {
        return $this->createQueryBuilder("opn")
            ->andWhere("opn.type = :type")
            ->setParameter("type", $type)
            ->andWhere("opn.resellerId = :reseller")
            ->setParameter("reseller", $user->getId())
            ->andWhere("opn.archive = :archive")
            ->setParameter('archive', $archive)
            ->orderBy("opn.dateCreation", "DESC")
            ->getQuery()
        ;
    }

controller

private function resultsAction(Request $request, User $user, $type, $archive)
    {
        $em = $this->getDoctrine()->getManager();

        $paginator = $this->get('knp_paginator');

        $qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);

        $results = $paginator->paginate(
            $qb,
            $request->query->get('page',1),
            $request->query->get('limit',50),
            [
                'defaultSortFieldName'      => 'opn.dateCreation',
                'defaultSortDirection' => 'desc'
            ]
        );

        return array("results" => $results, "archive" => $archive);
    }

and twig

    <tr>
          <th{% if results.isSorted('opn.id') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.id') }}</th>
          <th{% if results.isSorted('opn.vehiculeMarque') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.vehiculeMarque') }}</th>
          {% if typeOffre is defined and typeOffre == 'devisWeb' %}<th>Financement</th>{% endif %}
          <th{% if results.isSorted('opn.clientNom') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.clientNom') }}</th>
          <th{% if results.isSorted('opn.dateCreation') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.dateCreation') }}</th>
          <th>{{ 'commerce.achat.encours.etat'|trans }}</th>
         <th class="sorting_disabled">{{ 'commerce.achat.action'|trans }}</th>
        <th class="sorting_disabled">{{ 'commerce.vente.operation.basculer'|trans }}</th>
 </tr>

and so that works

Upvotes: 4

T. Abdelmalek
T. Abdelmalek

Reputation: 428

remove ->orderBy("opn.dateCreation", "DESC"). KnpPaginator have special Listener and Walker that can modify your query with correct ORDER BY based on request and then eqecute an paginate it for you.

Config sortable look at here. Translation look at here.

Upvotes: 2

Related Questions