{## # This file is part of the SgDatatablesBundle package. # # (c) stwe # # For the full copyright and license information, please view the LICENSE # file that was distributed with this source code. #} {% import _self as macros %} {% macro link_title(action) %} {% if action.label is same as(null) and action.icon is same as(null) %} {% if action.route is not same as(null) %} {{ action.route }} {% else %} null {% endif %} {% else %} {{ action.label }} {% endif %} {% endmacro %} {% macro attributes(action) %} {% for key, value in action.attributes %} {{ key }}="{{ value }}" {% endfor %} {% endmacro %} {% macro confirm_dialog(action) %} {% if action.confirm is same as(true) %} {% if action.confirmMessage is not same as(null) %} data-message="{{ action.confirmMessage }}" {% else %} data-message="{{ 'sg.datatables.confirmMessage'|trans({}, 'messages') }}" {% endif %} {% endif %} {% endmacro %} {% macro href(action, route_parameters) %} {% if action.routeParameters is not same as(null) %} {% if app.request.locale is defined and app.request.locale is not null %} {% if '_locale' in route_parameters|keys %} {% set route_parameters = route_parameters|merge({ '_locale': app.request.locale }) %} {% endif %} {% endif %} {% endif %} {% if action.route is not same as(null) %} href="{{ path(action.route, route_parameters) }}" {% else %} href="javascript:void(0);" {% endif %} {% endmacro %} {% macro value(value) %} {% if value is not null %} value="{{ value }}" {% endif %} {% endmacro %} {% set multiselect_actions %} {% for actionKey, action in actions %} {% if action.callRenderIfClosure is same as(true) %} {% if action.button is same as(false) %} {{ action.startHtml|raw }} {{ macros.link_title(action) }} {{ action.endHtml|raw }} {% else %} {{ action.startHtml|raw }} {{ action.endHtml|raw }} {% endif %} {% endif %} {% endfor %} {% endset %} {# add multiselect actions #} {% if dom_id is null %} $("#sg-datatables-{{ datatable_name }}-multiselect-actions").append("{{ multiselect_actions|e('js') }}"); {% else %} $("#{{ dom_id }}").append("{{ multiselect_actions|e('js') }}"); {% endif %} {# function to update the check-all-checkbox #} function updateCheckAll() { var cbox_all = $("#sg-datatables-{{ datatable_name }} tbody input.sg-datatables-{{ datatable_name }}-multiselect-checkbox:checkbox"); var cbox_checked = $("#sg-datatables-{{ datatable_name }} tbody input.sg-datatables-{{ datatable_name }}-multiselect-checkbox:checkbox:checked"); var cbox_checkall = $("#sg-datatables-{{ datatable_name }} input.sg-datatables-{{ datatable_name }}-multiselect-checkall:checkbox"); var action_links = $(".sg-datatables-{{ datatable_name }}-multiselect-action"); if(cbox_checked.length === 0){ cbox_checkall.prop('checked', false); cbox_checkall.prop('indeterminate', false); action_links.prop('disabled', true); action_links.addClass('disabled'); } else if (cbox_checked.length === cbox_all.length){ cbox_checkall.prop('checked', true); cbox_checkall.prop('indeterminate', false); action_links.prop('disabled', false); action_links.removeClass('disabled'); } else { cbox_checkall.prop('checked', false); cbox_checkall.prop('indeterminate', true); action_links.prop('disabled', false); action_links.removeClass('disabled'); } } {# handle table redraw #} $("#sg-datatables-{{ datatable_name }}").on("draw.dt", function () { updateCheckAll(); }); {# handle row click #} $("#sg-datatables-{{ datatable_name }} tbody").on("click", "tr", function () { {# add 'selected' class #} if ($(this).find("input").length) { $(this).toggleClass("selected"); } {# set !checked for the row checkbox #} $(this).find("input.sg-datatables-{{ datatable_name }}-multiselect-checkbox:checkbox").each(function() { this.checked = !this.checked; }); updateCheckAll(); }); {# handle checkbox click #} $("#sg-datatables-{{ datatable_name }} tbody").on("click", "input.sg-datatables-{{ datatable_name }}-multiselect-checkbox:checkbox", function () { this.checked = !this.checked; updateCheckAll(); }); {# select/unselect all checkboxes #} $("#sg-datatables-{{ datatable_name }}").on("click", "input.sg-datatables-{{ datatable_name }}-multiselect-checkall:checkbox", function () { $("input.sg-datatables-{{ datatable_name }}-multiselect-checkbox").prop('checked', $(this).prop("checked")); var propCheck = $(this).prop("checked"); $("#sg-datatables-{{ datatable_name }} tbody tr").each(function(){ if ($(this).find("input").length) { if (true == propCheck) { $(this).addClass("selected"); } else { $(this).removeClass("selected"); } } }); updateCheckAll(); }); {# handle multiselect action click #} $(".sg-datatables-{{ datatable_name }}-multiselect-action").on("click", function(event) { event.preventDefault(); if (oTable.rows(".selected").data().length > 0) { if ($(this).data("message")) { if (!confirm($(this).data("message"))) { return; } } var items = $.map(oTable.rows(".selected").data(), function (i) { return i }); var pipeline = {{ pipeline }}; {% set token = csrf_token('multiselect') %} var url = $(this).attr("href"); if (url != null) { $.ajax({ url: url, method: "POST", cache: false, data: { 'data': items, 'token': "{{ token }}" }, success: function(msg) { if (pipeline > 0) { oTable.clearPipeline().draw(); } else { oTable.draw(); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(XMLHttpRequest + ' ' + textStatus + ' ' + errorThrown); } }) } } else { alert("{{ 'sg.datatables.selectError'|trans({}, 'messages') }}"); } });