Assign a column value to a variable using a Delete button, pass that value to servlet.
Assign a column value to a variable using a Delete button, pass that value to servlet.
I need to assign a userID to a variable and then pass that to a servlet to delete that user from the database using mySQL. However, this specific issue is that I CANNOT assign a userID to a variable, I am getting an "undefined" in the alert.
Objective: Assign userID to variable in either JS, or jQuery when the "DELETE" is clicked. Pass that userID to servlet. I do realize I am only providing script for the datatable and the jQuery function.
<!-- JS Follows -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Bootstrap -->
<script
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- DataTables (Generate Tables based on Json) -->
<!-- JQuery -->
<script
src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<!-- DATATABLE -->
<script
src="https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"></script>
<!-- RESPONSIVE -->
<script
src="https://cdn.datatables.net/responsive/2.1.0/js/dataTables.responsive.min.js"></script>
<!-- Custom code -->
<script>
$(document).ready(function() {
var table = $('#table_users').DataTable( {
"ajax": "${pageContext.request.contextPath}/json/table/users",
"display": 'envelope',
"data": null,
"columns" : [
//ensure that fields match
//from column mapping in servlet
{
className: "center",
defaultContent: '<button id="btn_editmodal" class = "btn btn-primary">edit</button></td> <button id="btn_deletemodal" class = "btn btn-primary">delete</button></td>'
},
{"data" : "id"},
{"data" : "username"}
]
} );
$('#table_users tbody').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data( );
alert(data[1]);
} );
});
</script>
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
data[1]
will beundefined
since your data for each row appears to be an object rather than an array (based on the fact that you are usingcolumns.data
). You should usedata.id
for example to access theid
property of the data source object.Allan