Angular 数据表 saveState 不工作
Angular Datatables saveState not working
当我单击数据表第 2 页的特定条目时,它会将我重定向到另一个页面,当我返回时,它会将我重定向到第 1 页。
这里的问题是我想保存状态并且我想从我被重定向的地方返回到页面(例如:2)。
.withOption('bStateSave',true)
.withOption('stateSaveCallback', function (aoData, oSettings) {
console.log('performing save of settings');
localStorage.setItem( 'DataTables_home', JSON.stringify(aoData)) ;
})
.withOption('stateLoadCallback', function (oSettings) {
console.log('performing load of settings');
return JSON.parse( localStorage.getItem('DataTables_home') );
});
错误:
Unexpected token o in JSON at position 1
at Object.parse (<anonymous>)
at jQuery.fn.init.<anonymous> (app.js:14430)
at _fnLoadState (jquery.dataTables.js:6308)
at HTMLTableElement.<anonymous> (jquery.dataTables.js:1221)
at Function.each (base.js:365)
at jQuery.fn.init.each (base.js:137)
at jQuery.fn.init.DataTable [as dataTable] (jquery.dataTables.js:869)
at jQuery.fn.init.$.fn.DataTable (jquery.dataTables.js:15105)
at Object.renderDataTable (angular-datatables.js:757)
at Object.hideLoadingAndRenderDataTable (angular-datatables.js:773)
Cannot read property 'parentNode' of null
at _Api.<anonymous> (jquery.dataTables.js:9213)
at _Api.iterator (jquery.dataTables.js:6978)
at _Api.<anonymous> (jquery.dataTables.js:9212)
at _Api.destroy (jquery.dataTables.js:7141)
at jQuery.fn.init.DataTable.fnDestroy (jquery.dataTables.js:412)
at HTMLTableElement.<anonymous> (jquery.dataTables.js:923)
at Function.each (base.js:365)
at jQuery.fn.init.each (base.js:137)
at jQuery.fn.init.DataTable [as dataTable] (jquery.dataTables.js:869)
at jQuery.fn.init.$.fn.DataTable (jquery.dataTables.js:15105)
如果你能给我一个例子,那真的很有帮助。
谢谢。
我使用的版本
angular 版本:v1
jquery版本:2.2.4
数据表版本:1.10.12
angular-数据表版本:0.5.5
问题已使用以下代码解决:
<table id="example" datatable
dt-options="showCase.dtOptions"
dt-columns="showCase.dtColumns">
</table>
vm.dtOptions = DTOptionsBuilder.fromSource('data.json')
.withOption('stateSave', true)
.withOption('stateSaveCallback', function(settings,data) {
localStorage.setItem('DataTables_' + settings.sInstance, JSON.stringify(data));
})
.withOption('stateLoadCallback', function(settings) {
return JSON.parse(localStorage.getItem('DataTables_' + settings.sInstance ))
});
当我单击数据表第 2 页的特定条目时,它会将我重定向到另一个页面,当我返回时,它会将我重定向到第 1 页。 这里的问题是我想保存状态并且我想从我被重定向的地方返回到页面(例如:2)。
.withOption('bStateSave',true)
.withOption('stateSaveCallback', function (aoData, oSettings) {
console.log('performing save of settings');
localStorage.setItem( 'DataTables_home', JSON.stringify(aoData)) ;
})
.withOption('stateLoadCallback', function (oSettings) {
console.log('performing load of settings');
return JSON.parse( localStorage.getItem('DataTables_home') );
});
错误:
Unexpected token o in JSON at position 1
at Object.parse (<anonymous>)
at jQuery.fn.init.<anonymous> (app.js:14430)
at _fnLoadState (jquery.dataTables.js:6308)
at HTMLTableElement.<anonymous> (jquery.dataTables.js:1221)
at Function.each (base.js:365)
at jQuery.fn.init.each (base.js:137)
at jQuery.fn.init.DataTable [as dataTable] (jquery.dataTables.js:869)
at jQuery.fn.init.$.fn.DataTable (jquery.dataTables.js:15105)
at Object.renderDataTable (angular-datatables.js:757)
at Object.hideLoadingAndRenderDataTable (angular-datatables.js:773)
Cannot read property 'parentNode' of null
at _Api.<anonymous> (jquery.dataTables.js:9213)
at _Api.iterator (jquery.dataTables.js:6978)
at _Api.<anonymous> (jquery.dataTables.js:9212)
at _Api.destroy (jquery.dataTables.js:7141)
at jQuery.fn.init.DataTable.fnDestroy (jquery.dataTables.js:412)
at HTMLTableElement.<anonymous> (jquery.dataTables.js:923)
at Function.each (base.js:365)
at jQuery.fn.init.each (base.js:137)
at jQuery.fn.init.DataTable [as dataTable] (jquery.dataTables.js:869)
at jQuery.fn.init.$.fn.DataTable (jquery.dataTables.js:15105)
如果你能给我一个例子,那真的很有帮助。 谢谢。
我使用的版本 angular 版本:v1 jquery版本:2.2.4 数据表版本:1.10.12 angular-数据表版本:0.5.5
问题已使用以下代码解决:
<table id="example" datatable
dt-options="showCase.dtOptions"
dt-columns="showCase.dtColumns">
</table>
vm.dtOptions = DTOptionsBuilder.fromSource('data.json')
.withOption('stateSave', true)
.withOption('stateSaveCallback', function(settings,data) {
localStorage.setItem('DataTables_' + settings.sInstance, JSON.stringify(data));
})
.withOption('stateLoadCallback', function(settings) {
return JSON.parse(localStorage.getItem('DataTables_' + settings.sInstance ))
});