drhwb470572 2015-07-23 18:45
浏览 81
已采纳

DataTables(jQuery的表插件)继续从导航离开并返回DataTable时离开

I have been using DataTables to make database data easy to search through for site administrators. I have been getting lots of requests for users to be able to continue where they left off in the DataTable when clicking on an entry and navigating back to the table. I know I could start from scratch and probably bring this functionality to the table with a mostly server-side solution, but I was hoping there was a somewhat simple way to capture the DataTables data that is on the client-side and serve it to my server-side application to be able to re-draw the datatable where they left off when they navigate back to it. The main reason I would like to stick with DataTables is that I am using it dynamically for nearly 50 different database tables, and I really dont want to have to implement an entirely different solution for all these tables. I would prefer to just utilize DataTables in such a way that I can dynamically add this functionality to Datatables for all 50 pages. Any helpful pointers would be much appreciated.

It may be necessary to note that I am using PHP on the server side. (More specifically Symfony2 framework).

  • 写回答

2条回答 默认 最新

  • doubeng3216 2015-07-23 19:27
    关注

    DataTables has some built-in functionality for handling this. The docs refer to this as "state saving".

    From the docs:

    DataTables has the option of being able to save the state of a table (its paging position, ordering state etc) so that is can be restored when the user reloads a page, or comes back to the page after visiting a sub-page.

    This is enabled using the stateSave option in the config object you pass into the DataTables method, like this:

    $(document).ready(function() {
        $('#example').dataTable( {
            stateSave: true
        } );
    } );
    

    You can also control the length of time it is saved using the stateDuration option.

    Please be aware of this warning in the docs:

    The built in state saving method uses the HTML5 localStorage and sessionStorage APIs for efficient storage of the data. Please note that this means that the built in state saving option will not work with IE6/7 as these browsers do not support these APIs.

    DataTables provides the stateSaveCallback and stateLoadCallback options to work around this if you need to support those older browsers (e.g. by using cookies or server-side solutions).

    The full documentation can be found here: https://datatables.net/examples/basic_init/state_save.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面