-
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
"Deprecated: Passing objects in as data is not supported, and will be prevented in a future release." #1040
Comments
I really hope this becomes a supported case because this library seems really nice. I am playing with
|
As discussed here #1011, the table has never officially supported passing object data for table cells, and doing so leads to a number of errors if not handled precisely in custom renders. If you're using a custom render, you're stepping into custom territory, and there's not any strong case I can see for accepting objects for this case. As explained in the issue, you can map something like ids back to arbitrary data/objects. They don't need to be passed into the table to get you the information you need. |
Closing this, as there is no planned support for passing arbitrary data structures into the table. As discussed in #1038, I would accept something that added |
Since #1011 is closed, leaving a comment here. It is not uncommon for a table cell to include more than just a number or string. For example, having a user avatar that has an image url, name and email is a pretty common use-case. Conceptually, and practically, it makes sense to have this data in one place, instead of some placeholder that will need to be mapped back. Mapping to some external data can be very clunky to implement. It makes sense that a user will want to be able to search any visible data in the table too, but this puts limitations on searching and require accessing an external data source to search the table. Not having to re-map and restructure the data you need for every render can give some performance benefits as well. If there is some expensive operation, having to only do it once while constructing the table data is much less overhead rather than managing this on every cell re-render. The concern that the render/search/filter methods could break doesn't seem very reasonable. All JS objects have a |
Very relevant to #1038 |
Perhaps people wouldn't be so disheartened by this backslide in capability if it was clear how to 'map it back'? For example, I build my 'data' object for the table like so:
You'll notice that in the last column, I'd like to render a materialUI How would one re-do this in the 'proper' way? |
@jfederer The proper way to add a button to a column is to use |
Most data passed to the table are grabbed from an API in a json format, which is almost always an array of serialized objects. By removing support for passing objects as data, an overhead is added to the developers so now they need to start modifying their API data for this use case. If the feature is already there and working well, why remove it? |
Agreeing with the above individuals that passing the value of the current index from the Current method: customBodyRender: (value, {rowIndex}) => {
const post = posts[rowIndex];
return (
<Button
component={Link}
variant="outlined"
size="small"
to={{
pathname: `/posts/${post.id}`,
}}
>
View Post
</Button>
)
}, Preferred method: customBodyRender: (value, {actualFullRowData}) => (
<Button
component={Link}
variant="outlined"
size="small"
to={{
pathname: `/posts/${actualFullRowData.id}`,
}}
>
View Post
</Button>
), |
@omacranger Be careful with using The best way to remedy this right now is to do a search for your row data using an id or a key, which is an expensive operation. The search operation being expensive is one of the reasons I believe having direct access to the original data is the best way to go about it. |
@keyvanm Ah, good call. I have a bunch of those options disabled by default right now (download, filter, sort) but I'll refactor that to handle it better. Appreciate the heads heads up. |
@keyvanm It's not an expensive operation if you normalize your data. Then it's a constant-time lookup for the id. |
I'm a React / Redux / Firebase / Firestore / MUI-DataTables newbie ... My problem is that I need to sync Firestore database collection ('tasks') with a MUI-DataTable via Redux. The Firestore database stores timestamps as objects but I needed to format the dates to the users using toDate(). Here's the solution I came up with:
The fix was to create a mutable deep copy (Array.from) of the array prop being synched with the Firestore collection and to the check on the initial component render and subsequent renders whether or not the timestamp had been converted from a type of object to string yet in the tableData array. I thought I'd share this because it took me a while to figure it out. :) |
If customBodyRender could include a dataIndex in the callback i believe that would solve a lot of use cases without needing to pass through the full data object. For anyone who struggles with this, what i did was when generating the table data, i would always include the "data index" as the very last column. I wouldn't include it in the columns definition so it would never be show. Then it's a case of the following in the customBodyRender callback const dataIndex = tableMeta.rowData[tableMeta.rowData.length - 1]; |
First of all, I love your datatable! But I have to admit I stumbled across the same issue of not having access to the rowData as object, which would be really convinient. I solved that by wrapping the MUIDatatable component and adding an invisible column containing the id. Then I'm overriding the customBodyRender method of all columns that have one to add the rowDataAsObject as an additional paramter.
|
As an FYI, I've taken on maintainer responsibilities for this project. I've dug into this issue. It stems from this #915 issue, which is pretty easy to resolve (though though there are other areas that also need to be touched up to ensure full object support). I think the ability to pass objects is something this library should support, and it clearly is something people find useful. In the next release, this coming Sunday, this restriction will be removed. I'll also be introducing a customBodyRenderLite method which will be passed the dataIndex. This method will have better performance over the customBodyRender, though if you're interested in the details you can check out the release notes when they go up. |
Hi, I'm testing this lib to replace my current one and so far it's very good. I want to ask if you plan to return the full object of data, and not only an array with some of the value? |
What he suggested is now possible with the customBodyRenderLite method, which takes in the dataIndex. I’m not sure it’d be worth it to pass the data too, though I could consider it. The main problem would be that the table currently doesn’t internally store it after it transforms the data, so it would have to be attached somewhere. |
This was going to be a big help, since if I could access the object from About the fact of the table storing the array, is it really necessary? I'm asking this because I'm passing the Something like |
Yes, the table transforms/normalizes the data it receives: https://github.com/gregnb/mui-datatables/blob/master/src/MUIDataTable.js#L649
I'm a little confused why this is needed. customBodyRender gives you access to the row of transformed data via tableMeta.rowData (which is array[dataIndex] of the internal array). And if you want access to your raw data instead, customBodyRenderLite gives you the dataIndex. So you could you do something like this:
|
Yes, but this way I need to update my columns every time I update my data array, since the |
It does not. If you update your data array, I'm assuming you want the data in the table updated too, and thus a re-render would occur anyway (the data is a prop to the table, if it changes, the table re-renders). If your data is staying the same but changing because of a parent re-render (and you've defined your data as a local variable), you can avoid a re-render by putting the data on the state. If you define your columns and data in the same scope. You should be fine. If you're defining your columns in an external file, you could do something like this:
Additionally, the external data matches the internal data with only 2 exceptions:
|
Thanks for the reply, I'm testing things out here, and I'm going like this for now. I'm currently using this lib to show the data and use some of its features, I'm using a custom toolbar that I had here before. I'd like to say that the Just some suggestions:
|
|
|
Just upgraded to version
2.12.4
and I get the following:Deprecated: Passing objects in as data is not supported, and will be prevented in a future release. Consider using ids in your data and linking it to external objects if you want to access object data from custom render functions.
I am currently using this option as a work-around in order to access the original object from the rendering function, as described in my reply to issue 475.
Other recently opened issues (like 1038) are also asking for a way to access the original row object.
Is this feature being considered for inclusion in the future?
Thanks.
The text was updated successfully, but these errors were encountered: