Skip to content

Commit

Permalink
fix filter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Kretzer committed Jun 30, 2016
1 parent 5a2d34c commit e7cfcc8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/Ardagryd.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,25 @@ const Ardagryd = (props)=>{


let objects = props.objects.filter((currentObjectToBeFiltered) => {
let currentColumn;
let shouldPass = true;
for (let i in columnNamesWithFilter){
const currentColumn = columnNamesWithFilter[i];
if (!currentObjectToBeFiltered[currentColumn]){
return false;
} else {
currentColumn = columnNamesWithFilter[i];
let customFilterFunction;
if (columnConfig
&& columnConfig[currentColumn]
&& columnConfig[currentColumn].filterFunction) {
customFilterFunction = columnConfig[currentColumn].filterFunction;
}
if (customFilterFunction){
return customFilterFunction({value: currentObjectToBeFiltered[currentColumn],
shouldPass = customFilterFunction({value: currentObjectToBeFiltered[currentColumn],
expression: filters[currentColumn]});
} else {
return filterFunction({value: currentObjectToBeFiltered[currentColumn],
shouldPass = filterFunction({value: currentObjectToBeFiltered[currentColumn],
expression: filters[currentColumn]});
}
if (!shouldPass){
return false;
}
}
return true;
Expand Down Expand Up @@ -210,7 +211,7 @@ const defaultConfig = {
sortValueGetter: ({value}) => value && typeof value === "string" ? value.toLowerCase() : value ? JSON.stringify(value).toLowerCase(): value,
filterFunction: ({value, expression}) => {
if (!(typeof expression === "string") || expression === "" || expression === undefined || expression === null){return true}

if (value === undefined || value === null) {return false}
if ((typeof value === "string" ) && value.toLowerCase().indexOf(expression.toLowerCase()) === -1){
return false;
} else if (typeof value === "object" ){
Expand Down
17 changes: 17 additions & 0 deletions test/FilterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,21 @@ describe('Grid filter tests', function () {
expect(grid.find("tbody").children().length).be.equal(2);
});

it('Should use more than one filter expression', ()=>{
let grid = mount(<Grid objects={[{name: "aa", email: "aa@www.com"}, {name: "ab",email:"adsf@adsf.de"}, {name: "ccb",email:"adsf@ajgj.de"}]} filter={[{columnName: "name", expression: "a"}]} />);
expect(grid.find("tbody").children().length).be.equal(2);

grid.setProps({filter: [{columnName: "name", expression: "a"},{columnName: "email", expression: ".com"}]});

expect(grid.find("tbody").children().length).be.equal(1);
});

it('Filter should not be ignored on missing property', ()=>{
let grid = mount(<Grid objects={[{name: "aa", email: "aa@www.com"}, {name: "ab"}, {name: "ccb"}]} filter={[{columnName: "name", expression: "a"}]} />);
expect(grid.find("tbody").children().length).be.equal(2);

grid.setProps({filter: [{columnName: "name", expression: "a"},{columnName: "email", expression: ".com"}]});

expect(grid.find("tbody").children().length).be.equal(1);
});
});

0 comments on commit e7cfcc8

Please sign in to comment.