Skip to content

Commit

Permalink
fix select2 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Sep 9, 2014
1 parent fe3061a commit 68a836e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 83 deletions.
28 changes: 26 additions & 2 deletions Resources/public/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
});
Expand Down
166 changes: 86 additions & 80 deletions Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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') %}
Expand Down Expand Up @@ -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 %}

Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Pager/base_results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit 68a836e

Please sign in to comment.