gregnb / mui-datatables

Datatables for React using Material-UI
MIT License
2.71k stars 932 forks source link

on custom filters, chip always displays #816

Open terraswat opened 5 years ago

terraswat commented 5 years ago

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/core@3.9.2
MUI-datatables mui-datatables@2.7.0
React react@16.8.3
browser chrome 75.0.3770.142
platform mac
gabrielliwerant commented 5 years ago

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 commented 5 years ago

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).

terraswat commented 5 years ago

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.

gabrielliwerant commented 5 years ago

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>
terraswat commented 5 years ago

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

akirwin commented 5 years ago

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 },

carlin-q-scott commented 3 years ago

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.

JeremyEnglert commented 3 years ago

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

Tpleme commented 1 year ago

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.

srmxlt commented 6 months ago

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.