简体   繁体   English

如何隐藏或显示使用JavaScript中的数据表创建的表的列?

[英]How to hide or show column of table created using datatables in javascript?

Do you know how to hide or show column when datatable's source is javascript? 当数据表的源代码是javascript时,您知道如何隐藏或显示列吗?

Methods for showing or hiding columns 显示或隐藏列的方法

 table = $('#example').DataTable();
var col = table.column("0").visible(false);

work when data source is directly into html 当数据源直接进入html时工作

<table id="example" class="row-border hover">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
...          

But it does not work and launch an error when DataTable has a javascript source 但当DataTable具有javascript源时,它不起作用并启动错误

 var table = $('#example').dataTable({
                   "data": source,
                    "columns": columns,
                    "columnDefs": defs
    });


 var col = table.column("0").visible(false);//ERROR!

Do you know how to hide a column of Datatables with a javascript source please? 您知道如何用JavaScript源隐藏Datatables列吗?

Try something like this, 试试这个

var table = $('#example').dataTable({
  "data": source,
  "columns": columns,
  "columnDefs": [
  {
    "targets": [ 0 ],
    "visible": false,
  },
  "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull )     
  {
    $('td:eq(0)', nRow).hide();
  }
});

Updated. 更新。 Try to add fnRowCallback. 尝试添加fnRowCallback。 thanks! 谢谢!

I finally found another answere : it doesn't depends of html or json source but there is a difference between DataTable() which is the new version and dataTable() the old version 我终于找到了另一个答案:它不依赖于html或json源,但是新版本的DataTable()和旧版本的dataTable()之间有区别

column(n).visible(bool) 

works for DataTable() 适用于DataTable()

fnRowCallback 

works for dataTable() 适用于dataTable()

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

相关问题 如何在带有JavaScript源的JavaScript中动态隐藏或显示使用datatables创建的表的列? - How to hide or show dynamically column of table created using datatables in javascript with javascript source? 如何使用数据表在表格上显示更少的文本? - How to show less text on a table using datatables? 如何隐藏表列,但仍与Click事件一起使用? (jQuery数据表) - How To Hide A Table Column, But Still Use It With A Click Event? (JQuery Datatables) 如果没有数据,如何隐藏表格并使用jquery或javascript显示表单 - how to hide the table if there is no data and show the form using jquery or javascript 如何使用JavaScript显示/隐藏隐藏的HTML表行(没有jQuery) - How to show/hide hidden HTML table rows using JavaScript (no jQuery) 如何使用javascript再显示6行并隐藏表的前5行? - How to show 6 more rows and hide the previous 5 rows of a table using javascript? 如何为使用JavaScript创建的闪亮数据表调整一列的宽度? - How to adjust width of one column for shiny DataTables created with the JavaScript? 使用下拉菜单显示和隐藏表格中的列 - Show and hide column in a table using drop down 使用jQuery显示和隐藏表列 - Show and hide table column using jQuery 数据表:如何使用Javascript或任何函数对数据表中的列进行重新排序 - DataTables: how to reorder a column in datatables using Javascript or any function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM