javascript - How to dynamically enable/disable Responsive extension -
i have project users need able select whether or not accompanying script activates responsive extension of jquery datatables.
i want add dropdown menu in html lets users choose whether option responsive
set true
or false
in datatable()
initialization options.
i tried add separate function select value , used global variable datatable()
function couldn't work.
javascript:
$(document).ready(function(){ $("#example").datatable({ "responsive": false, "processing": false, "serverside": true, "ajax": "scripts/university.php", "columns": [ // id null, ........
*html**
<select id="selected2" onchange="myfunction()"> <option value="true">true</option> <option value="false">false</option> </select>
i tried adding document.getelementbyid
clause first line in datatable function couldn't make work.
what can add existing function make responsive
option user selectable list on html page?
solution
you need destroy table re-initialize , enable/disable responsive extension.
var dtoptions = { responsive: true }; var table = $('#example').datatable(dtoptions); $('#ctrl-select').on('change', function(){ dtoptions.responsive = ($(this).val() === "1") ? true : false; $('#example').datatable().destroy(); $('#example').removeclass('dtr-inline collapsed'); table = $('#example').datatable(dtoptions); });
notes
when table destroyed, responsive extension leaves classes (dtr-inline
, collapsed
), remove them manually before initializing table again.
also suggest having options in separate object dtoptions
make re-initialization easier, way need toggle 1 option responsive
.
demo
see this jsfiddle code , demonstration.
Comments
Post a Comment