Skip to content

Commit

Permalink
fix: added row data to fieldFilter function
Browse files Browse the repository at this point in the history
  • Loading branch information
kouts committed Aug 31, 2023
1 parent 6776a08 commit ab9671d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
15 changes: 9 additions & 6 deletions docs/components/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Dataset takes the original data object as a prop and also some useful props as o
<dataset
v-slot="{ ds }"
:ds-data="users"
:ds-filter-fields="{ firstName: 'John' }"
:ds-filter-fields="{ firstName: 'John', lastName: startsWithD }"
:ds-sortby="['lastName']"
:ds-search-in="['firstName', 'lastName']"
:ds-search-as="{ birthDate: searchAsEuroDate }"
Expand Down Expand Up @@ -66,17 +66,20 @@ firstName "John" and all lastNames that start with the letter "D"
}
```

`startsWithD` can be a function defined in your instance methods
`startsWithD` can be a predicate function defined in your instance methods.
The function takes two arguments, the value of the data object property and the current row data.

```js
startsWithD (value) {
return value.toLowerCase().startsWith('D')
startsWithD (value, row) {
return value.toLowerCase().startsWith('d')
}
```

````
#### ds-sortby
Type: `Array`
Type: `Array`
Default: <em>Empty Array</em>
It defines the data object properties by which the dataset object will be sorted.
Expand All @@ -86,7 +89,7 @@ For example this will sort the data by lastName
```html
['lastName']
```
````

#### ds-search-in

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function fieldFilter(items, filterFields) {
for (const itemKey in itemValue) {
if (itemKey === filterKey) {
if (typeof filterFields[filterKey] === 'function') {
return filterFields[filterKey](itemValue[itemKey])
return filterFields[filterKey](itemValue[itemKey], itemValue)
}
if (filterFields[filterKey] === '') {
return true
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/helpers/fieldFilter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ describe('fieldFilter', () => {

expect(res.length).toBe(4)
})

it('filters the given array by first names that start with "Ga" and last names that start with "Arr"', () => {
const filterFunction = (value, row) => value.startsWith('Ga') && row.lastName.startsWith('Arr')
const res = fieldFilter(filterData, { firstName: filterFunction })

expect(res.length).toBe(1)
})
})

0 comments on commit ab9671d

Please sign in to comment.