[UPDT] EMPLOYEE: Employee search method by adding search element by option

This commit is contained in:
Horilla
2024-01-20 12:54:05 +05:30
parent 79a2e53eb7
commit 1211556932
2 changed files with 100 additions and 29 deletions

View File

@@ -27,7 +27,24 @@ class EmployeeFilter(FilterSet):
"""
search = django_filters.CharFilter(method="filter_by_name")
selected_search_field = django_filters.ChoiceFilter(
label="Search Field",
choices=[
("employee", _("Search in : Employee")),
("reporting_manager", _("Search in : Reporting manager")),
("department", _("Search in : Department")),
("job_position", _("Search in : Job Position")),
],
method="filter_by_name_and_field",
widget=forms.Select(
attrs={
"size": 4,
"class": "oh-input__icon",
"style": "border: none; overflow: hidden; display: flex; position: absolute; z-index: 999; margin-left:8%;",
"onclick": "$('.filterButton')[0].click();",
}
),
)
employee_first_name = django_filters.CharFilter(lookup_expr="icontains")
employee_last_name = django_filters.CharFilter(lookup_expr="icontains")
country = django_filters.CharFilter(lookup_expr="icontains")
@@ -133,20 +150,52 @@ class EmployeeFilter(FilterSet):
"""
Filter queryset by first name or last name.
"""
filter_method = {
"department": "employee_work_info__department_id__department__icontains",
"job_position": "employee_work_info__job_position_id__job_position__icontains",
"job_role": "employee_work_info__job_role_id__job_role__icontains",
"shift": "employee_work_info__shift_id__employee_shift__icontains",
"work_type": "employee_work_info__work_type_id__work_type__icontains",
"company": "employee_work_info__company_id__company__icontains",
}
search_field = self.data.get("search_field")
# Split the search value into first name and last name
parts = value.split()
first_name = parts[0]
last_name = " ".join(parts[1:]) if len(parts) > 1 else ""
# Filter the queryset by first name and last name
if first_name and last_name:
queryset = queryset.filter(
employee_first_name__icontains=first_name,
employee_last_name__icontains=last_name,
)
elif first_name:
queryset = queryset.filter(employee_first_name__icontains=first_name)
elif last_name:
queryset = queryset.filter(employee_last_name__icontains=last_name)
if not search_field:
parts = value.split()
first_name = parts[0]
last_name = " ".join(parts[1:]) if len(parts) > 1 else ""
# Filter the queryset by first name and last name
if first_name and last_name:
queryset = queryset.filter(
employee_first_name__icontains=first_name,
employee_last_name__icontains=last_name,
)
elif first_name:
queryset = queryset.filter(employee_first_name__icontains=first_name)
elif last_name:
queryset = queryset.filter(employee_last_name__icontains=last_name)
else:
if search_field == "reporting_manager":
parts = value.split()
first_name = parts[0]
last_name = " ".join(parts[1:]) if len(parts) > 1 else ""
if first_name and last_name:
queryset = queryset.filter(
employee_work_info__reporting_manager_id__employee_first_name__icontains=first_name,
employee_work_info__reporting_manager_id__employee_last_name__icontains=last_name,
)
elif first_name:
queryset = queryset.filter(
employee_work_info__reporting_manager_id__employee_first_name__icontains=first_name
)
elif last_name:
queryset = queryset.filter(
employee_work_info__reporting_manager_id__employee_last_name__icontains=last_name
)
else:
filter = filter_method.get(search_field)
queryset = queryset.filter(**{filter: value})
return queryset
def __init__(self, data=None, queryset=None, *, request=None, prefix=None):

View File

@@ -196,12 +196,12 @@
id="filterForm"
hx-target="#view-container"
class="d-flex"
>
>
{% if emp %}
<div
class="oh-input-group oh-input__search-group"
:class="searchShow ? 'oh-input__search-group--show' : ''"
>
>
<ion-icon
name="search-outline"
class="oh-input-group__icon oh-input-group__icon--left"
@@ -215,12 +215,23 @@
class="oh-input oh-input__icon"
aria-label="Search Input"
/>
<div id="searchFieldDiv" style="display:none;">
<select name="search_field" class='oh-input__icon' style="border: none; display: flex; position: absolute; z-index: 999; margin-left:8%;" size="3" onclick="$('.filterButton')[0].click();">
<option style="margin-left: -10px;" value="reporting_manager">{% trans "Search in : Reporting Manager" %}</option>
<option style="margin-left: -10px;" value="department">{% trans "Search in : Department" %}</option>
<option style="margin-left: -10px;" value="job_position">{% trans "Search in : Job Position" %}</option>
<option style="margin-left: -10px;" value="job_role">{% trans "Search in : Job Role" %}</option>
<option style="margin-left: -10px;" value="shift">{% trans "Search in : Shift" %}</option>
<option style="margin-left: -10px;" value="work_type">{% trans "Search in : Work Type" %}</option>
<option style="margin-left: -10px;" value="company">{% trans "Search in : Company" %}</option>
</select>
</div>
</div>
{% endif %}
<div class="oh-main__titlebar-button-container">
{% if emp %}
<input type="hidden" name="view" value="{{request.GET.view}}" id="employeeViewType">
<input type="hidden" name="view" value="{{request.GET.view}}" id="employeeViewType">
<ul class="oh-view-types ml-2" style="margin-bottom: 0">
<li class="oh-view-type employee-view-type" data-view="list">
<a
@@ -251,7 +262,7 @@
class="oh-btn ml-2"
@click="open = !open"
onclick="event.preventDefault()"
>
>
<ion-icon name="filter" class="mr-1"></ion-icon>{% trans "Filter" %}
<div id="filterCount"></div>
</button>
@@ -260,14 +271,14 @@
x-show="open"
@click.outside="open = false"
style="display: none"
>
>
{% include 'employee_filters.html' %}
<div class="oh-dropdown__filter-footer">
<button
class="oh-btn oh-btn--secondary oh-btn--small w-100 filterButton"
id="#employeeFilter"
onclick="employeeFilter(this)"
>
>
{% trans "Filter" %}
</button>
</div>
@@ -278,9 +289,9 @@
class="oh-btn ml-2"
@click="open = !open"
onclick="event.preventDefault()"
>
>
<ion-icon name="library-outline" class="mr-1"></ion-icon>
{% trans "Group By" %}
{% trans "Group By" %}
<div id="filterCount"></div>
</button>
<div
@@ -288,7 +299,7 @@
x-show="open"
@click.outside="open = false"
style="display: none"
>
>
<div class="oh-accordion">
<label>{% trans "Group By" %}</label>
<div class="row">
@@ -304,7 +315,7 @@
id="id_field"
name="field"
class="select2-selection select2-selection--single"
>
>
{% for field in gp_fields %}
<option value="{{ field.0 }}">{% trans field.1 %}</option>
{% endfor %}
@@ -322,14 +333,14 @@
class="oh-btn oh-btn--dropdown"
@click="open = !open"
@click.outside="open = false"
>
>
{% trans "Actions" %}
</button>
<div
class="oh-dropdown__menu oh-dropdown__menu--right"
x-show="open"
style="display: none"
>
>
<ul class="oh-dropdown__items">
<li class="oh-dropdown__item">
<a
@@ -338,7 +349,7 @@
id="work-info-import"
data-toggle="oh-modal-toggle"
data-target="#workInfoImport"
>
>
{% trans "Import" %}
</a>
</li>
@@ -350,7 +361,7 @@
id="employee-info-export"
data-toggle="oh-modal-toggle"
data-target="#employeeExport"
>
>
{% trans "Export" %}
</a>
</li>
@@ -371,7 +382,7 @@
id="employeeBulkUpdateId"
data-toggle="oh-modal-toggle"
data-target="#bulkUpdateModal"
>
>
{% trans "Bulk Update" %}
</a>
</li>
@@ -452,6 +463,17 @@
return value;
}
$(document).ready(function () {
$('#employee-search').on('keyup', function () {
var searchFieldDiv = $('#searchFieldDiv');
var selectedField = searchFieldDiv.find(':selected');
if ($(this).val().trim() !== '') {
searchFieldDiv.show();
} else {
searchFieldDiv.hide();
selectedField.prop('selected', false);
}
$('.filterButton').eq(0).click();
});
$("#id_field").on("change", function () {
$(".filterButton")[0].click();
});