Jin
Jin

Reputation: 932

jQuery DataTable multi rows header

I used jQuery DataTable to show some data.

Here is my code.

I tried to add collapse/expand using bootstrap collapse functionality.

So I made two rows.

But if you see console it throws an javascript error so code below it not running.

How can I fix this?

Here is html code:

<table id="taskTable">
  <thead class="bg-light text-capitalize">
    <tr>
      <th>Subject</th>
      <th>Name</th>
      <th>Duration</th>
      <th>User</th>
    </tr>
    <tr style="display:none;">
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr data-toggle="collapse" data-target="#demo1">
      <td>Subject1</td>
      <td>Name1</td>
      <td>Duration1</td>
      <td>User1</td>
    </tr>
    <tr id="demo1">
      <td>aaa</td>
    </tr>
    <tr data-toggle="collapse" data-target="#demo2">
      <td>Subject2</td>
      <td>Name2</td>
      <td>Duration2</td>
      <td>User2</td>
    </tr>
    <tr id="demo2">
      <td>aaa</td>
    </tr>
  </tbody>
</table>

Upvotes: 0

Views: 1092

Answers (1)

Rajan
Rajan

Reputation: 1497

Not feeling good using this approach: Because of search, the sort, etc will break the concept of <tr> as header and it's next <tr> as content.

Maybe you can achieve the same thing using a different approach, like you can have child rows like here. But you can also fix the above problem as below:

JS Fiddle

HTML

<table id="taskTable">
  <thead class="bg-light text-capitalize">
    <tr>
      <th>Subject</th>
      <th>Name</th>
      <th>Duration</th>
      <th>User</th>
    </tr>
    <tr style="display:none;">
      <th colspan="4"></th>
    </tr>
  </thead>
  <tbody>
    <tr data-toggle="collapse" data-target="#demo1">
      <td>Subject1</td>
      <td>Name1</td>
      <td>Duration1</td>
      <td>User1</td>
    </tr>
    <tr id="demo1">
      <td colspan="4">aaa</td>
      <td style="display: none;"></td>
      <td style="display: none;"></td>
      <td style="display: none;"></td>
    </tr>
    <tr data-toggle="collapse" data-target="#demo2">
      <td>Subject2</td>
      <td>Name2</td>
      <td>Duration2</td>
      <td>User2</td>
    </tr>
    <tr id="demo2">
      <td colspan="4">aa2</td>
      <td style="display: none;"></td>
      <td style="display: none;"></td>
      <td style="display: none;"></td>
    </tr>
  </tbody>
</table>

JS

$("#taskTable").dataTable({
  "ordering": false
});

Upvotes: 1

Related Questions