I am a total noob when it comes to web design and would like to create a datatable with a fixed (frozen) left two columns. I am reading in my data successfully from a JSON file and the information is displayed successfully. However, when I try to set the columns to fixed, it just doesn’t happen.
Below is a test example from my html file. My input into JSFiddle works just fine, but the fixed columns are not rendering at all on my website.
I am using Hugo as my framework for my website, so if information from other files is needed, please let me know. The code snippet below is from a partial that gets called.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixed Columns Example</title>
<link href="https://cdn.datatables.net/2.1.0/css/dataTables.dataTables.css" rel="stylesheet">
<link href="https://cdn.datatables.net/v/dt/dt-2.1.0/fc-5.0.1/datatables.min.css" rel="stylesheet">
<style>
table {
width: 20%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<table id="example" class="stripe row-border order-column" style="width:100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th>Extn.</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger</td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011-04-25</td>
<td>$320,800</td>
<td>5421</td>
<td>[email protected]</td>
</tr>
<tr>
<td>Garrett</td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011-07-25</td>
<td>$170,750</td>
<td>8422</td>
<td>[email protected]</td>
</tr>
<tr>
<td>Ashton</td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009-01-12</td>
<td>$86,000</td>
<td>1562</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
<!-- jQuery first -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<!-- DataTables JS -->
<script src="https://cdn.datatables.net/v/dt/dt-2.1.0/datatables.min.js"></script>
<!-- FixedColumns JS -->
<script src="https://cdn.datatables.net/v/dt/dt-2.1.0/fc-5.0.1/datatables.min.js"></script>
<script>
$(document).ready(function() {
// Initialize DataTable with FixedColumns
$('#example').DataTable({
scrollX: true,
scrollY: '400px',
scrollCollapse: true,
paging: false,
fixedColumns: {
left: 1 // Number of columns to fix
}
});
});
</script>
</body>
</html>
skolves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.