mui-datatables icon indicating copy to clipboard operation
mui-datatables copied to clipboard

on custom filters, chip always displays

Open terraswat opened this issue 5 years ago • 10 comments

When there are custom filters the chip always displays. When the X on the chip is clicked the filter does reset, but the chip remains.

Thank you for the awesome component! ❤️

Expected Behavior

I expect the chip to disappear when its X is clicked with custom filters.

Current Behavior

The chip does not disappear when its X is clicked with custom filters.

Steps to Reproduce (for bugs)

Use column options such as below:

    const options = {
        filter: true,
        filterList: filterText,
        filterType: 'custom',
        customFilterListRender: v => 'Filter On',
        filterOptions: {
            names: filterText,
            logic: filterOut,
            display: Display,
        },
        sort: false,
    }

Your Environment

Tech Version
Material-UI @material-ui/[email protected]
MUI-datatables [email protected]
React [email protected]
browser chrome 75.0.3770.142
platform mac

terraswat avatar Aug 06 '19 19:08 terraswat

Hello @terraswat. Unfortunately, I can't verify the issue because your code contains a number of variables that I don't have. Can you provide a more complete example, ideally in https://codesandbox.io, so I can be sure we're looking at the same thing?

gabrielliwerant avatar Aug 06 '19 20:08 gabrielliwerant

Ok, so I messed around with the code, and I think I know the issue, although I'm not sure it's a bug.

The customFilterListRender is doing exactly what you are telling it, which is to always return the string 'Filter On'. This means it will always display that, no matter if you have a filter value or not. What you'd need to do is add something like

if (v.length) return 'Filter On';
else return false;

That way the custom filter will not return a string in the case that there is no filter. Depending on what you're trying to do, you may need more complicated logic because you probably need to specify each filter case (if this value exists, show something, otherwise, show nothing so the chip will vanish).

gabrielliwerant avatar Aug 06 '19 20:08 gabrielliwerant

Thank you Gabriel, that works! I used this code to set the chip visibility:

   customFilterListRender: v => {
            return (v !== undefined && v.length > 0 && v[0] !== '')
                ? 'Filter'
                : false
        },

A related question: Is there an option to programmatically call the 'reset filter' function?

I'm using a text field for the custom filtering and would like the filtering to be reset when the text field is empty of characters using the logic in the above conditional.

Currently the chip properly disappears when I reset by clicking the reset button on the filter pop-up, or by clicking the 'X' on the chip, however it does not disappear when I clear the text field.

terraswat avatar Aug 06 '19 21:08 terraswat

Glad that worked for you!

As to your other question, yes there is, but it involves managing the state of the filterList, like in this pseudo code:

this.state = { nameFilterList: ['Franky Miles'] };

const columns = {
  name: "Name",
  options: {
    filter: true,
    filterList: this.state.nameFilterList
  }
};

<button onClick={() => this.setState({ nameFilterList: [] })}>Reset Filters</button>

gabrielliwerant avatar Aug 07 '19 02:08 gabrielliwerant

Thank you Gabriel, that worked perfectly! Please carry on with this awesome package.

terraswat avatar Aug 27 '19 17:08 terraswat

Curious if this is an issue for anyone else? Returning false is just returning an empty chip when doing this: customFilterListRender: v => { return (v !== undefined && v.length > 0 && v[0] !== '') ? 'Filter' : false },

akirwin avatar Sep 27 '19 16:09 akirwin

I'm not using a custom filter, and I have this problem when I set filterList to anything. The chips for the filters in the filterList will always be displayed, even though they are removed from the filterList in the tableState. They still have x's on them like they can be removed, but they can't be removed because they get added back immediately.

carlin-q-scott avatar Nov 25 '20 17:11 carlin-q-scott

@carlin-q-scott - did you ever find a solution? Running into the same issue.

JeremyEnglert avatar Aug 16 '21 23:08 JeremyEnglert

I don't know if this will help someone, but in my case i had a custom select filter and the chip for that filter would not close.

Since CustomFilterListRender is deprecated, i was using customFilterListOption with the update function like so: customFilterListOptions: { render: v => v , update: (filterList) => filterList }

Removing the update function override solved my problem.

Tpleme avatar Nov 08 '22 18:11 Tpleme

I know it's been a while but this one is still open. Anyone have any luck with this one? Did either of the solutions above work for anyone? I tried both of the solns above and they did not resolve the issue of a chip not being removed after clicking the close 'x'.
I've set the filterList to display chips for saved filters using onTableInit. New filter chips can be removed but the initial set filter chips in onTableInit cannot be removed.

srmxlt avatar Apr 02 '24 06:04 srmxlt