<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<link href="https://nightly.datatables.net/buttons/css/buttons.dataTables.css?_=c6b24f8a56e04fcee6105a02f4027462.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/buttons/js/dataTables.buttons.js?_=c6b24f8a56e04fcee6105a02f4027462"></script>
<link href="https://nightly.datatables.net/select/css/select.dataTables.css?_=9a6592f8d74f8f520ff7b22342fa1183.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/select/js/dataTables.select.js?_=9a6592f8d74f8f520ff7b22342fa1183"></script>
<link href="https://editor.datatables.net/extensions/Editor/css/editor.dataTables.min.css" rel="stylesheet" type="text/css" />
<script src="https://editor.datatables.net/extensions/Editor/js/dataTables.editor.min.js"></script>
<meta charset=utf-8 />
<title>DataTables - JS Bin</title>
</head>
<body>
<div class="container">
<button id="trigger">Show All via Trigger</button>
<table id="sites" class="display nowrap" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
body {
font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
color: #333;
background-color: #fff;
}
td.details-control {
background: url('https://www.datatables.net/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('https://www.datatables.net/examples/resources/details_close.png') no-repeat center center;
}
/*
* Based on the code here:
* https://datatables.net/blog/2019-01-11
*/
var showAllFlag = false;
function showAll() {
showAllFlag = ! showAllFlag;
$("#sites").DataTable().draw(false);
}
function createChild_2(row) {
var rowData = row.data();
// This is the table we'll convert into a DataTable
var table = $('<table class="display level_2" width="100%"/>');
// Display it the child row
row.child(table).show();
table.parent().css('background', '#7ac470');
// Editor definition for the child table
var usersEditor_2 = new $.fn.dataTable.Editor({
idSrc: 'name',
table: table,
fields: [
{
label: "Name:",
name: "name"
},
{
label: "Position:",
name: "position"
},
{
label: "Office:",
name: "office"
},
{
label: "Salary:",
name: "salary",
}
]
});
// Child row DataTable configuration, always passes the parent row's id to server
var usersTable_2 = table.DataTable({
dom: "Bfrtip",
pageLength: 5,
rowId: 'name',
data: [rowData],
columns: [
{ title: "Name", data: "name" },
{ title: "Position", data: "position" },
{ title: "Office", data: "office" },
{ title: "Salary", data: "salary" }
],
select: true,
buttons: [
{ extend: "create", editor: usersEditor_2 },
{ extend: "edit", editor: usersEditor_2 },
{ extend: "remove", editor: usersEditor_2 }
]
});
}
function createChild(row) {
var rowData = row.data();
// This is the table we'll convert into a DataTable
var table = $('<table class="display level_1" width="100%"/>');
// Display it the child row
row.child(table).show();
table.parent().css('background', '#90b2e8');
// Editor definition for the child table
var usersEditor = new $.fn.dataTable.Editor({
idSrc: 'name',
table: table,
fields: [
{
label: "Name:",
name: "name"
},
{
label: "Extension:",
name: "extn"
},
{
label: "Start Date:",
name: "start_date"
},
{
label: "Salary:",
name: "salary",
}
]
});
// Child row DataTable configuration, always passes the parent row's id to server
var usersTable = table
.on('draw.dt', function (e) {
e.stopPropagation();
})
.DataTable({
dom: "Bfrtip",
pageLength: 5,
rowId: 'name',
data: [rowData],
columns: [
{
className: "details-control",
orderable: false,
data: null,
defaultContent: "",
width: "10%"
},
{ title: "First name", data: "name" },
{ title: "Last name", data: "extn" },
{ title: "Phone #", data: "start_date" },
{ title: "Location", data: "salary" }
],
select: true,
buttons: [
{ extend: "create", editor: usersEditor },
{ extend: "edit", editor: usersEditor },
{ extend: "remove", editor: usersEditor }
]
});
// Add event listener for opening and closing details
table.on("click", "td.details-control", function(e) {
e.stopPropagation(); // Make sure the Parent event doesn't execute
var tr = $(this).closest("tr");
var row = usersTable.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
destroyChild(row);
tr.removeClass("shown");
} else {
// Open this row
createChild_2(row);
tr.addClass("shown");
}
});
}
function updateChild(row) {
$("table", row.child())
.DataTable()
.ajax.reload();
}
function destroyChild(row) {
// Remove and destroy the DataTable in the child row
var table = $("table", row.child());
table.detach();
table.DataTable().destroy();
// And then hide the row
row.child.hide();
}
$(document).ready(function() {
var siteEditor = new $.fn.dataTable.Editor({
table: "#sites",
idSrc: 'name',
fields: [
{
label: "Site name:",
name: "name"
},
{
label: "Site position:",
name: "position"
}
]
});
var siteTable = $("#sites").DataTable({
dom: "Bfrtip",
ajax: "/ajax/objects.txt",
rowId: 'name',
order: [1, "asc"],
columns: [
{
className: "details-control",
orderable: false,
data: null,
defaultContent: "",
width: "10%"
},
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
],
select: {
style: "os",
selector: "td:not(:first-child)"
},
buttons: [
{ extend: "create", editor: siteEditor },
{ extend: "edit", editor: siteEditor },
{ extend: "remove", editor: siteEditor }
]
});
// Add event listener for opening and closing details
$("#sites tbody").on("click", "td.details-control", function() {
var tr = $(this).closest("tr");
var row = siteTable.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
destroyChild(row);
tr.removeClass("shown");
} else {
// Open this row
createChild(row);
tr.addClass("shown");
}
});
// When updating a site label, we want to update the child table's site labels as well
siteEditor.on("submitSuccess", function() {
siteTable.rows().every(function() {
if (this.child.isShown()) {
updateChild(this);
}
});
});
siteTable.on('draw', function () {
if ( showAllFlag ) {
$('#sites td.details-control').trigger('click');
setTimeout(function() {
$('.level_1 td.details-control').trigger('click');
// Level 2 is not part of this example
// setTimeout(function() {
// $('.level_2 td.details-control').trigger('click');
// }, 1);
}, 1);
}
});
$('#trigger').on('click', function () {
showAll();
});
});
You can jump to the latest bin by adding /latest
to your URL
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + [ | Indents selected lines |
ctrl + ] | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Clone Bin |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |