3

DataTables provide the following example to implement complex headers: https://datatables.net/examples/basic_init/complex_header.html

$(document).ready(function() {
    $('#example').DataTable();
} );
<head>
  <script src="//code.jquery.com/jquery-1.12.3.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
</head>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th rowspan="2">Name</th>
                <th colspan="2">HR Information</th>
                <th colspan="3">Contact</th>
            </tr>
            <tr>
                <th>Position</th>
                <th>Salary</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>E-mail</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Salary</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>E-mail</th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>$320,800</td>
                <td>Edinburgh</td>
                <td>5421</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>$170,750</td>
                <td>Tokyo</td>
                <td>8422</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>$86,000</td>
                <td>San Francisco</td>
                <td>1562</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>$433,060</td>
                <td>Edinburgh</td>
                <td>6224</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Accountant</td>
                <td>$162,700</td>
                <td>Tokyo</td>
                <td>5407</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Integration Specialist</td>
                <td>$372,000</td>
                <td>New York</td>
                <td>4804</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Herrod Chandler</td>
                <td>Sales Assistant</td>
                <td>$137,500</td>
                <td>San Francisco</td>
                <td>9608</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Rhona Davidson</td>
                <td>Integration Specialist</td>
                <td>$327,900</td>
                <td>Tokyo</td>
                <td>6200</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Colleen Hurst</td>
                <td>Javascript Developer</td>
                <td>$205,500</td>
                <td>San Francisco</td>
                <td>2360</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Sonya Frost</td>
                <td>Software Engineer</td>
                <td>$103,600</td>
                <td>Edinburgh</td>
                <td>1667</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jena Gaines</td>
                <td>Office Manager</td>
                <td>$90,560</td>
                <td>London</td>
                <td>3814</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Quinn Flynn</td>
                <td>Support Lead</td>
                <td>$342,000</td>
                <td>Edinburgh</td>
                <td>9497</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Charde Marshall</td>
                <td>Regional Director</td>
                <td>$470,600</td>
                <td>San Francisco</td>
                <td>6741</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Haley Kennedy</td>
                <td>Senior Marketing Designer</td>
                <td>$313,500</td>
                <td>London</td>
                <td>3597</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Tatyana Fitzpatrick</td>
                <td>Regional Director</td>
                <td>$385,750</td>
                <td>London</td>
                <td>1965</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Michael Silva</td>
                <td>Marketing Designer</td>
                <td>$198,500</td>
                <td>London</td>
                <td>1581</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Paul Byrd</td>
                <td>Chief Financial Officer (CFO)</td>
                <td>$725,000</td>
                <td>New York</td>
                <td>3059</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Gloria Little</td>
                <td>Systems Administrator</td>
                <td>$237,500</td>
                <td>New York</td>
                <td>1721</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Bradley Greer</td>
                <td>Software Engineer</td>
                <td>$132,000</td>
                <td>London</td>
                <td>2558</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Dai Rios</td>
                <td>Personnel Lead</td>
                <td>$217,500</td>
                <td>Edinburgh</td>
                <td>2290</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jenette Caldwell</td>
                <td>Development Lead</td>
                <td>$345,000</td>
                <td>New York</td>
                <td>1937</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Yuri Berry</td>
                <td>Chief Marketing Officer (CMO)</td>
                <td>$675,000</td>
                <td>New York</td>
                <td>6154</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Caesar Vance</td>
                <td>Pre-Sales Support</td>
                <td>$106,450</td>
                <td>New York</td>
                <td>8330</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Doris Wilder</td>
                <td>Sales Assistant</td>
                <td>$85,600</td>
                <td>Sidney</td>
                <td>3023</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Angelica Ramos</td>
                <td>Chief Executive Officer (CEO)</td>
                <td>$1,200,000</td>
                <td>London</td>
                <td>5797</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Gavin Joyce</td>
                <td>Developer</td>
                <td>$92,575</td>
                <td>Edinburgh</td>
                <td>8822</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jennifer Chang</td>
                <td>Regional Director</td>
                <td>$357,650</td>
                <td>Singapore</td>
                <td>9239</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Brenden Wagner</td>
                <td>Software Engineer</td>
                <td>$206,850</td>
                <td>San Francisco</td>
                <td>1314</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Fiona Green</td>
                <td>Chief Operating Officer (COO)</td>
                <td>$850,000</td>
                <td>San Francisco</td>
                <td>2947</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Shou Itou</td>
                <td>Regional Marketing</td>
                <td>$163,000</td>
                <td>Tokyo</td>
                <td>8899</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Michelle House</td>
                <td>Integration Specialist</td>
                <td>$95,400</td>
                <td>Sidney</td>
                <td>2769</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Suki Burks</td>
                <td>Developer</td>
                <td>$114,500</td>
                <td>London</td>
                <td>6832</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Prescott Bartlett</td>
                <td>Technical Author</td>
                <td>$145,000</td>
                <td>London</td>
                <td>3606</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Gavin Cortez</td>
                <td>Team Leader</td>
                <td>$235,500</td>
                <td>San Francisco</td>
                <td>2860</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Martena Mccray</td>
                <td>Post-Sales support</td>
                <td>$324,050</td>
                <td>Edinburgh</td>
                <td>8240</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Unity Butler</td>
                <td>Marketing Designer</td>
                <td>$85,675</td>
                <td>San Francisco</td>
                <td>5384</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Howard Hatfield</td>
                <td>Office Manager</td>
                <td>$164,500</td>
                <td>San Francisco</td>
                <td>7031</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Hope Fuentes</td>
                <td>Secretary</td>
                <td>$109,850</td>
                <td>San Francisco</td>
                <td>6318</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Vivian Harrell</td>
                <td>Financial Controller</td>
                <td>$452,500</td>
                <td>San Francisco</td>
                <td>9422</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Timothy Mooney</td>
                <td>Office Manager</td>
                <td>$136,200</td>
                <td>London</td>
                <td>7580</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jackson Bradshaw</td>
                <td>Director</td>
                <td>$645,750</td>
                <td>New York</td>
                <td>1042</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Olivia Liang</td>
                <td>Support Engineer</td>
                <td>$234,500</td>
                <td>Singapore</td>
                <td>2120</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Bruno Nash</td>
                <td>Software Engineer</td>
                <td>$163,500</td>
                <td>London</td>
                <td>6222</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Sakura Yamamoto</td>
                <td>Support Engineer</td>
                <td>$139,575</td>
                <td>Tokyo</td>
                <td>9383</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Thor Walton</td>
                <td>Developer</td>
                <td>$98,540</td>
                <td>New York</td>
                <td>8327</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Finn Camacho</td>
                <td>Support Engineer</td>
                <td>$87,500</td>
                <td>San Francisco</td>
                <td>2927</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Serge Baldwin</td>
                <td>Data Coordinator</td>
                <td>$138,575</td>
                <td>Singapore</td>
                <td>8352</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Zenaida Frank</td>
                <td>Software Engineer</td>
                <td>$125,250</td>
                <td>New York</td>
                <td>7439</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Zorita Serrano</td>
                <td>Software Engineer</td>
                <td>$115,000</td>
                <td>San Francisco</td>
                <td>4389</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jennifer Acosta</td>
                <td>Junior Javascript Developer</td>
                <td>$75,650</td>
                <td>Edinburgh</td>
                <td>3431</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Cara Stevens</td>
                <td>Sales Assistant</td>
                <td>$145,600</td>
                <td>New York</td>
                <td>3990</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Hermione Butler</td>
                <td>Regional Director</td>
                <td>$356,250</td>
                <td>London</td>
                <td>1016</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Lael Greer</td>
                <td>Systems Administrator</td>
                <td>$103,500</td>
                <td>London</td>
                <td>6733</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Jonas Alexander</td>
                <td>Developer</td>
                <td>$86,500</td>
                <td>San Francisco</td>
                <td>8196</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Shad Decker</td>
                <td>Regional Director</td>
                <td>$183,000</td>
                <td>Edinburgh</td>
                <td>6373</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Michael Bruce</td>
                <td>Javascript Developer</td>
                <td>$183,000</td>
                <td>Singapore</td>
                <td>5384</td>
                <td>[email protected]</td>
            </tr>
            <tr>
                <td>Donna Snider</td>
                <td>Customer Support</td>
                <td>$112,000</td>
                <td>New York</td>
                <td>4226</td>
                <td>[email protected]</td>
            </tr>
        </tbody>
    </table>

However, I am using JavaScript initialization:

 var table = $('#example').DataTable( {
      data: myData,
      dom: "B<'row'<'col-sm-6'l><'col-sm-6'f>><'row'<'col-sm-12'tr>><'row'<'col-sm-5'i><'col-sm-7'p>>",
      columns: [ {
        title: "Position",
        data: 'position'
      }, {
        title: "Salary",
        data: 'salary'
      }, {
        title: "Office",
        data: 'office'
      }, {title: "Extn",
        data: 'Extn'}, 
         {title: "Email",
        data: 'Email'}],
});
<table id="cacheTable" class="table-striped">
</table>

This in itself works fine (example doesn't include data/libraries), but now how do I add the complex headers when I instantiate DataTables in this way?

1

1 Answer 1

4

You build complex header by including thead element in your HTML markup, no matter what the data source is. There is no need for columns.title if you're using complex header.

$(document).ready(function() {
    var data = [{"name":"Tiger Nixon","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"}];
  
    $('#example').DataTable({
        data: data,
        columns: [
            { data: "name" },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: "start_date" },
            { data: "salary" }
        ]        
    });
} );
<head>
  <script src="//code.jquery.com/jquery-1.12.3.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
</head>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th rowspan="2">Name</th>
                <th colspan="2">HR Information</th>
                <th colspan="3">Contact</th>
            </tr>
            <tr>
                <th>Position</th>
                <th>Salary</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>E-mail</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Salary</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>E-mail</th>
            </tr>
        </tfoot>
    </table>

Sign up to request clarification or add additional context in comments.

1 Comment

Leaving out all the titles, and defining ALL the columns in the HTML works. Not a pure JavaScript solution but it does the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.