templates/blog/_testFilter.html.twig line 1

  1. <div class="test-filter-box">
  2.     <h3>Hledat v testech</h3>
  3.     
  4.     <form method="GET" action="#" class="test-filter-form" onsubmit="return submitFilter()">
  5.         <div class="filter-row">
  6.             <div class="filter-field">
  7.                 <label for="manufacturer">Značka</label>
  8.                 <select id="manufacturer" name="manufacturer" onchange="updateModels()">
  9.                     <option value="">Vyberte značku</option>
  10.                     {% for manufacturer in manufacturers %}
  11.                         <option value="{{ manufacturer.url }}" data-id="{{ manufacturer.id }}"
  12.                                 {% if activeManufacturer and activeManufacturer.url == manufacturer.url %}selected{% endif %}>
  13.                             {{ manufacturer.name }}
  14.                         </option>
  15.                     {% endfor %}
  16.                 </select>
  17.             </div>
  18.             
  19.             <div class="filter-field">
  20.                 <label for="model">Model</label>
  21.                 <select id="model" name="model">
  22.                     <option value="">Vyberte model</option>
  23.                     {% if models %}
  24.                         {% for model in models %}
  25.                             <option value="{{ model.url }}" 
  26.                                     {% if activeModel and activeModel.url == model.url %}selected{% endif %}>
  27.                                 {{ model.name }}
  28.                             </option>
  29.                         {% endfor %}
  30.                     {% endif %}
  31.                 </select>
  32.             </div>
  33.             
  34.             <div class="filter-field">
  35.                 <button type="submit" class="filter-button">Vyhledat</button>
  36.             </div>
  37.         </div>
  38.     </form>
  39. </div>
  40. <script>
  41. function updateModels() {
  42.     const manufacturerSelect = document.getElementById('manufacturer');
  43.     const modelSelect = document.getElementById('model');
  44.     const selectedOption = manufacturerSelect.options[manufacturerSelect.selectedIndex];
  45.     const manufacturerId = selectedOption ? selectedOption.getAttribute('data-id') : null;
  46.     
  47.     // Clear model options
  48.     modelSelect.innerHTML = '<option value="">Vyberte model</option>';
  49.     
  50.     if (!manufacturerId) {
  51.         return;
  52.     }
  53.     fetch('/models?idManufacturer=' + encodeURIComponent(manufacturerId))
  54.         .then(response => response.json())
  55.         .then(data => {
  56.             if (data.models) {
  57.                 data.models.forEach(model => {
  58.                     const option = document.createElement('option');
  59.                     option.value = model.url;
  60.                     option.textContent = model.name;
  61.                     modelSelect.appendChild(option);
  62.                 });
  63.             }
  64.         })
  65.         .catch(error => {
  66.             console.error('Error fetching models:', error);
  67.         });
  68. }
  69. function submitFilter() {
  70.     const manufacturerSelect = document.getElementById('manufacturer');
  71.     const modelSelect = document.getElementById('model');
  72.     const manufacturerUrl = manufacturerSelect.value;
  73.     const modelUrl = modelSelect.value;
  74.     hideErrorMessage();
  75.     
  76.     if (!manufacturerUrl) {
  77.         alert('Vyberte prosím značku');
  78.         return false;
  79.     }
  80.     
  81.     let url = '/clanky/testy-aut/';
  82.     
  83.     if (manufacturerUrl && modelUrl) {
  84.         url += manufacturerUrl + '-' + modelUrl;
  85.     } else if (manufacturerUrl) {
  86.         url += manufacturerUrl;
  87.     }
  88.     
  89.     window.location.href = url;
  90.     return false;
  91. }
  92. function hideErrorMessage() {
  93.     const errorMessage = document.getElementById('filter-error-message');
  94.     if (errorMessage) {
  95.         errorMessage.style.display = 'none';
  96.     }
  97. }
  98. </script>