From 624f7f6b2dbfbd1159ddf1d6c9b7eb9b2740a4f9 Mon Sep 17 00:00:00 2001 From: Ewald Vanderveken Date: Wed, 4 Dec 2019 15:07:19 +0100 Subject: [PATCH] Activate StateSave --- README.md | 2 +- js/ajax-datatables.js | 19 ++++++++++++++----- js/ajax-datatables.min.js | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a9ce5fb..a3684e1 100644 --- a/README.md +++ b/README.md @@ -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(){ diff --git a/js/ajax-datatables.js b/js/ajax-datatables.js index ffc9923..249a1ab 100644 --- a/js/ajax-datatables.js +++ b/js/ajax-datatables.js @@ -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", @@ -43,6 +45,7 @@ function initAjaxDatatable(path, sorting, filters){ "sLast": "Laatste" } }, + "bStateSave": true, "processing": true, "serverSide": true, "orderMulti": false, @@ -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)) + } }); } diff --git a/js/ajax-datatables.min.js b/js/ajax-datatables.min.js index 4c8f157..d66a449 100644 --- a/js/ajax-datatables.min.js +++ b/js/ajax-datatables.min.js @@ -7,4 +7,4 @@ * file that was distributed with this source code. */ -function ajaxDatatable(e,t,a){var e=e||"",t=t||[],a=a||{};void 0===oDatatable?initAjaxDatatable(e,t,a):oDatatable.ajax.reload()}function initAjaxDatatable(e,t,a){var o=$(".ajaxdatatable");oDatatable=o.DataTable({oLanguage:{sLengthMenu:"Toon _MENU_ entries",sZeroRecords:"Niets gevonden voor deze zoekopdracht",sInfo:"Toon _START_ tot _END_ van _TOTAL_ entries",sInfoEmpty:"Toon 0 tot 0 van 0 records",sInfoFiltered:"(gefilterd uit _MAX_ entries in totaal)",sSearch:"Zoeken",oPaginate:{sFirst:"Eerste",sPrevious:"« Vorige",sNext:"Volgende »",sLast:"Laatste"}},processing:!0,serverSide:!0,orderMulti:!1,ajax:{url:e,data:function(e){e.filters={};for(var t in a)e.filters[t]=a[t].val()}},columns:t,order:[[getDefaultSortField(o),getDefaultSortOrder(o)]]})}function getDefaultSortField(e){var t=0,a=e.find("th.default_sort");return a.index()>=0&&(t=a.index()),t}function getDefaultSortOrder(e){var t="asc",a=e.find("th.default_sort");return a.index()>=0&&a.hasClass("desc")&&(t="desc"),t}var oDatatable; \ No newline at end of file +var oDatatable;function ajaxDatatable(t,a,e,o){t=t||"",a=a||[],e=e||{},o=o||"";void 0===oDatatable?initAjaxDatatable(t,a,e,o):oDatatable.ajax.reload()}function initAjaxDatatable(t,a,e,o){var n=$(".ajaxdatatable");stateKey=window.location.pathname+o,oDatatable=n.DataTable({oLanguage:{sLengthMenu:"Toon _MENU_ entries",sZeroRecords:"Niets gevonden voor deze zoekopdracht",sInfo:"Toon _START_ tot _END_ van _TOTAL_ entries",sInfoEmpty:"Toon 0 tot 0 van 0 records",sInfoFiltered:"(gefilterd uit _MAX_ entries in totaal)",sSearch:"Zoeken",oPaginate:{sFirst:"Eerste",sPrevious:"« Vorige",sNext:"Volgende »",sLast:"Laatste"}},bStateSave:!0,processing:!0,serverSide:!0,orderMulti:!1,ajax:{url:t,data:function(t){for(var a in t.filters={},e)t.filters[a]=e[a].val()}},columns:a,order:[[getDefaultSortField(n),getDefaultSortOrder(n)]],stateSaveCallback:function(t,a){localStorage.setItem("DataTables-"+t.sInstance+stateKey,JSON.stringify(a))},stateLoadCallback:function(t){return JSON.parse(localStorage.getItem("DataTables-"+t.sInstance+stateKey))}})}function getDefaultSortField(t){var a=0,e=t.find("th.default_sort");return e.index()>=0&&(a=e.index()),a}function getDefaultSortOrder(t){var a="asc",e=t.find("th.default_sort");return e.index()>=0&&e.hasClass("desc")&&(a="desc"),a} \ No newline at end of file