使用knpPaginator

时间:2017-05-18 14:28:39

标签: symfony knppaginator

我有一个搜索栏,可以按名称查找分子,然后在同一页面的搜索栏下显示包含此名称的所有分子。

我添加了knpPaginator并且它完美无缺,但当我转到下一页时我必须重新加载表格并显示下一个分子。

我的问题是:有没有办法避免表单重新加载? 我可以在另一页显示结果,但我更喜欢这样保留:

i show you my con控制器:

$form = $this->createForm('NcstoxBundle\Form\FindMolNameType');
    $form->handleRequest($request);
    $em = $this->getDoctrine()->getManager();
    $molecules = $em->getRepository('NcstoxBundle:Molecule')->findAll();

    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();
        $molecules = $em->getRepository('NcstoxBundle:Molecule')->findByMolName($data);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $molecules, /* query NOT result */
            $this->get('request')->query->getInt('page', 1)/*page number*/,
            10/*limit per page*/
        );

        return $this->render('NcstoxBundle:Default:molNameResult.html.twig', array(
            'form' => $form->createView(),
            'molecules' => $molecules,
            'pagination' => $pagination
        ));
    }

我的观点:

<div class="search">
    <h3>Make a Search by Molecule Name :</h3>
    {{ form_start(form) }}
    {{ form_widget(form, {'attr': {'title': 'test'}}) }}
    <input class="btn btn-primary" type="submit" value="Search">
    {{ form_end(form) }}
</div>
<h3>Search Results :</h3>
<div class="count">
    {{ pagination.getTotalItemCount }} Molecules
</div>
<table class="table table-hover table-condensed">
    <thead>
    <tr>
        <th>{{ knp_pagination_sortable(pagination, 'Name', 'a.name') }}</th>
    </tr>
    </thead>
    <tbody>
    {% for molecule in pagination %}
        <tr>
            <td><a href="{{ path('molSheet', {'id':molecule.id}) }}">{{ molecule.mainName }}</a></td>
        </tr>
    {% else %}
        <tr>
            <td>There isn't molecule corresponding to your query</td>
        </tr>
    {% endfor %}
    </tbody>
    <tfoot></tfoot>
</table>
<div class="navigation fin taille">
    {{ knp_pagination_render(pagination) }}
</div>

请放纵,StackOverflow说我有被封锁的危险,我是法国人,所以英语不是我的主要语言。

1 个答案:

答案 0 :(得分:0)

KnpPaginatorBundle有一个'秘密'(未记录)过滤器。

您不需要自己创建表单,只需将其应用于模板中:

{{ knp_pagination_filter(pagination, {'a.name': 'Name'}) }}

然后

KnpPaginator将管理过滤。

有关更多信息,请点击此处查看:https://github.com/KnpLabs/KnpPaginatorBundle/issues/225

希望有所帮助:)

相关问题