Reputation: 129
My table looks like this:
I have a templated 'product name' wherein code goes like this:
<template v-slot:item.name="{ item }">
<v-list-item two-line>
<v-list-item-avatar style="width: 70px; height: 70px" tile>
<v-img :src="item.img" alt="John" />
</v-list-item-avatar>
<v-list-item-content>
<v-list-item-title style="">
<a
style="
text-decoration: none;
color: #0b0b0b;
font-size: 0.9em;
"
>{{ item.name }} {{ item.variation }}</a
>
</v-list-item-title>
<v-list-item-subtitle class="mt-1">
<small class="mr-5">
<v-icon small>mdi-package-variant</v-icon>
{{ item.sku }}
</small>
<small>
{{ item.shop_name }}
</small>
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</template>
as I understand, the search ONLY search for the value from the headers we indicated my headers btw:
headers: [
{ text: "Product Name", value: "name" },
{ text: "Quantity", value: "quantity" },
{ text: "Price", value: "price" },
{ text: "Orders", value: "itemsSold" },
{ text: "Revenue", value: "revenue" },
{ text: "Status", value: "active" },
],
How do I search un-declared value from headers? such as MEDIHEAL-MASK-PLACENTAREVITAL-EX (the subtitle for the product name of the first item in the table)
I was thinking of putting another value in the headers without the "text" element and templating the value to display nothing to the table. Any cleaner way of doing this?
Upvotes: 0
Views: 3534
Reputation: 1
You can try a simple solution:
First modify the opening v-data-table tag:
<v-data-table
:headers="headers"
:items="products"
:search="search"
:custom-filter="filter"
>
and then in the <script setup>
add:
<script setup>
function filter(value, search, item) {
return String(item.name).toLowerCase().includes(search);
}
</script>
where item.name will point to column desired to be filtered.
Upvotes: 0
Reputation: 553
I came up with this:
items: [
// ...
{ name: "MEDIHEAL Beauty Mask", quantity: 483, price: 0, somePropThatsNotVisible: "String it could be found by" }
// ...
]
<v-data-table :headers="headers" :items="items" :search="search" :custom-filter="filter">
methods: {
filter (value, search, item) {
return Object.values(item).some(prop => {
return String(prop).includes(search)
})
}
},
That's it! now all props are searchable but not shown if not included in the headers.
Upvotes: 2
Reputation: 50787
You can override the filter property on the header definition for that column.
headers: [
{
text: "Product Name", value: "name", filter: product => {
// compare product.property against this.search and return true/false
}
},
// ... your other col definitions
]
Upvotes: 1