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条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失