Split column_searchable_list into differente fields on frontend. #457
Replies: 2 comments 5 replies
-
|
Hey, can you explain what you mean by splitting the searchable columns in the frontend? |
Beta Was this translation helpful? Give feedback.
-
|
Hey there! So, I finally had some time to invest on this. I made a really simple solution, but it can be implemented through some kind of flag to switch between the search modes. The first change is in the def search_query(self, stmt: Select, term: List[str]) -> Select:
fields = self.column_searchable_list
values = term
expressions = [
getattr(self.model, fields[v_i]).like(text(f'"%{v}%"')) for v_i, v in enumerate(values) if v
]
params = {}
for f_i, f in enumerate(fields):
if values[f_i]:
params[f] = values[f_i]
ret = stmt.filter(and_(*expressions)).params(params)
return retSo, instead of receiving a Then, in order to receive a @login_required
async def list(self, request: Request) -> Response:
"""List route to display paginated Model instances."""
await self._list(request)
model_view = self._find_model_view(request.path_params["identity"])
page = int(request.query_params.get("page", 1))
page_size = int(request.query_params.get("pageSize", 0))
sort_by = request.query_params.get("sortBy", None)
sort = request.query_params.get("sort", "asc")
search_list = [request.query_params.get(
column, None) for column in model_view.column_searchable_list if column in model_view.model.__dict__]
searching = False
for s in search_list:
if s:
searching = True
break
pagination = await model_view.list(page, page_size, search_list, sort_by, sort)
pagination.add_pagination_urls(request.url)
context = {
"request": request,
"model_view": model_view,
"pagination": pagination,
"searching": searching
}
return self.templates.TemplateResponse(model_view.list_template, context)Basically, for each Then, in the {% if model_view.column_searchable_list %}
<form action="" method="GET" class="col row justify-content-end">
{% for column in model_view.column_searchable_list %}
<div class="col-md-3 text-muted">
<div class="input-group">
<input type="text" class="form-control" name="{{column}}" placeholder="{{ column | title }}" value="{{ request.query_params.get(column, '') }}">
</div>
</div>
{% endfor %}
<div class="btn-group col">
<button id="search-button" class="btn " type="submit">Buscar</button>
{% if searching %}
<a href="?" id="search-button" class="btn" ><i class="fa-solid fa-times"></i></a>
{% endif %}
</div>
</form>
{% endif %}There's a lot to improve on the solution and pretend to keep updating this thread until I get to the final solution. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there, is there any out of the box way of spliting the different searchable columns into different fields in the frontend?
If not, what class should I be extending to be able to change the way the search query is interpreted?
Beta Was this translation helpful? Give feedback.
All reactions