0

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>

1 Answer 1

1

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

it works but if this is not right approach, what would you recommend?
@Guru If you sort or search, then you might not going to get header first and it's child beneath it. I have also updated the answer.

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.