Skip to content

Commit

Permalink
Merge pull request #1 from eekes/master
Browse files Browse the repository at this point in the history
Activate StateSave
  • Loading branch information
vermeirentony authored Dec 5, 2019
2 parents fb7c61c + 624f7f6 commit f343a09
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 f343a09

Please sign in to comment.