数据表搜索在刷新后转到第一页而不是当前页面

时间:2017-10-14 12:34:31

标签: javascript twitter-bootstrap

我正在使用bootstrap数据表作为我的记录。但当我在桌子的第二页时,如果我刷新页面,它会将我重定向到表格的第一页而不是第二页,任何想法如何解决这个问题。

$("#dataTablesFull, #dataTablesFull2, #dataTablesFull3, #dataTablesFull4").dataTable( {
                    "pageLength": <?php echo getConfigValue("table_records"); ?>,
                    "dom": '<"top"f>rt<"bottom"><"row dt-margin"<"col-md-6"i><"col-md-6"p><"col-md-12"B>><"clear">',
                    "buttons":  [ 'copy', 'csv', 'excel', 'pdf', 'print' ],
                    "oLanguage": {
                        "sSearch": "<i class='fa fa-search text-gray dTsearch'></i>",
                        "sEmptyTable": "<?php _e('No entries to show'); ?>",
                        "sZeroRecords": "<?php _e('Nothing found'); ?>",
                        "sInfo": "<?php _e('Showing'); ?> _START_ <?php _e('to'); ?> _END_ <?php _e('of'); ?> _TOTAL_ <?php _e('entries'); ?>",
                        "sInfoEmpty": "",
                        "oPaginate": {
                            "sNext": "<?php _e('Next'); ?>",
                            "sPrevious": "<?php _e('Previous'); ?>",
                            "sFirst": "<?php _e('First Page'); ?>",
                            "sLast": "<?php _e('Last Page'); ?>"
                        }
                    },
                    "columnDefs": [ { "orderable": false, "targets": -1 } ] }
                );
                $("#dataTablesFullNoOrder, #dataTablesFullNoOrder2").dataTable( {
                    "order": [],
                    "pageLength": <?php echo getConfigValue("table_records"); ?>,
                    "dom": '<"top"f>rt<"bottom"><"row dt-margin"<"col-md-6"i><"col-md-6"p><"col-md-12"B>><"clear">',
                    "buttons":  [ 'copy', 'csv', 'excel', 'pdf', 'print' ],
                    "oLanguage": {
                        "sSearch": "<i class='fa fa-search text-gray dTsearch'></i>",
                        "sEmptyTable": "<?php _e('No entries to show'); ?>",
                        "sZeroRecords": "<?php _e('Nothing found'); ?>",
                        "sInfo": "<?php _e('Showing'); ?> _START_ <?php _e('to'); ?> _END_ <?php _e('of'); ?> _TOTAL_ <?php _e('entries'); ?>",
                        "sInfoEmpty": "",
                        "oPaginate": {
                            "sNext": "<?php _e('Next'); ?>",
                            "sPrevious": "<?php _e('Previous'); ?>",
                            "sFirst": "<?php _e('First Page'); ?>",
                            "sLast": "<?php _e('Last Page'); ?>"
                        }
                    },
                    "columnDefs": [ { "orderable": false, "targets": -1 } ] }
                );
                $("#dataTablesFullDesc, #dataTablesFullDesc2").dataTable( {
                    "order": [[ 0, "desc" ]],
                    "pageLength": <?php echo getConfigValue("table_records"); ?>,
                    "dom": '<"top"f>rt<"bottom"><"row dt-margin"<"col-md-6"i><"col-md-6"p><"col-md-12"B>><"clear">',
                    "buttons":  [ 'copy', 'csv', 'excel', 'pdf', 'print' ],
                    "oLanguage": {
                        "sSearch": "<i class='fa fa-search text-gray dTsearch'></i>",
                        "sEmptyTable": "<?php _e('No entries to show'); ?>",
                        "sZeroRecords": "<?php _e('Nothing found'); ?>",
                        "sInfo": "<?php _e('Showing'); ?> _START_ <?php _e('to'); ?> _END_ <?php _e('of'); ?> _TOTAL_ <?php _e('entries'); ?>",
                        "sInfoEmpty": "",
                        "oPaginate": {
                            "sNext": "<?php _e('Next'); ?>",
                            "sPrevious": "<?php _e('Previous'); ?>",
                            "sFirst": "<?php _e('First Page'); ?>",
                            "sLast": "<?php _e('Last Page'); ?>"
                        }
                    },
                    "columnDefs": [ { "orderable": false, "targets": -1 } ] }
                );

                $("a[data-tab-destination]").on('click', function() {
                    var tab = $(this).attr('data-tab-destination');
                    $("#"+tab).click();
                });

2 个答案:

答案 0 :(得分:1)

您可能需要statesave功能。它保存表的状态,包括cookie中的分页,以便在返回页面或刷新页面时,它将保持其所处的状态。

datatable state save

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

答案 1 :(得分:0)

您可以做的一件事是将URL哈希设置为当前选项卡,然后在刷新时调用该选项卡的单击功能。 (如果您不喜欢在URL中使用哈希,您也可以使用类似会话的内容来执行此操作)。我喜欢哈希选项的是书签会起作用。

$("a[data-tab-destination]").on('click', function() {
   var tab = $(this).attr('data-tab-destination');
   window.location.hash = '#' + encodeURIComponent(tab);
   $("#"+tab).click();
});

if(window.loaction.hash) {
  var tag = decodeURIComponent(window.location.hash.substring(1));
  $("a[data-tab-destination]").click();
}