Implement AND Condition in Odoo Search Bar with JS
Implementing an AND Condition in Odoo Search Bar extends a significant spring in search precision and efficiency. Instead of the broad results, users can combine specific criteria such as searching for products of a particular brand and with a specific model number, instantly narrowing down big datasets to the exact items they need.
This targeted filtering saves valuable time, reduces the frustration of filtering through irrelevant entries, and lets users to quickly access the information required for their tasks. Ultimately, this enhanced search functionality leads to improved productivity, better data management, and a more simplified user experience in the Odoo environment.
In this blog, we will discuss the code for implementing AND conditions in search bar. For most of business, having lots of products with same brand and to filter them according to their unique number or code, within each brand is necessary sometimes. For that requirement a back end coded search bar having the feature to search both brand or company and their specific code is made possible here.
search_bar.js
import {patch} from "@web/core/utils/patch";
import {SearchBar} from "@web/search/search_bar/search_bar";
patch(SearchBar.prototype, {
selectItem(item) {
const searchItem = this.getSearchItem(item.searchItemId);
if (
(searchItem.type === "field" && searchItem.fieldType === "properties") ||
(searchItem.type === "field_property" && item.unselectable)
) {
this.toggleItem(item, !item.isExpanded);
return;
}
if (!item.unselectable) {
const {searchItemId, label, operator, value} = item;
this.env.searchModel.addAutoCompletionValues(searchItemId, {
label,
operator,
value,
isShiftKey: this.isShiftKey,
});
}
this.resetState();
},
onSearchKeydown(ev) {
this.isShiftKey = ev.shiftKey || false;
super.onSearchKeydown(ev);
},
});
search_model.js
import {patch} from "@web/core/utils/patch";
import {rankInterval} from "@web/search/utils/dates";
import {SearchModel} from "@web/search/search_model";
patch(SearchModel.prototype, {
_getGroups() {
const preGroups = [];
for (const queryElem of this.query) {
const {searchItemId} = queryElem;
let {groupId} = this.searchItems[searchItemId];
if ("autocompleteValue" in queryElem) {
if (queryElem.autocompleteValue.isShiftKey) {
groupId = Math.random();
}
}
let preGroup = preGroups.find((group) => group.id === groupId);
if (!preGroup) {
preGroup = {id: groupId, queryElements: []};
preGroups.push(preGroup);
}
queryElem.groupId = groupId;
preGroup.queryElements.push(queryElem);
}
const groups = [];
for (const preGroup of preGroups) {
const {queryElements, id} = preGroup;
const activeItems = [];
for (const queryElem of queryElements) {
const {searchItemId} = queryElem;
let activeItem = activeItems.find(
({searchItemId: id}) => id === searchItemId
);
if ("generatorId" in queryElem) {
if (!activeItem) {
activeItem = {searchItemId, generatorIds: []};
activeItems.push(activeItem);
}
activeItem.generatorIds.push(queryElem.generatorId);
} else if ("intervalId" in queryElem) {
if (!activeItem) {
activeItem = {searchItemId, intervalIds: []};
activeItems.push(activeItem);
}
activeItem.intervalIds.push(queryElem.intervalId);
} else if ("autocompleteValue" in queryElem) {
if (!activeItem) {
activeItem = {searchItemId, autocompletValues: []};
activeItems.push(activeItem);
}
activeItem.autocompletValues.push(queryElem.autocompleteValue);
} else if (!activeItem) {
activeItem = {searchItemId};
activeItems.push(activeItem);
}
}
for (const activeItem of activeItems) {
if ("intervalIds" in activeItem) {
activeItem.intervalIds.sort(
(g1, g2) => rankInterval(g1) - rankInterval(g2)
);
}
}
groups.push({id, activeItems});
}
return groups;
},
deactivateGroup(groupId) {
this.query = this.query.filter((queryElem) => {
return queryElem.groupId !== groupId;
});
for (const partName in this.domainParts) {
const part = this.domainParts[partName];
if (part.groupId === groupId) {
this.setDomainParts({[partName]: null});
}
}
this._checkComparisonStatus();
this._notify();
},
});
First, add the first word, then type the second word click shift button and enter you will get the result.
Want to advance your Odoo search capabilities? We help you to implement this AND condition in search bar and other Odoo customization services to upgrade your operational efficiency, Partner with Transines Solutions now, we specialize in offering Customized Odoo ERP solutions to your unique business needs. Having doubts? Don’t worry our expert team is here to guide you, Contact us today for a FREE Odoo consultation now.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote