import { ref, watch } from "vue";
import BaseInput from "@/components/common/BaseInput.vue";
import DateInput from "@/components/common/DateInput.vue";
import { useRoute, useRouter } from "vue-router";
let route = useRoute()
let router = useRouter()
let commonSearchInput = ref(route.query.common_search_query)
let advancedSearchOn = ref(false)
let dateRangeFrom = ref(null)
let dateRangeTo = ref(null)
// Окна расширенного поиска
const typesOfSearch = [
{common: "Общий поиск"},
{image: "Поиск фотодокументов"},
{audio: "Поиск фонодокументов"},
{video: "Поиск киновидеодокументов"}
]
let currentSearchWindow = ref("common") // по умолчанию общий поиск
watch(advancedSearchOn, (newVal) => {
if (newVal) {
scrollToOneThirdHeight()
}
})
from rest_framework.metadata import SimpleMetadata class MaterialGetRequestMetadata(SimpleMetadata): def determine_actions(self, request, view): actions = {} action = view.action_map["get"] default_serializer_class = view.get_serializer_class() serializer_class = view.serializer_classes.get(action, default_serializer_class) serializer = serializer_class() actions[action] = self.get_serializer_info(serializer) return actions why common advanced from search is empty fix it. it should look exactly like in static variants but with dynamic adding from metadata like in my other materials. WRITE THE WHOLE MODIFIED VUE COMPONENT WITH DYNAMIC ADDING
IT SHOULDN'T USE STATICALLY WROTEN IN VUE COMPONENT FIELD NAMES
Assistant
To address the issue where the advanced search fields are not dynamically populated based on metadata, we'll create a Vue component that dynamically fetches and renders the advanced search fields. This approach ensures that the component remains flexible and adapts to any changes in the metadata without requiring manual updates to the field names.
Here's the complete modified Vue component with dynamic advanced search field rendering:
Consolidated all the logic into a single <script setup> block to maintain clarity and avoid conflicts.
Imported necessary components and composables (ref, watch, onMounted, etc.).
Dynamic Metadata Loading:
The loadMetadata function asynchronously fetches metadata for each material type (image, audio, video).
Metadata is stored in the metadata reactive variable, structured to map each material type to its respective fields.
The formatFields and formatCommonFields functions process the received metadata to a format suitable for rendering.
Dynamic Field Rendering in Template:
The advanced search section (v-window-item) now dynamically iterates over the metadata for the selected currentSearchWindow.
For each field, it checks the type to determine whether to render a BaseInput or DateInput.
This eliminates the need for hard-coded field names, allowing the form to adapt based on the fetched metadata.
Handling Date Fields:
The min-date attribute is dynamically set for the "Дата до" field to ensure that the end date is not earlier than the start date.
The stringToDate function parses date strings and formats them appropriately for the backend.
Form Submission with Dynamic Query Parameters:
Both handleCommonSearch and handleAdvancedSearch functions dynamically include form data in the query parameters based on the fields present in formData.
This ensures that only relevant and filled fields are sent with the search request.
Enhanced Styling and Responsiveness:
Adjusted styles to ensure that the component is responsive and maintains a clean layout across different screen sizes.
Added margin and padding adjustments for better spacing between elements.
Error Handling:
Backend Considerations
Ensure that your DRF backend correctly provides the required metadata for each material type. The getOptions API should return a JSON structure that includes the necessary fields and their attributes, such as label and type. Here's an example of how the response might look:
Ensure that each field includes necessary attributes that the frontend expects, such as label and type.
Final Notes
Extensibility: This setup allows you to easily add or modify fields in the backend without changing the frontend code.
Validation: Consider adding form validation to ensure that users input data in the correct format before submission.
User Feedback: Implement loading indicators or error messages to enhance user experience during metadata fetching or search submissions.
Optimization: If the metadata doesn't change frequently, consider caching it to reduce the number of API calls.
With these modifications, your advanced search should dynamically render fields based on the metadata provided by your backend, ensuring consistency and flexibility across different material types.