Skip to content

Commit

Permalink
#380 - Added GlobalFilter support
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Feb 12, 2019
1 parent 39aa154 commit 7adb497
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/components/treetable/TreeTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class TreeTable extends Component {
columnResizeMode: 'fit',
emptyMessage: "No records found",
filters: null,
globalFilter: null,
filterMode: 'lenient',
onFilter: null,
onExpand: null,
Expand Down Expand Up @@ -130,6 +131,7 @@ export class TreeTable extends Component {
columnResizeMode: PropTypes.string,
emptyMessage: PropTypes.string,
filters: PropTypes.object,
globalFilter: PropTypes.any,
filterMode: PropTypes.string,
onFilter: PropTypes.func,
onExpand: PropTypes.func,
Expand Down Expand Up @@ -380,12 +382,6 @@ export class TreeTable extends Component {
filters: newFilters
});
}

if (this.props.onValueChange) {
this.props.onValueChange(this.processData({
filters: newFilters
}));
}
}

hasFilter() {
Expand Down Expand Up @@ -758,20 +754,22 @@ export class TreeTable extends Component {
let filters = this.getFilters();
let columns = React.Children.toArray(this.props.children);
const isStrictMode = this.props.filterMode === 'strict';
let isValueChanged = false;

for(let node of value) {
for (let node of value) {
let copyNode = {...node};
let localMatch = true;
let globalMatch = false;

for(let j = 0; j < columns.length; j++) {
for (let j = 0; j < columns.length; j++) {
let col = columns[j];
let filterMeta = filters ? filters[col.props.field] : null;
let filterField = col.props.field;
let filterValue, filterConstraint, paramsWithoutNode;

//local
if(filterMeta) {
let filterMatchMode = filterMeta.matchMode||col.props.filterMatchMode;
if (filterMeta) {
let filterMatchMode = filterMeta.matchMode || col.props.filterMatchMode;
filterValue = filterMeta.value;
filterConstraint = filterMatchMode === 'custom' ? col.props.filterFunction : ObjectUtils.filterConstraints[filterMatchMode];
paramsWithoutNode = {filterField, filterValue, filterConstraint, isStrictMode};
Expand All @@ -780,18 +778,38 @@ export class TreeTable extends Component {
localMatch = false;
}

if(!localMatch) {
if (!localMatch) {
break;
}
}

//global
if (this.props.globalFilter && !globalMatch) {
let copyNodeForGlobal = {...copyNode};
filterValue = this.props.globalFilter;
filterConstraint = ObjectUtils.filterConstraints['contains'];
paramsWithoutNode = {filterField, filterValue, filterConstraint, isStrictMode};
if ((isStrictMode && (this.findFilteredNodes(copyNodeForGlobal, paramsWithoutNode) || this.isFilterMatched(copyNodeForGlobal, paramsWithoutNode))) ||
(!isStrictMode && (this.isFilterMatched(copyNodeForGlobal, paramsWithoutNode) || this.findFilteredNodes(copyNodeForGlobal, paramsWithoutNode)))) {
globalMatch = true;
copyNode = copyNodeForGlobal;
}
}
}

if(localMatch) {
let matches = localMatch;
if (this.props.globalFilter) {
matches = localMatch && globalMatch;
}

if (matches) {
filteredNodes.push(copyNode);
}

isValueChanged = isValueChanged || !localMatch || globalMatch;
}

return filteredNodes.length ? filteredNodes : value;
return isValueChanged ? filteredNodes : value;
}

findFilteredNodes(node, paramsWithoutNode) {
Expand All @@ -818,7 +836,7 @@ export class TreeTable extends Component {
isFilterMatched(node, {filterField, filterValue, filterConstraint, isStrictMode}) {
let matched = false;
let dataFieldValue = ObjectUtils.resolveFieldData(node.data, filterField);
if(filterConstraint(dataFieldValue, filterValue)) {
if (filterConstraint(dataFieldValue, filterValue)) {
matched = true;
}

Expand Down Expand Up @@ -846,7 +864,7 @@ export class TreeTable extends Component {
}

let localFilters = this.getFilters();
if (localFilters) {
if (localFilters || this.props.globalFilter) {
data = this.filterLocal(data, localFilters);
}
}
Expand Down

0 comments on commit 7adb497

Please sign in to comment.