Possible bug using ColVis and stateSave (with save and load Ajax call) together
Possible bug using ColVis and stateSave (with save and load Ajax call) together
Hello DataTables,
I'm seriously impressed with DataTables and all of it's options.
Never have I seen a library on datatables so complete and extendable.
Loving it!
The reasing I'm posting is because i'm running into a problem that I can't solve, despite hours of debugging.
I'm using the ColVis extension and the saveState stateSave
functionality.
If the saveState functionality uses the local storage (HTML5) the ColVis extension works perfectly without problems.
But when I use the stateSaveCallback stateSaveCallback
and stateLoadCallback stateLoadCallback
to save and load the state server-side into the database something weird happens with the ColVis extension.
The state is loaded and saved correctly into the database with Ajax calls but if a make a column visible again after hiding it it will position the header th correctly but will position the corresponding td of each row at the end of the row in stead of the correct position.
Also the saved visibility of a column is not restored when using the saveState functionality.
I've tried using the latest stable versions and the latest nightly builds, but nothing seems to work.
Can you please help me?
<script language="javascript">
$(document).ready(function() {
var owoningbeheerwoningendatatable = $('#woningbeheerwoningendatatable').DataTable( {
'dom': "RC<'clear'><'row'<'col-xs-12 col-sm-6 col-md-6 col-lg-6'Tl>r>t<'row'<'col-xs-3 col-sm-4 col-md-5'i><'col-xs-9 col-sm-8 col-md-7 text-right'p>>",
'processing': true,
'serverSide': true,
'responsive': true,
'stateSave': true,
'ajax': {
'url': '/woningbeheer/datatables/woningenjson'
},
'lengthMenu': [ [ 10, 25, 50, 75, 100, 250, 500, -1 ], [ 10, 25, 50, 75, 100, 250, 500, 'Alles' ] ],
'order': [ [ 1, 'asc' ] ],
'colVis': {
'buttonText': 'Toon / verberg kolommen',
'overlayFade': 0,
'exclude': [ 0 ]
},
'stateSaveCallback': function (settings, data) {
$.ajax({
'url': '/datatables/state/save',
'data': { 'identifier': 'woningbeheerwoningendatatable', 'data': data },
'dataType': 'json',
'method': 'POST'
});
},
'stateLoadCallback': function (settings) {
var o;
$.ajax({
'url': '/datatables/state/load',
'data': { 'identifier': 'woningbeheerwoningendatatable' },
'async': false,
'dataType': 'json',
'method': 'POST',
'success': function (json) {
o = json;
}
});
return o;
},
'stateSaveParams': function (settings, data) {
for (var i = 0; i < data.columns.length; i++) {
data.columns[i].search.search = '';
}
data.search.search = '';
},
'tableTools': {
'sSwfPath': '/metronics/global/swf/copy_cvs_xls_pdf.swf',
'aButtons': []
},
'columns': [
{
'name': 'complexnummer',
'title': 'Complex',
'className': 'string complexnummer tooltips',
'tooltip': 'Het complexnummer van desbetreffende woning.',
'type': 'string',
'orderable': true,
'searchable': true,
'visible': true,
'data': {
'select': 'complex.complexnummer',
'_': 'complexnummer',
'filter': 'complexnummer',
'display': 'complexnummer'
},
},
{
'name': 'vhe_nummer',
'title': 'VHE',
'className': 'string vhe_nummer tooltips',
'tooltip': 'Het VHE nummer van desbetreffende woning.',
'type': 'string',
'orderable': true,
'searchable': true,
'visible': false,
'data': {
'select': 'woning.vhe_nummer',
'_': 'vhe_nummer',
'filter': 'vhe_nummer',
'display': 'vhe_nummer'
},
},
{
'name': 'straat',
'title': 'Straat',
'className': 'string straat tooltips',
'tooltip': 'De straatnaam van de woning.',
'type': 'string',
'orderable': true,
'searchable': true,
'visible': true,
'data': {
'select': 'woning.straat',
'_': 'straat',
'filter': 'straat',
'display': 'straat'
},
},
{
'name': 'huisnummer',
'title': 'Huisnr',
'className': 'integer huisnummer tooltips',
'tooltip': 'Het huisnummer van desbetreffende woning.',
'type': 'integer',
'orderable': true,
'searchable': true,
'visible': true,
'data': {
'select': 'woning.huisnummer',
'_': 'huisnummer',
'filter': 'huisnummer',
'display': 'huisnummer'
},
},
{
'name': 'toevoeging',
'title': 'Toev',
'className': 'string toevoeging tooltips',
'tooltip': 'De toevoeging aan het huisnummer van het adres van desbetreffende woning.',
'type': 'string',
'orderable': true,
'searchable': true,
'visible': true,
'data': {
'select': 'woning.toevoeging',
'_': 'toevoeging',
'filter': 'toevoeging',
'display': 'toevoeging'
},
},
{
'name': 'postcode',
'title': 'Postcode',
'className': 'string postcode tooltips',
'tooltip': 'De postcode van het adres.',
'type': 'string',
'orderable': true,
'searchable': true,
'visible': true,
'data': {
'select': 'woning.postcode',
'_': 'postcode',
'filter': 'postcode',
'display': 'postcode'
},
}
]
});
});
</script>
Replies
The problem isn't that ColVis doesn't work with state saving, but rather that Responsive is being used (
'responsive': true,
).Responsive controls the column visibility, so it is inherently incompatible with ColVis which also attempts to control the column visibility (I'm going to add a note to the documentation to highlight this as it has come up a few times recently, or possibly a warning in the code).
Basically there becomes a bit of a race condition between ColVis and Responsive, and only one of them will win. Usually Responsive...
Allan
Allen,
Thanks for the quick response.
I've removed the
responsive
option, but the problem is still there.When I remove the saved state from the database so the first load state gets an empty response the problem is gone. But when a saved state is returned after a reload the problem returns.
Below the php json_encode string from my database.
Could it be because the integers and boolean variables are converted to strings (as it seems) that the problem is caused?
If so, the problem is with the way I save and return the state json.
Never mind!
The problem was indeed that the values where casted to strings.
Many thanks though for the responsive removal tip.
Good to hear. Yes - the string
'true'
and'false'
will both be "true" in the loose checking that DataTables uses there. It need to retain he original Javascript data type.Allan
Hi!
In advance sorry for my poor english (i promise that write java better than english). And great work in the page!!
I´m trying to integrate a full datatable with some extensions, like colVis and responsive, in a bootstrap development .
As you mention colVis and responsive appear to be incompatible; so i try to solve it modifying the source (version 1.10.4):
I´m testing yet, but it works for the moment. I hope it serves!!
Bye!!
ColVis and Responsive both attempt to control column visibility state. Therefore without hacks they simply cannot operate together.
Allan