Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow valueFilter to filter-in and filter-out without listing all values. Like an include and exclude. #77

Merged
merged 1 commit into from
Apr 1, 2024
Merged
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
45 changes: 39 additions & 6 deletions src/PivottableUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ export default {
cols: [],
rows: [],
attributes: [],
/**
* ValueFilter's keys with the special field '*' will match all values and filter them out (true) or show (false).
Sample:
```
valueFilter: {
field1: {
'*': true, // filter out all values
'value1': true, // filter out value1
'value2': false // select to display value2
},
field2: {
'*': false, // select to display all values
'value1': true, // filter out value1
'value2': false // select to display value2
}
}
```
*/
valueFilter: {},
renderer: null
},
Expand Down Expand Up @@ -237,15 +255,30 @@ export default {
this.propsData.aggregatorName = this.aggregatorName
this.propsData.attributes = this.attributes.length > 0 ? this.attributes : Object.keys(this.attrValues)
this.unusedOrder = this.unusedAttrs
Object.keys(this.attrValues).forEach(key => {
let valueFilter = {}
const values = this.valueFilter && this.valueFilter[key]
if (values && Object.keys(values).length) {
valueFilter = this.valueFilter[key]
const allSelector = '*'
Object.entries(this.attrValues).forEach(([key, values]) => {
let attributes = {}
const valueFilterItem = this.valueFilter && this.valueFilter[key]
if (valueFilterItem && Object.keys(valueFilterItem).length) {
if (valueFilterItem[allSelector] === true) {
// add all keys to be filtered out
Object.keys(values).forEach(k => {
if (k !== allSelector) {
const keyPresent = valueFilterItem[k]
if (keyPresent === undefined || keyPresent === true) {
attributes[k] = true
} else {
// attributes[k] = false
}
}
})
} else {
attributes = valueFilterItem
}
}
this.updateValueFilter({
attribute: key,
valueFilter
valueFilter: attributes
})
})
},
Expand Down
17 changes: 14 additions & 3 deletions src/helper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,26 @@ class PivotData {
}
)
}
filter (record) {

filter(record) {
const allSelector = '*'
for (const k in this.props.valueFilter) {
if (record[k] in this.props.valueFilter[k]) {
return false
if (k !== allSelector) {
const valueFilterItem = this.props.valueFilter && this.props.valueFilter[k]
if (record[k] in valueFilterItem) {
const existingKey = valueFilterItem[record[k]]
if (existingKey === true) {
return false
}
} else if (valueFilterItem[allSelector] === true) {
return false
}
}
}
return true
}


forEachMatchingRecord (criteria, callback) {
return PivotData.forEachRecord(
this.props.data,
Expand Down