Skip to content

Commit

Permalink
Activate StateSave
Browse files Browse the repository at this point in the history
  • Loading branch information
Ewald Vanderveken committed Dec 4, 2019
1 parent fb7c61c commit 624f7f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ The `filters` are optional, here you can pass searchable/filterable fields
};
{# on page load, init the datatable #}
ajaxDatatable(ajaxCallPath, sortable, filters);
ajaxDatatable(ajaxCallPath, sortable, filters, stateSaveAffix);
{# on submit, change, whenever you want #}
$(document).on("click", "#js-filter-submit", function(){
Expand Down
19 changes: 14 additions & 5 deletions js/ajax-datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
var oDatatable;

/* ajax datatable */
function ajaxDatatable(path, sorting, filters){
function ajaxDatatable(path, sorting, filters, stateSaveAffix){
var path = path || '';
var sorting = sorting || [];
var filters = filters || {};
var stateSaveAffix = stateSaveAffix || '';

if (oDatatable === undefined) {
/* first init */
initAjaxDatatable(path, sorting, filters)
initAjaxDatatable(path, sorting, filters, stateSaveAffix)
} else {
/* reload */
oDatatable.ajax.reload();
}
}

function initAjaxDatatable(path, sorting, filters){

function initAjaxDatatable(path, sorting, filters, stateSaveAffix){
var $ajaxDatatable = $('.ajaxdatatable');

stateKey = window.location.pathname + stateSaveAffix;

oDatatable = $ajaxDatatable.DataTable({
"oLanguage": {
"sLengthMenu": "Toon _MENU_ entries",
Expand All @@ -43,6 +45,7 @@ function initAjaxDatatable(path, sorting, filters){
"sLast": "Laatste"
}
},
"bStateSave": true,
"processing": true,
"serverSide": true,
"orderMulti": false,
Expand All @@ -56,7 +59,13 @@ function initAjaxDatatable(path, sorting, filters){
}
},
"columns": sorting,
"order": [[ getDefaultSortField($ajaxDatatable), getDefaultSortOrder($ajaxDatatable)]]
"order": [[ getDefaultSortField($ajaxDatatable), getDefaultSortOrder($ajaxDatatable)]],
"stateSaveCallback": function(settings, data) {
localStorage.setItem('DataTables-' + settings.sInstance + stateKey, JSON.stringify(data))
},
"stateLoadCallback": function(settings) {
return JSON.parse( localStorage.getItem('DataTables-' + settings.sInstance + stateKey))
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion js/ajax-datatables.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 624f7f6

Please sign in to comment.