问题描述:

I am pretty sure there has to be a clean and nice way to do this.

I need to offer the user the option to see all the rows of a table in a page (maybe pagination here would be awesome too) and then allow him to filter for those which has some value in some specific field. Also it would be great to allow re order this results.

Because I don't know from ahead which filter or sorting will apply I simply give all the set of data to my twig template which in turn print all those. Until here everything ok.

But this is the point where I dont know how to proceed...

So suppose you have:

ControllerDefault.php

...default header....

public function listAction()

{

$em = $this->getDoctrine()->getManager();

$entities = $em->getRepository('ACMEAppBundle:Item')->findAll();

return $this->render('ACMEAppBundle:Default:list.html.twig', array('entities'=>$entities));

}

list.html.twig

{% extends '::base.html.twig' %}

{% block content %}

<h3>List of items</h3>

<table>

<thead>

<tr>

<th>id</th>

<th>name</th>

<th>details</th>

</tr>

</thead>

<tbody>

{% for entity in entities %}

<tr>

<td>{{ entity.id }}</td>

<td>{{ entity.name }}</td>

<td>{{ entity.details }}</td>

</tr>

{% endfor %}

</tbody>

</table>

{% endblock %}

Would you use a text with a button for action and then trying to generate a new url by sending some kind of variable back to the controller? or maybe generating a url for a different action?

Would you prefer AJAX somehow on this page...if so? how would be the most basic implementation?

Any advise will be much appreciated since this is for real work and my endline is two days ahead (how I ended doing this is another story xD)

Thanks!!!

网友答案:

You need this: KnpPaginatorBundle

1) Add this to Your composer.json file (/composer.json):

{
    "require": {
        "knplabs/knp-paginator-bundle": "~2.4"
    }
}

2) Type in Your console in root dir of Your project:

composer update knplabs/knp-paginator-bundle

If You don't have composer: https://getcomposer.org/download/

3) Add this to Your config.yml (/app/config/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: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template

4) Register bundle in AppKernel.php (/app/AppKernel.php):

public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        // ...
    );
}

5) Now replace the listAction (ControllerDefault.php):

public function listAction()
{
    $em = $this->getDoctrine()->getManager();
    $dql   = "SELECT i FROM ACMEAppBundle:Item i";
    $query = $em->createQuery($dql);
    $paginator  = $this->get('knp_paginator');
    $entities = $paginator->paginate(
        $query,
        $this->get('request')->query->get('page', 1)/*page number*/,
        10/*limit per page*/
    );

    return $this->render('ACMEAppBundle:Default:list.html.twig', array('entities'=>$entities));
}

6) At last Your must change the view (list.html.twig):

{% extends '::base.html.twig' %}
{% block content %}
<h3>List of items ({{ entities.getTotalItemCount }})</h3>

<table>
    <thead>
    <tr> {# sorting of properties based on query components #}
        <th>{{ knp_pagination_sortable(entities , 'Id', 'i.id') }}</th>
        <th>{{ knp_pagination_sortable(entities , 'Name', 'i.name') }}</th>
        <th>{{ knp_pagination_sortable(entities , 'Details ', 'i.details ') }}</th>
    </tr>
    </thead>
    <tbody>
    {% for entity in entities %}
        <tr>

            <td>{{ entity.id }}</td>
            <td>{{ entity.name }}</td>
            <td>{{ entity.details }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

{# display navigation #}
<div class="pagination">
    {{ knp_pagination_render(entities) }}
</div>
{% endblock %}
相关阅读:
Top