如何在页面加载时控制Datatable Colvis?

时间:2015-07-03 10:41:20

标签: jquery mysql datatable

<table class="table table-striped table-bordered table-hover table-checkable table-colvis datatable" id="example">
<thead>
<tr>
  <th style="background-color:#235671; color:white; font-size:12px;">Column1</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column2</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column3</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column4</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column5</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column6</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Column7</th>
  <th style="background-color:#235671; color:white; font-size:12px;">Action</th>
</tr>
</thead>
<tbody>

<?php
mysql_connect('localhost','****','');
mysql_select_db('****');
$query = mysql_query('SELECT * FROM Table_name');
while ($row = mysql_fetch_array($query)) {

echo '<tr>
    <td style="font-size:12px;"><input type="text" value="'.$row["col1"].'" class="form-control"/></td>
    <td style="font-size:12px;"><input type="text" value="'.$row["col2"].'"/></td>
    <td style="font-size:12px;">'.$row["col3"].'</td>
    <td style="font-size:12px;">'.$row["col4"].'</td>
    <td style="font-size:12px;">'.$row["col5"].'</td>
    <td style="font-size:12px;">'.$row["col6"].'</td>
    <td style="font-size:12px;">'.$row["col7"].'</td>
    <td style="font-size:12px; width:7%;"><i class="icon-upload-alt"</i>&nbsp;&nbsp;&nbsp;<i class="icon-pencil"</i>&nbsp;&nbsp;&nbsp;<i class="icon-trash"</i></td>
    </tr>';
  }
?>
</tbody>
</table>

这里我使用table-colvis来显示/隐藏DataTable的列。但是我需要在页面加载时隐藏一些列。所以我使用了以下代码

var table = $('#example').dataTable( {
            "columnDefs": [ 
            {
                "targets": [ 2 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 3 ],
                "visible": false
            }
        ]
        } );

它不起作用。我再次使用以下内容..但不幸的是,那也没有用。

$('#example').dataTable( {
    dom: 'C<"clear">lfrtip',
    colVis: {
        exclude: [ 0 ]
    }
} );

0 个答案:

没有答案