Skip to content

Commit

Permalink
Merge pull request #77 from raisercostin/master
Browse files Browse the repository at this point in the history
Allow valueFilter to filter-in and filter-out without listing all values. Like an include and exclude.
  • Loading branch information
Seungwoo321 authored Apr 1, 2024
2 parents 0c2e6fb + 6cf8e91 commit b719f86
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
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

0 comments on commit b719f86

Please sign in to comment.