Giter Club home page Giter Club logo

avlistbundle's Introduction

AvListBundle

Easily make paginate and orderable list in Symfony2

  1. Install

Add this in your composer :

"appventus/avlist-bundle": "dev-master"
  1. How to use it ?

a) Controller

This is a classic action to list an entity :

use Symfony\Component\HttpFoundation\Request;

class FooController extends Controller
{
    /**
     * Lists all Foo entities.
     *
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $qb = $em->getRepository(MyBundle:Foo)->findAll();

        // Create an AvList
        $list = $this->get('av_list')->getList($qb, null, array(
            'max_per_page' => 20,
        ));

        $list->addColumn('title', null, 'myList.column.title', true, 'entity.title');
        $list->addColumn('startDate', array(array("name" => "date", "params" => array('d/m/Y'))), 'myList.column.startDate', true, 'entity.startDate');


        return array(
            'list' => $list,
        );
        
        if ($this->get('request')->isXmlHttpRequest()) {
            return $this->render($list->getTemplate(), array(
                'list' => $list,
            ));
        } else {
            return array(
                'list' => $list,
            );
        }
    }
}

The addColunmn allow you to add/define the columns to display in the list. In order :

    1. You can define the field to display
    1. Is there some special Twig filters to use on the variable to display correctly ?
    1. The label (th)
    1. Do we may sort on this column ?
    1. If so, on which field

b) View (optional):

If you want to override default views, you'll have to write an index with the layout, and a partial witch just contain the list:

index.html.twig :

{% extends "MyBundle::layout.html.twig" %}

{% block content %}
    <h1>Foo list</h1>
    {% include 'MyBundle:Foo:indexPartial.html.twig' with {'list':list}%}
{% endblock %}

Yeah, it's pretty minimalist, but it's very important.

And now our Partial :

{% if list.pager.nbResults > 0 %}
    <div id="{{ list.option('id') }}">
        <table class="table">
            <thead>
                <tr>
                    <th class="sortable" data-target="f.name">Name</th>
                    <th class="sortable" data-target="f.price">Price</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                {% for foo in list.pager %}
                    <tr>
                        <td>{{ foo.name }}</td>
                        <td>{{ foo.price }}</td>
                        <td>{{ foo.name }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
        {% include 'AvListBundle:AvList:control.html.twig' with {'list':list}%}
    </div>
{% else %}
    <p>No Foos :(</p>
{% endif %}

And voila !

  1. How it works ?

In the partial view, you set on the th tag a class sortable and an id with the value of the field you want to search on. In our exemple, the entity Foo has the alias "f" in the query builder, so to sort by Foo's name, you set the following th id: "f.name".

  1. Options

This bundle use the PagerFanta bundle to build paginator, more specificly the TwitterBootstrapView. Options are available for this view, and you can pass them as second argument when you instanciate AvList:

$list = $this->get('av_list');
$list->addOption("proximity", 3);
$list->addOption("previous_message", "Précédent");

Please refer to PagerFanta doc for more information

  1. Multiple list in a page

If you need to have several lists components in a single page, you will have to define the route option (to specify a different url to call that the request one). Optionnally and if your route requires params, you obviously may pass an array through the route_parameters option. Also, you won't be able to use the av_list service because of a singleton pattern issue (if you use the service, you always will use the same object and so on, you will overwrite the values instead of create a new list.

/**
 * Lists all Help\FaqItem entities.
 *
 * @Route("/", name="admin_myentity_list")
 * @Route("/ajax/cat/{id}", name="ajax_admin_myentity_list_by_category")
 * @Method("GET")
 * @Template()
 */
public function indexAction($id = null)
{
    $categories = Your_function();
    foreach ($categories as $key => $category) {
        $list = new AvList($request, $this->get('templating'));
        $list->setQueryBuilder($em->getRepository('MyBundle:MyEntity')->createQueryBuilder('e')->where('e.category = :category')->setParameter('category', $category));
        $list->setTemplate($partialTemplate);
        $list->setOptions(array(
                'route'            => 'ajax_admin_myentity_list_by_category',
                'route_parameters' => array('id' => $category->getId()),
                'theme'            => 'range',
                'maxPerPage'       => 5,
                'container_class'  => $category->getId().'-myentity-list',
                'container_id'     => $category->getId().'-myentity-list'
        ));
        $params["lists"][$category->getId()] = $list;
    }
}
  1. Custom control theme

By default, AvList provides one theme : range. This theme adds the basics actions to get something like this : <- [10,20] on 100 ->

If you want to, you can create your own theme by setting your theme name as "theme" option and to put it in the app/Resources/AvListBundle/views/AvList/yourCustomThemeFileName.html.twig

avlistbundle's People

Contributors

paulandrieux avatar lenybernard avatar bitdeli-chef avatar charlie-lucas avatar juliensnz avatar hyukchan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.