I wish to make stacked responsive tables with React. To avoid horizontal scrolling in a small screen, each row is displayed as mini-tables; please see included images.
For my use case, content in td
(and maybe th
) tags may be string, number or rich nodes.
I want to implement the same in React, since no library exists yet for this. What is a good approach?
I have examined two approaches.
First one is Javascript-based, like jQuery-based StackTable, from where I included the images. It turns the table into a table+card tables combination, and then uses provided media query to show either the table or card table. The cardtable function traverses the table and generates two-column key value tables from each row.
This seems easy in react: render the table and card table and use media query to show either one. But is rendering same node (as opposed to strings) at multiple places safe? A Every
th
element will be rendered for actual table header and for every single row.Second one uses pure CSS, such as this one from CSS Tricks.
/* if media query */ table, thead, tbody, th, td, tr { display: block; } td:nth-of-type(1):before { content: "First Name"; } td:nth-of-type(2):before { content: "Last Name"; } td:nth-of-type(3):before { content: "Job Title"; }
The "content" thing seems iffy though, we have to maintain a copy-paste atrocity of contents in an otherwise DRY codebase. But I hope this will be easier with React.
Example of a stacked responsive table:
Full table:
Responsive: