Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 14 additions & 3 deletions src/hooks/useTotalItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ 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,
) {
const defaultSearchFunction = (item: Item) => {
if (searchValue.value === '') {
return true;
}

const regExp = new RegExp(searchValue.value, 'i');
return regExp.test(generateSearchingTarget(item));
};


const generateSearchingTarget = (item: Item): string => {
if (typeof searchField.value === 'string' && searchField.value !== '') return getItemValue(searchField.value, item);
if (Array.isArray(searchField.value)) {
Expand All @@ -32,9 +43,9 @@ export default function useTotalItems(
// items searching
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)));
if (!isServerSideMode.value) {
const searchFn = customSearch ?? defaultSearchFunction;
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