mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 08:51:09 -05:00
Refactor ride model search results template to utilize Alpine.js for state management. Enhanced selection handling and improved event dispatching for better user experience.
This commit is contained in:
@@ -1,4 +1,27 @@
|
|||||||
<div class="absolute z-50 w-full mt-1 bg-white border border-gray-300 rounded-lg shadow-lg dark:bg-gray-700 dark:border-gray-600" style="max-height: 240px; overflow-y: auto;">
|
<script>
|
||||||
|
document.addEventListener('alpine:init', () => {
|
||||||
|
Alpine.data('rideModelSearchResults', () => ({
|
||||||
|
selectRideModel(id, name) {
|
||||||
|
// Update ride model fields using AlpineJS reactive approach
|
||||||
|
const rideModelInput = this.$el.closest('form').querySelector('#id_ride_model');
|
||||||
|
const searchInput = this.$el.closest('form').querySelector('#id_ride_model_search');
|
||||||
|
const resultsDiv = this.$el.closest('form').querySelector('#ride-model-search-results');
|
||||||
|
|
||||||
|
if (rideModelInput) rideModelInput.value = id;
|
||||||
|
if (searchInput) searchInput.value = name;
|
||||||
|
if (resultsDiv) resultsDiv.innerHTML = '';
|
||||||
|
|
||||||
|
// Dispatch custom event for parent component
|
||||||
|
this.$dispatch('ride-model-selected', { id, name });
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div x-data="rideModelSearchResults()"
|
||||||
|
@click.outside="$el.innerHTML = ''"
|
||||||
|
class="absolute z-50 w-full mt-1 bg-white border border-gray-300 rounded-lg shadow-lg dark:bg-gray-700 dark:border-gray-600"
|
||||||
|
style="max-height: 240px; overflow-y: auto;">
|
||||||
{% if not manufacturer_id %}
|
{% if not manufacturer_id %}
|
||||||
<div class="px-4 py-2 text-gray-700 dark:text-gray-300">
|
<div class="px-4 py-2 text-gray-700 dark:text-gray-300">
|
||||||
Please select a manufacturer first
|
Please select a manufacturer first
|
||||||
@@ -8,7 +31,7 @@
|
|||||||
{% for ride_model in ride_models %}
|
{% for ride_model in ride_models %}
|
||||||
<button type="button"
|
<button type="button"
|
||||||
class="w-full px-4 py-2 text-left text-gray-900 hover:bg-gray-100 dark:text-white dark:hover:bg-gray-600"
|
class="w-full px-4 py-2 text-left text-gray-900 hover:bg-gray-100 dark:text-white dark:hover:bg-gray-600"
|
||||||
onclick="selectRideModel('{{ ride_model.id }}', '{{ ride_model.name|escapejs }}')">
|
@click="selectRideModel('{{ ride_model.id }}', '{{ ride_model.name|escapejs }}')">
|
||||||
{{ ride_model.name }}
|
{{ ride_model.name }}
|
||||||
{% if ride_model.manufacturer %}
|
{% if ride_model.manufacturer %}
|
||||||
<div class="text-sm text-gray-700 dark:text-gray-300">
|
<div class="text-sm text-gray-700 dark:text-gray-300">
|
||||||
@@ -28,11 +51,3 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
function selectRideModel(id, name) {
|
|
||||||
document.getElementById('id_ride_model').value = id;
|
|
||||||
document.getElementById('id_ride_model_search').value = name;
|
|
||||||
document.getElementById('ride-model-search-results').innerHTML = '';
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user