jQuery DataTables - Responsive extension and custom controls
Example for article at https://www.gyrocode.com/articles/jquery-datatables-checkboxes/
by gyrocode
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/raty/2.7.0/jquery.raty.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/raty/2.7.0/jquery.raty.min.css">
<script src="https://cdn.datatables.net/r/dt/dt-1.10.9,r-1.0.7/datatables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/r/dt/dt-1.10.9,r-1.0.7/datatables.min.css">
<h3><a href="https://www.gyrocode.com/articles/jquery-datatables-responsive-extension-and-custom-controls/">jQuery DataTables: Responsive extension and custom controls</a></h3>
<a href="https://www.gyrocode.com/articles/jquery-datatables-responsive-extension-and-custom-controls/">See full article on Gyrocode.com</a>
<hr><br>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Rating</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Rating</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td><span class="rating" data-score="4"></span></td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td><span class="rating" data-score="3"></span></td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td><span class="rating" data-score="1"></span></td>
<td>$86,000</td>
</tr>
<tr>
...
JavaScript
$(document).ready(function() {https://jsfiddle.net/gyrocode/z8q8vLff/#
var table = $('#example').DataTable({
columnDefs: [
{ targets: 4, width: '100px' }
],
createdRow: function(row, data, dataIndex){
// Initialize custom control
initRating(row);
},
responsive: {
details: {
renderer: function(api, rowIdx, columns){
var $row_details = $.fn.DataTable.Responsive.defaults.details.renderer(api, rowIdx, columns);
// Initialize custom control
initRating($row_details);
return $row_details;
}
}
}
});
});
//
// Initializes jQuery Raty control
//
function initRating(container){
$('span.rating', container).raty({
half: true,
starHalf: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.7.0/images/star-half.png',
starOff: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.7.0/images/star-off.png',
starOn: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.7.0/images/star-on.png',
readOnly: true,
score: function() {
return $(this).attr('data-score');
}
});
}