sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

documentation on creating list view filter #2208

Closed abbood closed 10 years ago

abbood commented 10 years ago

i'm trying to create a dropdown menu fitler for a sonata list view page.. based on my search it has something to do with list-filters.. i googled it and got a cached (the original doesn't show up?) documentation page about 'the list view'

the cached page shows a TODO list under the filters documentation.. can someone refer me to a place where such documentation exists?

screen shot 2014-06-27 at 8 49 19 am

abbood commented 10 years ago

to make my question more specific

I inherited a sonata admin project that already has a free form textfield search in its admin pages.. which looks like this:

enter image description here

this is the corresponding code in App/AdminBundle/Resources/Views/Passenger/list.html.twig

{% extends 'SonataAdminBundle:CRUD:list.html.twig' %}

{% block list_filters %}
    {% if admin.datagrid.filters %}
        <form class="form-search"  action="{{ admin.generateUrl('list') }}" method="GET">
            <label class="control-label" style="padding-right: 10px;font-weight:bold;">{{ 'Search'|trans({}, 'SonataAdminBundle') }}</label>
            <div class="input-append">
                {% for filter in admin.datagrid.filters %}
                    {{ form_widget(form.children[filter.formName].children['value'], { 'attr': {'style': 'margin-bottom: 0px;', 'class': 'input-medium search-query'} }) }}
                {% endfor %}
                <input type="hidden" name="filter[_page]" id="filter__page" value="1" />
                <button type="submit" class="btn btn-primary">{{ 'Go'|trans({}, 'SonataAdminBundle') }}</button>
            </div>
            <a class="btn" href="{{ admin.generateUrl('list', {filters: 'reset'}) }}">{{ 'link_reset_filter'|trans({}, 'SonataAdminBundle') }}</a>
        </form>
    {% endif %}
{% endblock %}

from the code above.. the key line is

{% for filter in admin.datagrid.filters %}
    {{ form_widget(form.children[filter.formName].children['value'], ..}
{% endfor %}

this is its rendered html:

<input type="text" id="filter_search_value" name="filter[search][value]" .. />

my question is: where is this admin.datagrid.filters defined? how can i customize it? (ie how can i add my own datagrid.filter that is a dropdown)?

abbood commented 10 years ago

turns out by doing something like this under the configureDataGridFiltered in the PassengerAdmin.php page:

        ->add('isVerifiedBySMS', 'doctrine_mongo_callback', [
            'callback' => function ($queryBuilder, $alias, $field, $params) {
                    if ($params['value'] === null) {
                        return;
                    }
                    if ($params['value'] == 'true') {
                        $isUnverifiedBySms = false;
                    } else {
                        $isUnverifiedBySms = true;
                    }

                    $searchFields = ['isUnverifiedBySms'];
                    foreach ($searchFields as $field) {
                        $queryBuilder->addAnd($queryBuilder->expr()->field($field)->equals($isUnverifiedBySms));
                    }
                },
            'field_type' => 'choice',
            'field_options' => ['choices'=> ['true'=> "is verified by SMS", "false"=>"is not verified by SMS"]]
        ])