Skip to content

Commit

Permalink
feat: adding custom-search prop
Browse files Browse the repository at this point in the history
  • Loading branch information
pohnean committed Sep 29, 2023
1 parent aa77f92 commit ffc112d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/components/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
v-else-if="slots['header']"
name="header"
v-bind="header"
/>
/>
<span
v-else
class="header-text"
Expand Down Expand Up @@ -145,7 +145,7 @@
// eslint-disable-next-line max-len
}, typeof bodyItemClassName === 'string' ? bodyItemClassName : bodyItemClassName(column, index + 1), `direction-${bodyTextDirection}`]"
@click="column === 'expand' ? updateExpandingItemIndexList(index + prevPageEndIndex, item, $event) : null"
>
>
<slot
v-if="slots[`item-${column}`]"
:name="`item-${column}`"
Expand Down Expand Up @@ -482,6 +482,7 @@ const {
itemsSelected,
searchField,
searchValue,
props.customSearch,
serverItemsLength,
multiSort,
emits,
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useTotalItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function useTotalItems(
itemsSelected: Ref<Item[]>,
searchField: Ref<string>,
searchValue: Ref<string>,
customSearch: (item: Item) => boolean | undefined,
serverItemsLength: Ref<number>,
multiSort: Ref<boolean>,
emits: (event: EmitsEventName, ...args: any[]) => void,
Expand All @@ -33,8 +34,8 @@ export default function useTotalItems(
const itemsSearching = computed((): Item[] => {
// searching feature is not available in server-side mode
if (!isServerSideMode.value && searchValue.value !== '') {
const regex = new RegExp(searchValue.value, 'i');
return items.value.filter((item) => regex.test(generateSearchingTarget(item)));
const searchFn = customSearch ?? ((item) => new RegExp(searchValue.value, 'i').test(generateSearchingTarget(item)));
return items.value.filter(searchFn);
}
return items.value;
});
Expand Down
16 changes: 16 additions & 0 deletions src/modes/Client.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
:header-item-class-name="headerItemClassNameFunction"
:body-item-class-name="bodyItemClassNameFunction"
:body-expand-row-class-name="bodyExpandRowClassNameFunction"
:custom-search="customSearchFunction"
@update-sort="updateSort"
@update-filter="updateFilter"
multi-sort
Expand Down Expand Up @@ -165,6 +166,21 @@ const updateTotalItems = (items: Item[]) => {
console.log(JSON.stringify(items));
};
// Example of Custom search by name and height
// e.g.: searchValue = 'h 6-11'
const customSearchFunction = (item: Item) => {
const keyword = searchValue.value.toLowerCase();
return keyword
.split(' ')
.every((word) => {
const nameMatched = item.name.toLowerCase().includes(word);
const heightMatched = item.indicator.height.toLowerCase().includes(word);
return nameMatched || heightMatched;
});
}
const items = ref<Item[]>([
{ name: "Stephen Curry", firstName: "GSW", number: 30, position: 'G', indicator: {"height": '6-2', "weight": 185}, lastAttended: "Davidson", country: "USA"},
{ name: "Kevin Durant", firstName: "BKN", number: 7, position: 'F', indicator: {"height": '6-10', "weight": 240}, lastAttended: "Texas-Austin", country: "USA"},
Expand Down
4 changes: 4 additions & 0 deletions src/propsWithDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export default {
type: String,
default: '',
},
customSearch: {
type: Object as PropType<(item: Item) => boolean> | null,
default: null,
},
serverOptions: {
type: Object as PropType<ServerOptions> | null,
default: null,
Expand Down

0 comments on commit ffc112d

Please sign in to comment.