aggio search

This commit is contained in:
Surya Paolo
2023-12-31 15:26:57 +01:00
parent 29e18ca293
commit 0cece2b1fc
2 changed files with 18 additions and 9 deletions

View File

@@ -39,16 +39,24 @@ export default defineComponent({
const getArrProducts = computed(() => {
let arrprod = productStore.getProducts(cosa.value)
let catstr = cat.value;
if (!search.value && !catstr) {
if ((!search.value || (search.value && search.value.length < 2)) && !catstr) {
return arrprod
}
let lowerSearchText = search.value.toLowerCase();
return arrprod.filter((product: IProduct) => {
let lowerName = product.productInfo.name!.toLowerCase()
let hasCategoria = !catstr || (catstr && product.productInfo.idCatProds?.includes(catstr))
return (product.productInfo.code!.includes(search.value) || lowerName.includes(lowerSearchText)) && hasCategoria
let lowerName = product.productInfo.name!.toLowerCase();
let hasCategoria = !catstr || (catstr && product.productInfo.idCatProds?.includes(catstr));
// Use a regular expression to match whole words
let codeMatch = new RegExp(`\\b${search.value}\\b`, 'i');
let nameMatch = new RegExp(`\\b${lowerSearchText}`, 'i');
// Check if any word in lowerName starts with lowerSearchText
let anyWordStartsWithSearch = lowerName.split(/\s+/).some(word => nameMatch.test(word));
return (codeMatch.test(product.productInfo.code!) || anyWordStartsWithSearch) && hasCategoria;
});
})
@@ -57,10 +65,10 @@ export default defineComponent({
if (!search.value) {
return arrprod
}
let lowerSearchText = search.value.toLowerCase();
let catstr = cat.value;
return arrprod.filter((product: IProduct) => {
let lowerName = product.productInfo.name!.toLowerCase();
const hasCategoria = !catstr || (catstr && product.productInfo.idCatProds?.includes(catstr));

View File

@@ -62,6 +62,7 @@
:dense="tools.isMobile() ? true : false"
:label="t('ecomm.code_o_text_search')"
v-model="search"
debounce="500"
class="q-ml-md"
>
<template v-slot:append>
@@ -75,9 +76,9 @@
:push="cat === reccat.value"
dense
:size="tools.isMobile() ? '0.70rem' : '1rem'"
:icon="reccat.icon"
:color="reccat.color ? reccat.color : undefined"
:text-color="cat === reccat.value ? 'blue' : 'black'"
:icon="reccat.icon ? reccat.icon : undefined"
:color="cat === reccat.value ? 'primary' : undefined"
:text-color="cat === reccat.value ? 'white' : 'black'"
rounded
:label="reccat.label"
@click="cat = reccat.value"