edit.html.twig 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. {% extends "@SonataAdmin/CRUD/edit.html.twig" %}
  2. {% block javascripts %}
  3. {{ parent() }}
  4. <script type="text/javascript">
  5. var $fixedIP = false;
  6. var $initHostTypeId = false;
  7. $(document).ready(function() {
  8. {% if object.getFixedAddress() is not null or object.getFixedAddress() != "" %}
  9. $fixedIP = "{{object.getFixedAddress()}}";
  10. {% endif %}
  11. {% if object.getHostType() is not null %}
  12. $initHostTypeId = {{object.getHostType().getId()}};
  13. {% endif %}
  14. showHostField();
  15. $("select[id$='hostType']").on('change', showHostField);
  16. });
  17. // Al seleccionar tipo Cablemodem oculto el campo Host
  18. // de lo contrario lo muestro y selecciono la primer option
  19. // luego actualizo las fixed IP según el tipo de host
  20. function showHostField()
  21. {
  22. var $hostTypeField = $("select[id$='hostType'] :selected");
  23. var $hostField = $("div.form-group[id$='host']");
  24. var $hostSelect = $("select[id$='host']");
  25. if ($hostTypeField.html() === 'Cablemodem') {
  26. if ($hostSelect.find("option[value='']").length == 0) {
  27. $hostSelect.prepend('<option value=""></option>');
  28. }
  29. $hostSelect.val(null).trigger('change');
  30. $hostField.hide();
  31. } else {
  32. if ($hostSelect.find("option[value='']").length) {
  33. $hostSelect.find("option[value='']").remove();
  34. }
  35. var val = $hostSelect.find("option").first().attr('value');
  36. $hostSelect.val(val).trigger('change');
  37. $hostField.show();
  38. }
  39. updateFixedIPs();
  40. return false;
  41. }
  42. function updateFixedIPs()
  43. {
  44. var $hostTypeField = $("select[id$='hostType'] :selected");
  45. var $hostTypeId = $hostTypeField.val();
  46. var option = '<option value=""></option>';
  47. if (($hostTypeId == $initHostTypeId) && $fixedIP != false) {
  48. option = `<option value="${$fixedIP}" selected>${$fixedIP}</option>`;
  49. $("div[id$='_fixed_address'] a span.select2-chosen").html($fixedIP);
  50. } else {
  51. $("div[id$='_fixed_address'] a span.select2-chosen").html('');
  52. }
  53. $.ajax({
  54. url: '{{ path ('post_host_ipv4_range') }}',
  55. type: 'POST',
  56. data: {id: $hostTypeId},
  57. success: function(data) {
  58. $('select[id$="_fixed_address"]').html(option);
  59. if (data.ips) {
  60. $.each(data.ips, function (index, value) {
  61. $('select[id$="_fixed_address"]').append('<option value="' + value + '">' + value + '</option>');
  62. });
  63. }
  64. return false;
  65. }
  66. });
  67. }
  68. </script>
  69. {% endblock javascripts %}