调用另一个脚本后显示的Jquery datatable隐藏列。如何永久隐藏指定的列?

调用另一个脚本后显示的Jquery datatable隐藏列。如何永久隐藏指定的列?,jquery,jquery-datatables,Jquery,Jquery Datatables,我的桌子是空的 <table id="EmployeesTable" style="width: 100%;" class="grid-table06 border-one"> <thead> <tr> <th width="80%" align="left" valign="middle">Name</th> <th width="20%" align="l

我的桌子是空的

<table id="EmployeesTable" style="width: 100%;" class="grid-table06 border-one">
    <thead>
        <tr>
            <th width="80%" align="left" valign="middle">Name</th>
            <th width="20%" align="left" valign="middle">Department</th>
            <th>Id</th>
        </tr>
    </thead>
</table>

名称
部门
身份证件
我的剧本如下

$(function () {
    $(".switchDate").click(function () {
        var id = $(this).attr("rel");
        fetchEmployeedetails(id);
    });

    fetchEmployeedetails(@model.Id); //on load

    function fetchEmployeedetails(id) {
        $("#EmployeesTable").dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "/Employees/FetchDetails?Deptid=" + id + "&thresholdLow=4&threshold=100",
            "sPaginationType": "full_numbers",
            "bDestroy": true,
            "aaSorting": [[1, 'desc']],
            "asStripClasses": ['color01', 'color03'],

            "aoColumnDefs": [{
                "aTargets": [2],
                "bVisible": false
            }, {
                "aTargets": [1],
                "fnRender": function (oObj) {
                    return "<a href='#showemployees' rel='" + oObj.aData[2] + "'></a>";
                }
            }]
        });
    }
});
$(函数(){
$(“.switchDate”)。单击(函数(){
var id=$(this.attr(“rel”);
获取员工详细信息(id);
});
fetchEmployeedetails(@model.Id);//正在加载
函数fetchEmployeedetails(id){
$(“#EmployeesTable”).dataTable({
“bProcessing”:正确,
“bServerSide”:正确,
“sAjaxSource”:“/Employees/FetchDetails?Deptid=“+id+”&thresholdLow=4&threshold=100”,
“sPaginationType”:“完整编号”,
是的,
“aaSorting”:[[1,'说明]],
“asStripClasses”:['color01','color03'],
“aoColumnDefs”:[{
“目标”:[2],
“可见”:假
}, {
“目标”:[1],
“fnRender”:函数(oObj){
返回“”;
}
}]
});
}
});
加载时,它可以正常工作,不显示隐藏的“Id”列,但如果我通过switchDate On click函数选择Id,它会使隐藏列在第二秒内可见


如何永久隐藏列?

数据表(
.dataTable(…)
)的初始化只应在页面加载事件之后发生一次。从那时起,
.fnDraw()
负责更新它

发件人:

对于那些使用服务器端处理的人来说,注意:许多API函数假定数据存储是在客户端而不是服务器端完成的。因此,fnAddData和fnDeleteRow等函数不会影响数据库中保存的数据。事实上,DataTables甚至不知道您是否正在使用数据库!因此,您必须对服务器进行必要的调用,以便根据需要操作数据,然后只需重新绘制表(fnDraw)即可查看新数据

请在此处阅读更多信息:

因此,您必须按如下方式更改代码:

$(function () {

   dataTableInitialisation();

   $(".switchDate").click(function () {
       var ajaxUrl = "/Employees/FetchDetails?Deptid=" + $(this).attr("rel") + "&thresholdLow=4&threshold=100";
       fetchEmployeedetails(ajaxUrl);
   });
});

fetchEmployeedetails(ajaxSource){
    var oSettings = myOTable.fnSettings();
    oSettings.sAjaxSource = ajaxSource;
    myOTable.fnDraw();
} 

function dataTableInitialisation() {
    myOTable = $("#EmployeesTable").dataTable({
          "bProcessing": true,
          "bServerSide": true,
          "sAjaxSource": "/Employees/FetchDetails?Deptid=" + @model.Id + "&thresholdLow=4&threshold=100",
          "sPaginationType": "full_numbers",
          "bDestroy": true,
          "aaSorting": [[1, 'desc']],
          "asStripClasses": ['color01', 'color03'],
          "aoColumnDefs": [{
                  "aTargets": [2],
                  "bVisible": false
                  }, {
                  "aTargets": [1],
                  "fnRender": function (oObj) {
                  return "<a href='#showemployees' rel='" + oObj.aData[2] + "'></a>";
                  }
           }]
       });
   }
});
$(函数(){
DataTableInitialization();
$(“.switchDate”)。单击(函数(){
var ajaxUrl=“/Employees/FetchDetails?Deptid=“+$(this).attr(“rel”)+”&thresholdLow=4&threshold=100”;
获取员工详细信息(ajaxUrl);
});
});
获取员工详细信息(ajaxSource){
var oSettings=myOTable.fnSettings();
oSettings.sAjaxSource=ajaxSource;
myOTable.fnDraw();
} 
函数DataTableInitialization(){
MyTable=$(“#EmployeesTable”).dataTable({
“bProcessing”:正确,
“bServerSide”:正确,
“sAjaxSource”:“/Employees/FetchDetails?Deptid=“++@model.Id+”&thresholdLow=4&threshold=100”,
“sPaginationType”:“完整编号”,
是的,
“aaSorting”:[[1,'说明]],
“asStripClasses”:['color01','color03'],
“aoColumnDefs”:[{
“目标”:[2],
“可见”:假
}, {
“目标”:[1],
“fnRender”:函数(oObj){
返回“”;
}
}]
});
}
});
顺便说一下,我建议使用
aoData.push()
向服务器发送更多数据,而不是更改
sAjaxSource
。以下是有关向服务器发送额外数据的详细信息:
看看这个例子:


然后调用
dnShowHide(columnPosition)

hi现在还显示隐藏列的秒数switchdate@SreenathPlakkat-我无法理解您的问题,请详细创建一个新问题,并给我阅读该问题的链接。
function fnShowHide( iCol ) {
    /* Get the DataTables object again - this is not a recreation, just a get of the object */
    var oTable = $('#EmployeesTable').dataTable();

    var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
    oTable.fnSetColumnVis( iCol, bVis ? false : true );
}