How can I add in responsiveness to my table?
How can I add in responsiveness to my table?
I have figured out how to convert my data to json, and I'm trying now to add in responsiveness to the table. For the sake of convenience, I've created a dummy file in codepen that writes in the table data, and I've linked in the scripts and files needed to add in the responsive feature. However, my table will not responsively resize, and I'm not seeing any errors in the console. I have the same issue with my active code as well.
This is a link to the code: https://codepen.io/katprohas/pen/BaemEQe?editors=1010
I've looked through the forums but am not seeing anything stand out as to what I'm missing, any help would be greatly appreciated.
This question has an accepted answers - jump to answer
Answers
Update to DataTables 2.1.8 (the current release) and that will do it. The other option is to add
style="width: 100%
to thetable
(DataTables 2.1+ will do that automatically if no width is found to be defined).Allan
Thanks! That worked in my dummy case. For the active project though, it is being responsive in that the columns are collapsing, but it is not showing any child rows with the hidden columns. Is there something additional I will need to do because the data is being converted from XML to JSON?
This is the code I have. The table and data are being written with Velocity, and the script below is standard javascript.
Are you able to link to your page showing the issue? What happens if you click on the first cell in a table row? Does the child row appear anyway? If so, that would suggest your page isn't loading the Responsive CSS.
Allan
Unfortunately I can't link to the page as it's behind a login, but clicking on the first column does actually make the child row appear! So it is working (hurray!). Do I need to add in an extra empty column at the front of the table so the dropdown menu icon shows?
No, see this example. As Allan mentioned are loading
responsive.dataTables.css
or the appropriate CSS for the styling framework you are using?Use the Download builder to get all the correct files.
Kevin
I'm loading these css files:
<link href="https://cdn.datatables.net/2.1.8/css/dataTables.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/plug-ins/2.0.8/features/alphabetSearch/dataTables.alphabetSearch.css" rel="stylesheet" type="text/css"/>
<link href="https://cdn.datatables.net/responsive/3.0.3/css/responsive.dataTables.min.css" rel="stylesheet" type="text/css"/>
and in the table I've added
style="width: 100%"
That looks correct. If you aren't seeing the icon then there is some styling conflict causing the issue. It will be hard to debug without seeing it.
Try right clicking the first cell in a row. You should see the
dtr-control
class and the::before
pseudo-element. Check to see if that is being overridden by another CSS. You can compare to this example.Kevin
I'm not seeing dtr-control, but the ::before element is being applied to the wrong row, so it seems like there's something wonky going on. I will do some digging and see if I can figure out what's interfering.
What happens if you have simply:
?
Allan
The same thing happens, child row works by clicking on the first column but there are no indicators that it is a drop down. I removed styling from the page except what is default with datatables, and it seems to have the same problem, though my CMS is a bit convoluted and I'm still looking to see if there is stray css code somewhere.
It seems strange the the
dtr-control
class doesn't show in the firsttd
of each row. Possibly some JS code is removing it? I think I would place a breakpoint on the first statement after the Datatables initialization to see the class is there and if it is step through to find where its removed.Kevin
As a test you could try adding a column and applying the responsive control to that like, like this example.
Or, as another test, maybe just add
className: 'dtr-control'
to the first column.Kevin
Okay, so the dtr-control class is now showing in the <td> tag of each row, it was a browser caching issue. Should I still try to manually apply dtr-control to a column to see if the toggle buttons appear?
Okay, I did try adding a class "dtr-control" in columnDefs and I also tried assigning a column specifically the "dtr-control" class to no avail. Adding in columnDefs resulted in the first column disappearing in full-width, and appearing when sized down, but still no toggle. Similar issue with the manually assigned dtr-control class.
The only thing I can think of is to make sure
responsive.dataTables.css
is loaded only once. There might be a styling conflict if more than once is loaded especially different versions.I copied your CSS code snippet into this test case and it works:
https://live.datatables.net/ladicuca/1/edit
Kevin
Okay, I'll check it out. Is is possible to add the ::before pseudo element to force the toggle or it doesn't work like that?
The toggle should appear automatically unless you configure it not to show. One way is to set
responsive.details.type
to"none"
. In this case the::before
wouldn't be added.I don't believe there is a way to force the toggle to show. There is something in your environment that is conflicting with the display of the toggle. You could try using
View Source
and get a copy of the generated HTML table and copy into the last test case I provided. I suspect it will work but its something to try.Kevin
I loaded in my page source, and it looks like it is having the same problem.
Do you have an updated URL link to the test case? It will update when you start editing it.
Kevin
Oops, I didn't know that, here you go: https://live.datatables.net/ladicuca/4/edit
I fixed a couple minor issues:
1- The page is load responsive.js twice. It, along with the other .js files, should be loaded only once. Commented out this line:
2- Added
style="width: 100%"
to thetable
tag.3- Commented out this as it was generating a JS error:
4- Removed the first unneeded column. You can remove the other empty column if you want the toggle in the First Name column.
5- The issue causing the missing toggle is
rel="stylesheet"
is missing from this statement:Added it to this now working test case:
https://live.datatables.net/qajomele/1/edit
Kevin
You are absolutely brilliant! Many thanks to you and Allan for all of your help.