Browse Source

fix select2 integration

Thomas Rabaix 11 years ago
parent
commit
68a836e5db

+ 26 - 2
Resources/public/Admin.js

@@ -70,8 +70,8 @@ var Admin = {
         if (window.SONATA_CONFIG && window.SONATA_CONFIG.USE_SELECT2 && window.Select2) {
 
             jQuery('select:not([data-sonata-select2="false"])', subject).each(function() {
-                var select = jQuery(this);
 
+                var select = jQuery(this);
                 var allowClearEnabled = false;
 
                 if (select.find('option[value=""]').length) {
@@ -84,8 +84,32 @@ var Admin = {
                     allowClearEnabled = false;
                 }
 
+                ereg = /width:(auto|(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc)))/i;
                 select.select2({
-                    width: 'resolve',
+                    width: function() {
+
+                    // this code is an adaptation of select2 code (initContainerWidth function)
+                    style = this.element.attr('style');
+                    //console.log("main style", style);
+                    if (style !== undefined) {
+                        attrs = style.split(';');
+                        for (i = 0, l = attrs.length; i < l; i = i + 1) {
+
+                            matches = attrs[i].replace(/\s/g, '').match(ereg);
+
+                            if (matches !== null && matches.length >= 1)
+                                return matches[1];
+                            }
+                        }
+
+                        style = this.element.css('width');
+                        if (style.indexOf("%") > 0) {
+                            return style;
+                        }
+
+                        return '100%';
+                    },
+                    dropdownAutoWidth: true,
                     minimumResultsForSearch: 10,
                     allowClear: allowClearEnabled
                 });

+ 86 - 80
Resources/views/CRUD/base_list.html.twig

@@ -84,92 +84,94 @@ file that was distributed with this source code.
                         {% endblock %}
 
                         {% block table_footer %}
-                            <tr>
-                                <th colspan="{{ admin.list.elements|length - (app.request.isXmlHttpRequest ? (admin.list.has('_action') + admin.list.has('batch')) : 0) }}">
-                                    <div class="form-inline">
-                                        {% if not app.request.isXmlHttpRequest %}
-                                            {% if admin.hasRoute('batch') and batchactions|length > 0  %}
-                                                {% block batch %}
-                                                    <script>
-                                                        {% block batch_javascript %}
-                                                            jQuery(document).ready(function ($) {
-                                                                $('#list_batch_checkbox').on('ifChanged', function () {
-                                                                    $(this)
-                                                                        .closest('table')
-                                                                        .find('td.sonata-ba-list-field-batch input[type="checkbox"]')
-                                                                        .iCheck($(this).is(':checked') ? 'check' : 'uncheck')
-                                                                    ;
-                                                                });
-
-                                                                $('td.sonata-ba-list-field-batch input[type="checkbox"]')
-                                                                    .on('ifChanged', function () {
+                            <tfoot>
+                                <tr>
+                                    <th colspan="{{ admin.list.elements|length - (app.request.isXmlHttpRequest ? (admin.list.has('_action') + admin.list.has('batch')) : 0) }}">
+                                        <div class="form-inline">
+                                            {% if not app.request.isXmlHttpRequest %}
+                                                {% if admin.hasRoute('batch') and batchactions|length > 0  %}
+                                                    {% block batch %}
+                                                        <script>
+                                                            {% block batch_javascript %}
+                                                                jQuery(document).ready(function ($) {
+                                                                    $('#list_batch_checkbox').on('ifChanged', function () {
                                                                         $(this)
-                                                                            .closest('tr')
-                                                                            .toggleClass('sonata-ba-list-row-selected', $(this).is(':checked'))
+                                                                            .closest('table')
+                                                                            .find('td.sonata-ba-list-field-batch input[type="checkbox"]')
+                                                                            .iCheck($(this).is(':checked') ? 'check' : 'uncheck')
                                                                         ;
-                                                                    })
-                                                                    .trigger('ifChanged')
-                                                                ;
-                                                            });
+                                                                    });
+
+                                                                    $('td.sonata-ba-list-field-batch input[type="checkbox"]')
+                                                                        .on('ifChanged', function () {
+                                                                            $(this)
+                                                                                .closest('tr')
+                                                                                .toggleClass('sonata-ba-list-row-selected', $(this).is(':checked'))
+                                                                            ;
+                                                                        })
+                                                                        .trigger('ifChanged')
+                                                                    ;
+                                                                });
+                                                            {% endblock %}
+                                                        </script>
+
+                                                        {% block batch_actions %}
+                                                            <label class="checkbox" for="{{ admin.uniqid }}_all_elements">
+                                                                <input type="checkbox" name="all_elements" id="{{ admin.uniqid }}_all_elements">
+                                                                {{ 'all_elements'|trans({}, 'SonataAdminBundle') }}
+                                                                 ({{ admin.datagrid.pager.nbresults }})
+                                                            </label>
+
+                                                            <select name="action" style="width: auto; height: auto" class="form-control">
+                                                                {% for action, options in batchactions %}
+                                                                    <option value="{{ action }}">{{ options.label }}</option>
+                                                                {% endfor %}
+                                                            </select>
                                                         {% endblock %}
-                                                    </script>
-
-                                                    {% block batch_actions %}
-                                                        <label class="checkbox" for="{{ admin.uniqid }}_all_elements">
-                                                            <input type="checkbox" name="all_elements" id="{{ admin.uniqid }}_all_elements">
-                                                            {{ 'all_elements'|trans({}, 'SonataAdminBundle') }}
-                                                             ({{ admin.datagrid.pager.nbresults }})
-                                                        </label>
-
-                                                        <select name="action" style="width: auto; height: auto" class="form-control">
-                                                            {% for action, options in batchactions %}
-                                                                <option value="{{ action }}">{{ options.label }}</option>
-                                                            {% endfor %}
-                                                        </select>
-                                                    {% endblock %}
-
-                                                    <input type="submit" class="btn btn-small btn-primary" value="{{ 'btn_batch'|trans({}, 'SonataAdminBundle') }}">
-                                                {% endblock %}
-                                            {% endif %}
 
-                                            <div class="pull-right">
-                                                {% if admin.hasRoute('export') and admin.isGranted("EXPORT") and admin.getExportFormats()|length %}
-                                                    <div class="btn-group">
-                                                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
-                                                            <i class="glyphicon glyphicon-export"></i>
-                                                            {{ "label_export_download"|trans({}, "SonataAdminBundle") }}
-                                                            <span class="caret"></span>
-                                                        </button>
-                                                        <ul class="dropdown-menu">
-                                                            {% for format in admin.getExportFormats() %}
-                                                                <li>
-                                                                    <a href="{{ admin.generateUrl('export', admin.modelmanager.paginationparameters(admin.datagrid, 0) + {'format' : format}) }}">
-                                                                        <i class="glyphicon glyphicon-download"></i>
-                                                                        {{ format|upper }}
-                                                                    </a>
-                                                                <li>
-                                                            {% endfor %}
-                                                        </ul>
-                                                    </div>
-
-                                                    &nbsp;-&nbsp;
+                                                        <input type="submit" class="btn btn-small btn-primary" value="{{ 'btn_batch'|trans({}, 'SonataAdminBundle') }}">
+                                                    {% endblock %}
                                                 {% endif %}
 
-                                                {% block pager_results %}
-                                                    {% include admin.getTemplate('pager_results') %}
-                                                {% endblock %}
-                                            </div>
-                                        {% endif %}
-                                    </div>
-                                </th>
-                            </tr>
+                                                <div class="pull-right">
+                                                    {% if admin.hasRoute('export') and admin.isGranted("EXPORT") and admin.getExportFormats()|length %}
+                                                        <div class="btn-group">
+                                                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
+                                                                <i class="glyphicon glyphicon-export"></i>
+                                                                {{ "label_export_download"|trans({}, "SonataAdminBundle") }}
+                                                                <span class="caret"></span>
+                                                            </button>
+                                                            <ul class="dropdown-menu">
+                                                                {% for format in admin.getExportFormats() %}
+                                                                    <li>
+                                                                        <a href="{{ admin.generateUrl('export', admin.modelmanager.paginationparameters(admin.datagrid, 0) + {'format' : format}) }}">
+                                                                            <i class="glyphicon glyphicon-download"></i>
+                                                                            {{ format|upper }}
+                                                                        </a>
+                                                                    <li>
+                                                                {% endfor %}
+                                                            </ul>
+                                                        </div>
+
+                                                        &nbsp;-&nbsp;
+                                                    {% endif %}
+
+                                                    {% block pager_results %}
+                                                        {% include admin.getTemplate('pager_results') %}
+                                                    {% endblock %}
+                                                </div>
+                                            {% endif %}
+                                        </div>
+                                    </th>
+                                </tr>
 
-                            {% block pager_links %}
-                                {% if admin.datagrid.pager.haveToPaginate() %}
-                                    {% include admin.getTemplate('pager_links') %}
-                                {% endif %}
-                            {% endblock %}
+                                {% block pager_links %}
+                                    {% if admin.datagrid.pager.haveToPaginate() %}
+                                        {% include admin.getTemplate('pager_links') %}
+                                    {% endif %}
+                                {% endblock %}
 
+                            </tfoot>
                         {% endblock %}
                     </table>
                 {% if admin.hasRoute('batch') %}
@@ -208,9 +210,13 @@ file that was distributed with this source code.
                                     {% set attr = form.children[filter.formName].children['type'].vars.attr|default({}) %}
                                     {% set attr = attr|merge({'class': (attr.class|default('') ~ ' sonata-filter-option')|trim}) %}
 
-                                    {{ form_widget(form.children[filter.formName].children['type'], {'attr':  attr}) }}
+                                    <div>
+                                        {{ form_widget(form.children[filter.formName].children['type'], {'attr':  attr}) }}
+                                    </div>
 
-                                    {{ form_widget(form.children[filter.formName].children['value']) }}
+                                    <div>
+                                        {{ form_widget(form.children[filter.formName].children['value']) }}
+                                    </div>
                                 </div>
                             {% endfor %}
 

+ 1 - 1
Resources/views/Pager/base_results.html.twig

@@ -21,7 +21,7 @@ file that was distributed with this source code.
 
 {% block max_per_page %}
     <label class="control-label" for="{{ admin.uniqid }}_per_page">{% trans from 'SonataAdminBundle' %}label_per_page{% endtrans %}</label>
-    <select class="form-control per-page small" id="{{ admin.uniqid }}_per_page">
+    <select class="form-control per-page small" id="{{ admin.uniqid }}_per_page" style="width: auto">
         {% for per_page in admin.getperpageoptions %}
             <option {% if per_page == admin.datagrid.pager.maxperpage %}selected="selected"{% endif %} value="{{ admin.generateUrl('list', {'filter': admin.datagrid.values | merge({'_page': 1, '_per_page': per_page})}) }}">
                 {{ per_page }}