I am using the DataTables library to create a responsive table. I want to achieve a behavior where all columns in the DataTable can toggle the collapse/expand state when clicked, except for the last column.
Here’s a simplified version of my current code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.7/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.7/js/jquery.dataTables.min.js"></script>
</head>
<body>
<table id="example" class="display responsive nowrap" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>30</td>
<td><button onclick="performAction()">Do Action</button></td>
</tr>
<tr>
<td>Jane Smith</td>
<td>25</td>
<td><button onclick="performAction()">Do Action</button></td>
</tr>
</tbody>
</table>
<script>
$(document).ready( function () {
var table = $('#example').DataTable();
});
function performAction() {
// Perform the desired action here
alert("Action performed!");
}
</script>
</body>
</html>
As of now, clicking on ONLY FIRST column in the DataTable toggles the collapse state,
I want all other columns "AGE" to also work as DataTable toggles the collapse state,
However, I want to exclude the last column from this behavior and make it perform a different action when clicked.
2
Answers
This works for me. By trigger the click on the first column when user click on other columns i get the toggle working as per my requirement.
We can use these library. I think your code may lack of some libraries, so it can’t work properly.
Hope it will help!!