Thursday, 16 November 2023

Fix both first row and first column in a vertical header JQuery DataTable

I have implemented vertical header flow in Datatables here: https://jsfiddle.net/2unr54zc/

I have fixed the columns on horizontal scroll but I am finding it really hard to fix the first two rows when vertical scrolled because they are different TDs/THs in different TRs.

How can I achieve this? Please help.

Following is the code:

.dataTable {
  display: block;
  width: 100%;
}
.dataTable thead {
  display: block;
  position: relative;
}
.dataTable thead tr {
  display: flex;
}
.dataTable thead th {
  flex-grow: 1;
  position: absolute;
  top: 100%;
  z-index: 9;
  width: 140px !important;
  left: 0;
  height: 80px;
  box-sizing: border-box;
  color: #fff;
  border: unset;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  background-color: #65696b;
}
.dataTable thead th:nth-child(2) {
  top: calc(100% + 80px * 1);
}
.dataTable thead th:nth-child(3) {
  top: calc(100% + 80px * 2);
}
.dataTable thead th:nth-child(4) {
  top: calc(100% + 80px * 3);
}
.dataTable thead th:nth-child(5) {
  top: calc(100% + 80px * 4);
}
.dataTable thead th:nth-child(6) {
  top: calc(100% + 80px * 5);
}
.dataTable thead th:nth-child(7) {
  top: calc(100% + 80px * 6);
}
.dataTable thead th:nth-child(8) {
  top: calc(100% + 80px * 7);
}
.dataTable thead th:nth-child(9) {
  top: calc(100% + 80px * 8);
}
.dataTable thead th:nth-child(10) {
  top: calc(100% + 80px * 9);
}
.dataTable thead th:nth-child(11) {
  top: calc(100% + 80px * 10);
}

.dataTable tbody {
  display: flex;
  width: 100%;
  overflow-x: scroll;
  padding-left: 140px;
  min-height: calc(80px * 10 + 18px);
}
.dataTable tbody tr {
  display: block;
  box-sizing: border-box;
}
.dataTable tbody td {
  display: block;
  height: 80px;
  width: 100px;
  position: relative;
  box-sizing: border-box;
}

.dataTable tbody td:not(:last-child)::before {
  border-bottom: 1px solid #f2e7e7f5;
}
.dataTable thead th:first{
  position:fixed
}
.dataTable tbody td:first{
  position:fixed
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js"></script>
<script src="https://gyrocode.github.io/jquery-datatables-pageLoadMore/1.0.2/js/dataTables.pageLoadMore.min.js"></script>
<table id="example" class="display" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
      <th>Country</th>
      <th>Town</th>
      <th>School</th>
      <th>Degree</th>

    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
      <td>UK</td>
      <td>London</td>
      <td>Lorem</td>
      <td>Phd</td>

    </tr>


  </tbody>
</table>
<script>
$(function() {$('#example').DataTable();});
</script>


from Fix both first row and first column in a vertical header JQuery DataTable

No comments:

Post a Comment