-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using mui-datatables in a type-safe way by having access to the original object #475
Comments
hey @dandrei thanks for taking the time to write and explain your issue. This would be a big breaking change and I'm not comfortable at the moment doing this. With that said, if a enough of the user community comes back saying this is affecting them I would consider making this change. I will make this issue for community feedback |
@gregnb thanks for your prompt reply. In the meanwhile, I've written an API that sits in front of the It takes:
It then produces What happens under the hood is that I use a hidden ( The
As mentioned previously, the Long story short, here's how I use it:
Note 1: The functions passed to Note 2: Real-life use cases are much more complex than just rendering the raw value. I'm drawing graphs, rendering the same data differently based on the column it's in, using values from multiple fields to render a single cell, etc. This logic can all be painlessly specified in the This is the code that's working behind the scenes, if anyone's interested:
|
I second better typescript support and flexible value resolution, but putting those points aside, the lacking source object parameter in |
Hey, How about just leveraging the TS compiler for strong typing of the data? I have just taken a few minutes to modify the Definitely Typed typings here. It just uses generics to infer the type of the data and also uses Give it a test and if you think it works, then feel free to submit a PR to the definitely typed repo. |
Quick update: out of necessity, I added another parameter that I can optionally add to any column definition: How I use it:
This allows me to change how any column is displayed right there in the definition. That definition gets into the
|
@dandrei please explain more on how i can use this solution, have been trying to implement it but am not getting it work. Thanks |
(largely based on my similar reply to issue #109, but decided it was more appropriate to start an issue as well to get the discussion rolling)
Hi @gregnb, thank you for your great work on this.
I wonder if it's possible use
mui-datatables
in a more "type-safe" way. Maybe it's already available or maybe it's something that can be easily implemented.Currently we specify the columns as such:
This will take the
object_field
field from each object in the array sent todata
. However, specifying the field name as a string isn't type-safe. TypeScript has no way of knowing whether objects indata
contain or don't contain the fieldobject_field
.Question:
Is it possible perhaps, instead of having
name
as a required field, another option could be added? (let's call itvalue
, required whenname
isn't specified), that would be used as such:This way, we could use it with TypeScript:
...and TypeScript would check that
object_field
does indeed belong toKnownType
. Since it's an extra option, it wouldn't interfere with the current way of doing things or JS-only users.Would also be nice to get the original data object in
customBodyRender
. Use case for this is: if we want to render one column based on a value from another.Right now we do have access to the values array in
tableMeta.rowData
, The caveat is: the order matters. Changing the column order requires a change in how we accesstableMeta.rowData
. But would be nice to be able to keep the type information if using TypeScript.So maybe we could have the original object available in the parameters sent to
customBodyRender
?What are your thoughts? Thanks!
The text was updated successfully, but these errors were encountered: