Datatable with multi header
Datatable with multi header

Hi Team,
My requirement is like as below
1] Multi-header
2] Category should be display as Group
3] Excel and pagging on TOP display left side and search text box should be display right side.
I have created the html and when we ran the app we are getting an unknown parameters error.
When I implemented the same html into editor the excel button, grouping and pagging is not working see the example
https://live.datatables.net/teduneji/1/
Attached the actual output we need with column sorting, pagging, search and excel.
- this should be removed
Appreciate your help!
Thank you
Answers
Hi Team,
https://live.datatables.net/teduneji/4/
I have created another example but I am getting below error
Uncaught TypeError: Cannot read properties of undefined (reading 'cell')
at Te (datatables.min.js:16:23904)
at _e (datatables.min.js:16:24169)
at n (datatables.min.js:16:34521)
at Me (datatables.min.js:16:35329)
at HTMLTableElement.<anonymous> (datatables.min.js:16:6980)
at E.each (jquery-3.5.1.slim.min.js:2:3182)
at E.fn.init.each (jquery-3.5.1.slim.min.js:2:1660)
at E.fn.init.V [as dataTable] (datatables.min.js:16:1327)
at H.fn.DataTable (datatables.min.js:16:95586)
at HTMLDocument.<anonymous> (4:528:44)
The
colspan
values add up to 18 in the first two header rows. In the third they only add up to 12. Each header row needs to have the same total number ofcolspan
. Dataatables uses the first -tag thead` row to determine the number of columns. It places sorting event listeners in each cell. I believe this error is coming from the fact that you are missing the last 6 cells in the third header and thus they are undefined.You defined
colspan
in thetbody
which is not supported by Datatables. See the html requirements doc for more details. I believe this is why you are seeing the extra cells to the right of the table.See the url linked in the error for troubleshooting steps:
https://datatables.net/manual/tech-notes/4
It is due to using
colspan
in thetbody
. Datatables counts eachtd
as a cell and doesn't pay attention to thecolspan
. Datatables sees 16 columns defined in the -tag tbodybut 18 in the -tag thead
. Datatables believes that the data for columns 17 and 18 are missing thus the error.Looks like you are using the
Target
column for this. Is this working as expected?Use the
layout
option for this. Here are some examples.Kevin
Hi Kevin,
Thanks for reply and as you suggested I made equal colspan and it fixed the issue.
Can you please let me know how we can make this responsive?
The current pagging is into center, I want to be set to the left side end and how can we change the pagging buttons and text?
I want to take of the datatable scroll bar and make it responsive.
https://live.datatables.net/teduneji/9/
I want to change the pagging UI like attached and want to merge the buttons as attached.
You need to add the Responsive library. See the Responsive download docs for details.
It sounds like you want to apply styling to the buttons. You can use
buttons.buttons.className
to assign classes then use CSS to apply the appropriate styling.Kevin
Hi Kevin,
When I applied the bootstrap js and css it gives me an error, I have already set responsive property. When I add below two scripts
it gives started me an error, see the reference.
Uncaught TypeError: Cannot read properties of undefined (reading 'cell')
at vt (dataTables.min.js:4:56009)
at X.<anonymous> (dataTables.min.js:4:62640)
at X.iterator (dataTables.min.js:4:48690)
at X.<anonymous> (dataTables.min.js:4:62601)
at X.<anonymous> (dataTables.min.js:4:50384)
at X.header (dataTables.min.js:4:49642)
at X.<anonymous> (dataTables.responsive.js:574:28)
at X.map (<anonymous>)
at X.map (dataTables.min.js:4:48902)
at Responsive._classLogic (dataTables.responsive.js:572:5)
https://live.datatables.net/teduneji/10/
I copied your full HTML code into the W3C Validator and it reports this error affect Datatables.
Looks like your second header row has 16 cells where the first has 15.
Kevin
Also this error in the
tbody
:I guess an additional
th
needs to be added to the first header.Also the test case was missing
dataTables.responsive.min.js
andresponsive.bootstrap5.js
but hasresponsive.bootstrap5.min.css
.I added the two responsive JS includes and added a
th
to the last column in the first header.https://live.datatables.net/teduneji/11/edit
Responsive is now working.
EDIT: Typically when you see
Cannot read properties of undefined (reading 'cell')
you can assume there is a mismatch in the number of columns in thethead
andtbody
and start troubleshooting there.Kevin
Thank you. so much Kevin. You are star
Hi Kevin,
https://live.datatables.net/teduneji/11/edit
Can we remove this scrollbar?
You have this:
Which enables Bootstrap's table responsive. It's goal is similar to Datatables Responsive. They will compete with each other. Remove he class
table-responsive
or thediv
to use Datatables Responsive.Also this is incorrect:
According to the Responsive docs use either
responsive
ordt-responsive
on thetable
tag. This is redundant to usingresponsive: true
. You can use both but I would suggest using only one to reduce confusion later when making changes or troubleshooting.Kevin
HI Kevin,
https://live.datatables.net/teduneji/15/
Thanks it worked, but now I have another problem, When I expand the row it is not displaying the first header title such as 2026,2027 and Overall Total, see example. If you want to see the title then you have to zoom out you can see two columns header.
Actual output

Your test case still has only 15 columns in the first header:
I believe Responsive follows the
orderCellsTop
definition to choose when header row to use for the titles in the default Responsive renderer. The default is to use the bottom header row. You will need to useresponsive.details.renderer
to fetch the titles from different header rows. See this example. Usecolumn().title()
to specify the row to get the title from.I updated your test case to show an example:
https://live.datatables.net/teduneji/16/edit
I updated the last column with
colspan="2"
otherwise my test case will result in your favorite errorCannot read properties of undefined (reading 'cell')
Kevin
Hi Kevin,
Thank you
Can we hide other fields and show when we click on particular header column like slider in excel.
For example, we will hide the fields related to 2026 and 2027 and show particular fields when click on 2026 or 2027 into the same datatable?
Thank you,
Sanket Vyas
One option is to use Column Visibility. See these examples.
Another option is to use
column().visible()
orcolumns().visible()
to programmatically show / hide columns in your event handlers.Kevin
You may have some interesting effects with Responsive since it also hides columns. You may need to use Column Priority like this example or use class control like this examples or this example to keep the proper columns visible.
Kevin
Hi Kevin,
Thank you, I will try to implement and let you know.
Hi Kevin,
https://live.datatables.net/teduneji/17
When I hide the columns the header is displaying weird. Can we display all the column for section wise e.g all the column for 2025 Actual, Target, Actual, Target, Actual, Target, Forecast, Target, currently to see the last Target column we have to do expand.
Also, I have added one button to Show all the column it shows all the column but how we can display the title name before start the other column? See the second image.
Hi Kevin,
https://live.datatables.net/teduneji/18
Also, when I tried to set the width of the particular column, it's not working.
{ width: '5%', targets: 3 }
Hi Kevin,
I did try to set using the class as well but it's not working.
Appreciate your help.
Thank you,
Sanket Vyas
I think your requirement will be very complex using both responsive and
columns.visible()
. I think using responsive to hide the groups would be easiest. First thing to do is to add theall
class to the first two and last two columns - I assume you want those always visible. Like this:I'm adding
main
, you can call it what you like, to those columns as they aren't part of a section, ie 2025, 2026 or 2027. You will see how its used in a moment.Then add a class to designate the columns in each section, for example:
You could add this class directly in HTML if you prefer.
If you want to start by displaying 2025 then use something like this to force to show and 2026, 2027 to hide:
You could start with adding the
none
orall
class names incolumnDefs
to set the default display.Then toggle the two classnames depending on which section or sections you want to show. For example:
Note the use of the
column-selector
as a class with:not(".main")
. This will select all the columns but the first two and last two with the classmain
.To get the title when the section changes add a variable to keep track of where the sections change:
Then get the first header row title when the section changes:
Prefix the child row HTML with the
tr
for each section change:I added a button to show year 2027.
This solution negates the automatic hiding of columns by responsive. See the following docs for the additional APIs used:
column().header()
responsive.rebuild()
responsive.recalc()
It could be the data in the column is larger than 5% so the browser will widen the column. See this thread for Allan's explanation of how column widths are calculated.
Kevin
Hi Kevin,
https://live.datatables.net/teduneji/20/
Thanks for reply as usual.
Somehow I am able to set the title and header when expand the column. Now the only issue is to set the width of the column.
Thanks.
Hi Kevin,
I did not find this, also I have gone through Allan's explanation but it's not working.
I updated your test case to add table-layout: fixed; to your table. This uses the width's you configured instead the browser determining the width. You can see the column content is actually bigger that you defined causing the text to overwrite the sorting icons:
https://live.datatables.net/teduneji/21
Kevin
Sorry I forgot to post the test case
https://live.datatables.net/fuhehili/1/edit
Never mind my post about table-layout. It is incorrect. However if you apply it with
autoWidth
false you can see the width setting is applied. Note the width setting will need to be applied to all the columns when usingtable-layout.: fixed
.https://live.datatables.net/qojaduco/1
Note I also applied width setting to the first two columns.
Kevin