I am currently pulling data in a jQuery datatable which is grand! But I am having an issue with the Role column.
I am trying to inset a select list in that column which will have two options "admin" & "User" and whatever data is in the array set that value within the select list.
But I haven't been able to render select list into the table then set it to admin or user based on whats in the array.
jQuery(function($) {
var data = [
["test1", "admin", "yes"],
["test2", "admin", "yes"],
["test3", "user", "no"],
["test4", "user", "no"]
]
function authusers() {
t = $('#authTable').DataTable({
retrieve: true,
paging: false,
data: data,
columns: [{
"title": "Email",
"render": function(data, type, row, meta) {
return row[0];
}
}, {
"title": "Role",
"render": function(data, type, row, meta) {
return row[1];
}
}, {
"title": "Active",
"render": function(data, type, row, meta) {
var checkbox = $("<input/>", {
"type": "checkbox"
});
checkbox.attr("checked", (row[2] === "yes"));
checkbox.addClass((row[2] === "yes") ? "checkbox_checked" : "checkbox_unchecked")
return checkbox.prop("outerHTML")
}
}, ],
order: []
});
}
//Passes value and if it was enabled or disabled value to serverside.
$(document).on('click', '.checkbox_checked , .checkbox_unchecked', function() {
if ($(this).is(':checked')) {
var rowIndex = $(this).closest('tr').find('td:eq(0)').text();
var status = 'yes';
} else if ($(this).prop('checked', false)) {
var rowIndex = $(this).closest('tr').find('td:eq(0)').text();
var status = 'no';
}
});
authusers();
});
<link href="//cdn.datatables.net/1.10.11/css/jQuery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<body>
<table class="display" id="authTable" width="60%">
<thead>
<tr>
<th>Email</th>
<th>Active</th>
<th>Role</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
And also a JSfiddle
Any advice or recommendations are welcome!