简体   繁体   English

隐藏列时,DataTable重新加载到第一页

[英]DataTable reload to the first page when hidding column

I am using the jquery dataTable plug-in and I am trying to allow users to hide a column. 我正在使用jquery dataTable插件,并且试图允许用户隐藏列。

The code is working, but when I go to a page that is not the first page, and I hide a column, It goes back to the first page with the column hidden. 该代码可以正常工作,但是当我转到不是第一页的页面并隐藏一列时,它将返回到第一列,其中隐藏了该列。

What I would like is that it stays on the page that the user is on and hides the column. 我想要的是它停留在用户所在的页面上并隐藏该列。

    var oTable = $("#x").dataTable({
        responsive: true,
        "bFilter": true,
        "bPaginate": true,
        "order": [[5, 'desc']],
        "aoColumns": [
        {"bSortable": false },
        {"sClass": "text-center", "sWidth": "10%"},
        {"sClass": "text-center", "sWidth": "10%"},
        {"sClass": "text-center", "sWidth": "10%", "bSortable": false},
        {"sClass": "text-center", "sWidth": "10%", "bSortable": false},
        {"sClass": "text-center", "sWidth": "10%"}
        ]
    });
    $('.oculta-mostra').click(function(event) {
        event.preventDefault();
        $(this).children().first().toggleClass("label-success");
        $(this).children().first().toggleClass("label-danger");
        fnShowHide($(this).prop('id'));
    });
    function fnShowHide(iCol) {
        var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
        oTable.fnSetColumnVis( iCol, bVis ? false : true );
    }
    });

HTML 的HTML

        <a href="" id="1" class="oculta-mostra" style="text-decoration: none">
          <span class="label label-success">J</span>
        </a>
        <a href="" id="2" class="oculta-mostra" style="text-decoration: none">
          <span class="label label-success">V</span>
        </a>
        <a href="" id="3" class="oculta-mostra" style="text-decoration: none">
          <span class="label label-success">E</span>
        </a>
        <a href="" id="4" class="oculta-mostra" style="text-decoration: none">
          <span class="label label-success">D</span>
        </a>
   <table id="x" class="table table-bordered table-striped table-hover" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>nome</th>
            <th>J</th>
            <th>V</th>
            <th>E</th>
            <th>D</th>
            <th>P</th>
        </tr>
    </thead>
    <tbody>
        @foreach( $x as $u )
        <tr>
            <td>{{ $u->nome }}</td>
            <td>{{ $u->peladas }}</td>
            <td>{{ $u->vitorias }}</td>
            <td>{{ $u->empates }}</td>
            <td>{{ $u->derrotas }}</td>
            <td>{{ $u->pontuacao }}</td>
        @endforeach
        </tr>
    </tbody>

Your function to show and hide column, should return the current page: 您显示和隐藏列的函数应返回当前页面:

First, get the number of current page: 首先,获取当前页数:

var iPage = Math.ceil(oTable.fnSettings()._iDisplayStart / oTable.fnSettings()._iDisplayLength )

Second, set the page to table: 其次,将页面设置为表格:

oTable.fnPageChange(iPage);

Complete Function 功能齐全

function fnShowHide(iCol) {
    var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
    var iPage = Math.ceil(oTable.fnSettings()._iDisplayStart / oTable.fnSettings()._iDisplayLength )
    oTable.fnSetColumnVis( iCol, bVis ? false : true );
    oTable.fnPageChange(iPage);
}

Result: http://jsfiddle.net/cmedina/7kfmyw6x/104/ 结果: http : //jsfiddle.net/cmedina/7kfmyw6x/104/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM