jQuery DataTables: Responsive extension and custom controls (Alternative solution)
Example for article at https://www.gyrocode.com/articles/jquery-datatables-responsive-extension-and-custom-controls/
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">
<link rel="stylesheet" href="https://cdn.datatables.net/s/dt/dt-1.10.10,r-2.0.0/datatables.min.css">
<script src="https://cdn.datatables.net/s/dt/dt-1.10.10,r-2.0.0/datatables.min.js"></script>
<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() {
var table = $('#example').DataTable({
columnDefs: [
{ targets: 4, width: '100px' }
],
createdRow: function(row, data, dataIndex){
// Initialize custom control
initRating(row);
},
responsive: true
});
table.on('responsive-display', function ( e, api, row, showHide, update ) {
// If row details are being shown
if(showHide){
var $row_details = $(row.node()).next();
initRating($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',
score: function(){
return $(this).data('score');
},
click: function(score){
$(this).data('score', score);
}
});
}