<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>
<script src="https://nightly.datatables.net/buttons/js/buttons.colVis.js?_=c6b24f8a56e04fcee6105a02f4027462"></script>
<meta charset=utf-8 />
<title>DataTables - JS Bin</title>
</head>
<body>
<div class="container">
<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Jennifer Acosta</td>
<td>Javascript Developer</td>
<td>New York</td>
<td>43</td>
<td>2013/02/01</td>
<td>$2,875</td>
</tr>
<tr>
<td>Cara Stevens</td>
<td>Sales Assistant</td>
<td>New.York</td>
<td>46</td>
<td>2011/12/06</td>
<td>$4,800</td>
</tr>
<tr>
<td>Hermione Butler</td>
<td>Director</td>
<td>New/York</td>
<td>47</td>
<td>2011/03/21</td>
<td>$4,080</td>
</tr>
<tr>
<td>Lael Greer</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>21</td>
<td>2009/02/27</td>
<td>$3,120</td>
</tr>
<tr>
<td>Donna Snider</td>
<td>System Architect</td>
<td>New-York</td>
<td>27</td>
<td>2011/01/25</td>
<td>$3,120</td>
</tr>
</tbody>
</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;
}
$(document).ready( function () {
function buildSelect( table ) {
$("thead tr:eq(1) td").each( function () {
$(this).empty();
});
table
.columns([1,2])
.every( function () {
var column = this;
var visIndex = column.index('visible');
console.log(visIndex)
if ( visIndex !== null ) {
console.log('Adding select')
var select = $('<select><option value=""></option></select>')
.appendTo( $("thead tr:eq(1) td").eq( visIndex ).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex($(this).val());
column
.search( val ? '^' + val + '$' : '', true, false ).draw();
});
table
.cells( null, column.index(), {search: 'applied'})
.render('filter')
.unique()
.sort()
.each( function ( d, j ) {
select.append( '<option value="' + d + '">' + d + '</option>' );
});
// The rebuild will clear the exisiting select, so it needs to be repopulated
var currSearch = column.search();
if ( currSearch ) {
console.log(currSearch.replace(/\\(.)/g, '$1'));
select.val( currSearch.substring(1, currSearch.length-1).replace(/\\(.)/g, '$1') );
}
}
});
}
var table = $('#example').DataTable({
/*
columns: [
{
name: "Name",
},
{
name: "Position",
},
{
name: "Office",
},
{
name: "Age",
},
{
name: "Start date",
},
{
name: "Salary",
}
], */
orderCellsTop: true,
dom: 'liBfrtp',
buttons: [
{
text: 'Toggle Columns',
extend: 'collection',
buttons: [
{
text: 'Name',
className: 'active',
extend: 'columnToggle',
columns: [0]
},
{
text: 'Position/Office',
className: 'active',
extend: 'columnToggle',
columns: [1, 2]
},
{
text: 'Age/Start Date',
className: 'active',
extend: 'columnToggle',
columns: [3, 4]
},
{
text: 'Position/Age',
className: 'active',
extend: 'columnToggle',
columns: [1, 3]
},
{
text: 'Office/Start Date',
className: 'active',
extend: 'columnToggle',
columns: [2, 4]
},
]
},
/* make all columns visible */
{
className: 'show-all-columns',
extend: 'columnVisibility',
text: 'Show all',
visibility: true
},
],
initComplete: function() {
buildSelect( this.api() );
},
});
table.on( 'draw', function () {
buildSelect( table );
} );
$('#example').on( 'column-visibility.dt', function ( e, settings, column, state ) {
table.columns(column).search('').draw();
table.columns([1,2])
.every( function () {
var idx = this.index('visible');
if (this.search() === '' && idx !== null) {
$('#example thead tr:eq(1) td:eq('+idx+') select').val('');
}
});
} );
} );
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. |