Open alielkhateeb opened 5 years ago
So, I think this might be down to some confusion over what customBodyRender
is doing as far as your ability to filter your data. That's not your fault; the behavior is a bit confusing.
If you are using customBodyRender
with your own filter logic, and the code is able to resolve your render function to a value, it will use that value. If it can't, it will resort to the initial value in your data. So in your case, the render function resolves directly to a string, so the filters are using the string you supplied, not the value in your original data. You can therefore fix your issue by checking if salary === "Yes"
and salary === "no"
in your custom filter logic, and it should work as expected.
It's a bit difficult to predict this behavior at the moment, and it's not well documented, so I'm open to suggestions about how to make this more clear. I'm also open to changing the behavior, however, that would have to wait a while for a major release as it would be a breaking change.
@gabrielliwerant Oh ok I get it, yeah you're right it's a bit confusing.
Your workaround for comparing string works well, but I modified the code a bit to make customBodyRender
return a react component with the prop value = {!value}
this obviously causes a problem in the value that goes to the logic function (it will always be true), if I change the props I send in value={value}
it works fine, It's very weird, I think in that case this is a bug, no?
NOTE: I edited the sandbox above to elaborate better.
Is there any solution for boolean data value issue?
Just use the proposed workaround. For the example in the codesandbox above, just change the salary column to this and it will work as expected:
{
name: "Salary",
options: {
filter: true,
filterType: "checkbox",
filterOptions: {
names: ["Yes", "No"],
logic(salary, filterVal) {
const show =
(filterVal.indexOf("Yes") >= 0 && ( salary === true || salary === 'true') ) ||
(filterVal.indexOf("No") >= 0 && ( salary === false || salary === 'false') );
return !show;
}
},
customBodyRender: (val) => {
return val === true ? "true" : "false";
}
}
}
Hmm, actually, after investigating this a little more, that may not do it for every case. There's currently a PR (https://github.com/gregnb/mui-datatables/pull/1328) that will add the raw row data to the logic call. That means you would get the unprocessed data. That would solve this issue. That will be in the 3.0.1 release, which will be out in a few days.
If the column data is boolean and
customBodyRender
is set, the logic function always receivestrue
value.Expected Behavior
The
value
should be eithertrue
orfalse
depending on the actual valueCurrent Behavior
Value received in
logic
function is alwaystrue
Steps to Reproduce (for bugs)
https://codesandbox.io/s/trusting-tharp-2ldj7
salary
withYes
orNo
it won't work with the optionNo
as the value is always true.customBodyRender
lineAnother way to see the problem you could
console.log(salary)
in the beginning of thelogic
function and re-run the above procedure to see the value sent to thelogic
functionYour Environment