I am using "jquery": "^3.4.0"
and DataTables 1.10.18
.
I am having the following table:
$(document).ready(() => {
var table = $('.datatable-responsive').DataTable();
});
$("#edit-row").click(() => {
var c = this.id
console.log(c)
});
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global stylesheets -->
<link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
<link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
<link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- /global stylesheets -->
<!-- Core JS files -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
<!-- /core JS files -->
<!-- Load plugin -->
<script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<!-- /load plugin -->
<!-- Theme JS files -->
<script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
<script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
</head>
<body class="navbar-top">
<!-- Page content -->
<div class="page-content pt-0">
<!-- Default ordering -->
<div class="card">
<div class="card-body">
<table class="table datatable-responsive dataTable" style="width:100%">
<thead>
<tr>
<th>#</th>
<th>Status</th>
<th>Title</th>
<th>Image</th>
<th>Profile</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>-2</td>
<td><span class="badge badge-success">Posted</span></td>
<td>Awesome Post</td>
<td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
<td>Joe</td>
<td class="text-center">
<div class="list-icons">
<div class="dropdown">
<a href="#" class="list-icons-item" data-toggle="dropdown">
<i class="icon-menu9"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
<a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>99</td>
<td><span class="badge badge-secondary">Queued</span></td>
<td>Cool Post</td>
<td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
<td>Brad</td>
<td class="text-center">
<div class="list-icons">
<div class="dropdown">
<a href="#" class="list-icons-item" data-toggle="dropdown">
<i class="icon-menu9"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
<a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>10</td>
<td><span class="badge badge-secondary">Queued</span></td>
<td>Awesome Post</td>
<td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
<td>Tom</td>
<td class="text-center">
<div class="list-icons">
<div class="dropdown">
<a href="#" class="list-icons-item" data-toggle="dropdown">
<i class="icon-menu9"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
<a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<!-- /default ordering -->
</div>
</div>
</div>
</body>
</html>
I am trying to put a click listener on the clicked edit button.
However, I currently only get undefined
back. Instead I would like to get the id within the first column back.
Any suggestions what I am doing wrong?
5
Answers
You are using arrow functions that will change the scope of
this
towindow
so to reference the scope of the element, easiest way is to avoid arrow functions for those handlers and simply use traditional functions:However, if you still want the arrow functions then pass a
event
parameter to the arrow function and get the element reference usingevent.target
.id
that you have in the first column you can find the closest table row then select the first table column to get that text value.#edit-row
asid
should be unique in the HTML page.The
this
in the click handler was pointing to the globalWindow
object because you were using arrow function, change it to a regular callback and it will work as expected.Check this answer for more information.
I just use your code as example and make some changes in jQuery.
Please try this, hope this works for you.
DataTables has own solution, you can use select extension to get selected row data, there is an example very useful for you
Considering your ultimate goal (editable DataTable), I’ll allow myself to suggest few improvements to your app, so that you don’t really need to get id of clicked row and do all the HTML heavy lifting on your own.
ajax
optioncolumns.render
option to modify cell contents, to make it appear as a badge or drop-down menurow().remove()
for deletion), I’ll use simplest example (deletion of the row in the front-end), since you didn’t share much details as for whether you want to modify both back end and front end data or only the latterComplete working DEMO of your sample project can be found over here or you can examine that demo in your browser’s Dev Tools, using this link.
P.S. if, for some reason, you still decide to proceed with your approach, you may inquiry this post (which is pretty similar to yours) for a way of getting data model properties (‘id’, in particular) of clicked row