DataTables
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/bs-3.3.6/dt-1.10.12/datatables.min.css">
<script src="https://cdn.datatables.net/v/bs-3.3.6/dt-1.10.12/datatables.min.js"></script>
<div class="container">
<table id="example" class="table table-bordered table-condensed table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th></th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>London</td>
<td>41</td>
<td>2012/10/13</td>
<td>$132,000</td>
</tr>
<tr>
<td>Dai Rios</td>
<td>Personnel Lead</td>
<td>Edinburgh</td>
<td>35</td>
<td>2012/09/26</td>
<td>$217,500</td>
</tr>
<tr>
<td>Jenette Caldwell</td>
<td>Development Lead</td>
...
JavaScript
$(document).ready(function (){
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input class="form-control" type="text" placeholder="Search '+title+'" />' );
} );
var table = $('#example').DataTable({
'ajax': 'https://api.myjson.com/bins/qgcu',
'columnDefs': [
{
'targets': 6,
'data': null,
'searchable': false,
'orderable': false,
'defaultContent': '<button type="button" class="btn btn-primary btn-details">Edit</button>'
}
]
});
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
$('#example').on('click', '.btn-details', function(){
showModalDialog(this);
});
$('#dlg-details').modal({ show: false });
$('#dlg-details').on('show.bs.modal', function (e){
var $dlg = $(this);
var $tr = $($dlg.data('btn')).closest('tr');
var $table = $($dlg.data('btn')).closest('table');
var data = $table.DataTable().row($tr).data();
var html =
'<form>' +
'<div class="form-group">' +
'<label>Name: </label><input class="form-control" type="text" value="' + $('<div/>').text(data[0]).html() + '">' +
'</div>' +
'</form>';
$('h5', $dlg).html(data[0]);
$('.modal-body', $dlg).html(html);
});
});
function showModalDialog(elBtn){
$('#dlg-details').data('btn', elBtn);
$('#dlg-details').modal('show');
}