后退按钮移动数据表的第n页

时间:2015-11-19 05:44:31

标签: jquery grails datatables

假设我们在数据表中有n个页面,此数据表的一列有链接通过发送新请求来打开新页面(仪表板)。

我们必须在这个新页面(仪表板)上添加后退按钮,这样就可以将我们移到最后打开的数据表页面。

我们应用了这样的按钮 -

<button style="postion:fixed;" class="pull-right" onclick="window.history.go(-1); ">Back</button>

但是这个按钮将我们发送到数据表的第一页而不是打开链接的第n页。             enter image description here

2 个答案:

答案 0 :(得分:1)

您可以将页码保存在第一个网址的哈希值中,如下所示:

<a href="secondPage.html" onClick="window.location.hash = encodeURIComponent(JSON.stringify({page: table.current_page_in_table}));">link</a>

然后在加载页面时检查您是否有哈希值,如果有,请阅读页码并更新表格以转到该页面:

<script type="text/javascript">
    function goToRightPage() {
        if (window.location.hash) {
            // Read the hash string omitting the # prefix
            var hashJson = window.location.hash.substring(1);
            // Restore the deserialized data to memory
            myData = JSON.parse(decodeURIComponent(hashJson));
            datatable.goToPage(myData.page);
         }
    }
    window.onload = goToRightPage;
</script>

答案 1 :(得分:0)

首先在localstorage中保存当前页面的位置,然后重定向到新页面 如果localstorage具有该变量并返回该表,并使用fnPageChange()打开该页面的数据表:

var table = $('#data_table').dataTable();
table.fnPageChange(localStorage.getItem('page'),true);