Skip to content

Commit

Permalink
Init toggles in embeddedList at load
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Feb 23, 2018
1 parent 34107db commit 521c86e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
56 changes: 56 additions & 0 deletions src/Resources/public/js/easyadmin-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function reloadEmbeddedList(identifier, toggleBaseUrl)
{
var containerPrefix = '.embedded-list[for="'+identifier+'"]';

$(containerPrefix).find('table .toggle input[type="checkbox"]').each(function (idx, el) {
$(this).bootstrapToggle();
})

// Reload sorted/paginated list in the embedded-list container
$(containerPrefix)
.on('click', 'th[data-property-name] a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', '.list-pagination a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
;

$(containerPrefix).find('table .toggle input[type="checkbox"]').change(function() {
var toggle = $(this);
var newValue = toggle.prop('checked');
var oldValue = !newValue;

var columnIndex = $(this).closest('td').index() + 1;
var propertyName = $(containerPrefix + ' table th.toggle:nth-child(' + columnIndex + ')').data('property-name');

var toggleUrl = toggleBaseUrl
+ "&id=" + $(this).closest('tr').data('id')
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();

var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });

toggleRequest.done(function(result) {});

toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.bootstrapToggle(oldValue == true ? 'on' : 'off');
toggle.bootstrapToggle('disable');
});
});
}
56 changes: 4 additions & 52 deletions src/Resources/views/default/embedded_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -116,58 +116,10 @@
{% block paginator %}{{ include(_entity_config.templates.paginator) }}{% endblock paginator %}

<script type="text/javascript">
$(document).ready(function () {
var containerPrefix = '.embedded-list[for="{{ widget_identifier|e('js') }}"]';
// Reload sorted/paginated list in the embedded-list container
$(containerPrefix)
.on('click', 'th[data-property-name] a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', '.list-pagination a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
;
$(containerPrefix).find('table .toggle input[type="checkbox"]').change(function() {
var toggle = $(this);
var newValue = toggle.prop('checked');
var oldValue = !newValue;
var columnIndex = $(this).closest('td').index() + 1;
var propertyName = $(containerPrefix + ' table th.toggle:nth-child(' + columnIndex + ')').data('property-name');
var toggleUrl = "{{ path('easyadmin', { action: 'edit', entity: _entity_config.name, view: 'list' })|raw }}"
+ "&id=" + $(this).closest('tr').data('id')
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();
var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function(result) {});
toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.bootstrapToggle(oldValue == true ? 'on' : 'off');
toggle.bootstrapToggle('disable');
});
});
});
reloadEmbeddedList(
'{{ widget_identifier|e('js') }}',
'{{ path('easyadmin', { action: 'edit', entity: _entity_config.name, view: 'list' })|raw }}'
);
</script>
</div>
{% endblock main %}
6 changes: 6 additions & 0 deletions src/Resources/views/default/layout.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends '@BaseEasyAdmin/default/layout.html.twig' %}

{% block head_javascript %}
{{ parent() }}
<script src="{{ asset('bundles/easyadminextension/js/easyadmin-extension.js') }}"></script>
{% endblock %}

0 comments on commit 521c86e

Please sign in to comment.