0

I'm trying to render grid with multiple toggling filters. I'm Using react hooks.

This is data array:

const items = [
    {
        name: 'hotel1',
        type: 'hotel'
    },
    {
        name: 'bar1',
        type: 'bar'
    },{
        name: 'entertainment1',
         type: 'entertainment'
    },{
        name: 'equipment1',
        type: 'equipment'
    },
    {
        name: 'shop1',
        type: 'shop'
    }
]


    const initialFilters = [
        {
            id: 1,
            active: false,
            type: 'bar'
        },
        {
            id: 2,
            active: false,
            type: 'shop'
        },
        {
            id: 3,
            active: false,
            type: 'hotel'
        },
        {
            id: 4,
            active: false,
            type: 'entertainment'
        },
        {
            id: 5,
            active: false,
            type: 'equipment'
        },
    ];

const [data, setData] = useState(items);
const [filterItems, setFilteredItems] = useState(initialFilters);

currently I'm filtering with single key that is passed

const mainFilter = (key) => {
 setData(items.filter(x => x.type === key));
}

and filter buttons with grid item names are rendered:

return (
    <div>
        <ul>
            {filterItems.map(x =>
                <li key={x.type}><a
                    className={x.active == true ? 'active' : ''}
                    onClick={() => mainFilter(x.type)}>
                    {x.type}
                </a></li>
            )}
        </ul>
     <div>{data.map(item => <div>{item.name}</div>}</div>
    </div>
)

I need to get the functionality where when pressed for example shop filter, it should only leave items with type shop. if you press bar filter, it should only leave items with bar an shop and it should work all the way to all 5 filters. if none are selected, it should show full array.

I've tried converting logic from this:

https://gist.github.com/jherax/f11d669ba286f21b7a2dcff69621eb72#file-filterplainarray-js

but no luck yet

2 Answers 2

1

You can use the function every for checking if all flags have value false

This code snippet, have an initialFilter with all the flags equal to false

const items = [{name: 'hotel1',type: 'hotel'},{name: 'bar1',type: 'bar'}, {name: 'entertainment1',type: 'entertainment'}, {name: 'equipment1',type: 'equipment'},{name: 'shop1',type: 'shop'}];
const initialFilters = [{id: 1,active: false,type: 'bar'},{id: 2,active: false,type: 'shop'},{id: 3,active: false,type: 'hotel'},{id: 4,active: false,type: 'entertainment'},{id: 5,active: false,type: 'equipment'}];

let allFalse = initialFilters.every(({active}) => !active);
if (allFalse) console.log(items);

Otherwise, you can use the function filter along with the function some:

This code snippet has as active shop and entertainment

const items = [{name: 'hotel1',type: 'hotel'},{name: 'bar1',type: 'bar'}, {name: 'entertainment1',type: 'entertainment'}, {name: 'equipment1',type: 'equipment'},{name: 'shop1',type: 'shop'}];
const initialFilters = [{id: 1,active: false,type: 'bar'},{id: 2,active: true,type: 'shop'},{id: 3,active: false,type: 'hotel'},{id: 4,active: true,type: 'entertainment'},{id: 5,active: false,type: 'equipment'}];

let fltered = items.filter(({type}) => initialFilters.some(({type: t, active}) => t === type && active));
console.log(fltered);

Sign up to request clarification or add additional context in comments.

Comments

0

You can store the selected keys and then sort by checking if they are included:

const mainFilter = (keys) => {
 setData(items.filter(x => keys.includes(x.type)));
}

This implementation would need a change in your onClick function - instead of calling a function with the key you'll need to store the keys.

1 Comment

I've tried storing keys in array, declaring new array and adding this code in that function: const mainFilter = (key) => { if(filterArray.includes(key)){ setFilterArray(filterArray.filter(x => x !== key)) } else { setFilterArray([...filterArray, key]); } } last step would be to get a function which will filter array by array of keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.