javascript - Datatables initialization warning -
i have dropdown multiple options initializes datatable using javascript. works fine problem comes during execution in when option selected second time error , check out fiddle,
datatables warning (table id = 'defdiv'): cannot reinitialise datatable.
and below code :
if(user.position=="def"){ var table = $('#defdiv').datatable({ "aadata":defenders, "idisplaylength":15, "aocolumns": [ { "mdataprop": "playerinfo" }, { "mdataprop": "playername" }, { "mdataprop": "playerclub" }, { "mdataprop": "playervalue" }, { "mdataprop": "playerpoints" }, ], "order": [[ 3, "desc" ]], }); }
question how can prevent warning happening when option selected again ?
i've altered code , works now:
var goalkepeers = [{ "playername": "mignolet", "playerclub": "liverpool", "playervalue": "5.0", "playerpoints": "89", }, { "playername": "de gea", "playerclub": "manchester", "playervalue": "6.7", "playerpoints": "120", }]; var defenders = [{ "playername": "ivanovic", "playerclub": "chelsea", "playervalue": "7.8", "playerpoints": "100", }, { "playername": "mertesacker", "playerclub": "arsenal", "playervalue": "7.7", "playerpoints": "110", }]; var aocolumns = [{ "stitle": "name", "mdataprop": "playername" },{ "stitle": "club", "mdataprop": "playerclub" },{ "stitle": "value", "mdataprop": "playervalue" },{ "stitle": "points", "mdataprop": "playerpoints" }]; var table = null; $("#playersfilter").change(function () { var value = $('#playersfilter').val(); if (value == "gk") { if ($.fn.datatable.isdatatable('#players')) { $('#players').datatable().destroy(); } table = $('#players').datatable({ "aadata": goalkepeers, "idisplaylength": 15, "aocolumns": aocolumns, "bretrieve": true, "order": [ [3, "desc"] ], "bdestroy": true }); } else if (value == "def") { if ($.fn.datatable.isdatatable('#players')) { $('#players').datatable().destroy(); } table = $('#players').datatable({ "aadata": defenders, "idisplaylength": 15, "aocolumns": aocolumns, "bretrieve": true, "order": [ [3, "desc"] ], "bdestroy": true, }); } });
i'm pretty sure there's more efficient way of doing it, i'll have think , post link jsfiddle comment.
you need 1 table well:
<select id="playersfilter"> <option>choose players</option> <option value="gk">goalkepeers</option> <option value="def">defenders</option> </select> <table class="display" id="players"></table>
Comments
Post a Comment