【问题标题】:Hide table headers of table when mobile responsive移动响应时隐藏表格的表格标题
【发布时间】:2017-01-25 06:27:42
【问题描述】:

我正在使用 jQuery Datatables,并且我已经创建了相同的表。我的任务是让它具有移动响应能力,我做到了。但是当我在移动视图中查看表格时,我想完全隐藏表格标题,它应该恢复正常,就像在正常屏幕上显示的标题一样。

更具体地说,我通常可以使用 css 隐藏标题,但是当我启用垂直滚动到我的表格时,我的标题在我不想要的移动视图中变得可见。

示例表代码。重复数据以进行测试。

<table id="no-more-tables">
<thead>
    <tr>
        <th>Code</th>
        <th>Company</th>
        <th class="numeric">Price</th>
        <th class="numeric">Change</th>
        <th class="numeric">Change %</th>
        <th class="numeric">Open</th>
        <th class="numeric">High</th>
        <th class="numeric">Low</th>
        <th class="numeric">Volume</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td data-title="Code">AAC</td>
        <td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.     </td>
        <td data-title="Price" class="numeric">$1.38</td>
        <td data-title="Change" class="numeric">-0.01</td>
        <td data-title="Change %" class="numeric">-0.36%</td>
        <td data-title="Open" class="numeric">$1.39</td>
        <td data-title="High" class="numeric">$1.39</td>
        <td data-title="Low" class="numeric">$1.38</td>
        <td data-title="Volume" class="numeric">9,395</td>
    </tr>
    <tr>
        <td data-title="Code">AAC</td>
        <td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.     </td>
        <td data-title="Price" class="numeric">$1.38</td>
        <td data-title="Change" class="numeric">-0.01</td>
        <td data-title="Change %" class="numeric">-0.36%</td>
        <td data-title="Open" class="numeric">$1.39</td>
        <td data-title="High" class="numeric">$1.39</td>
        <td data-title="Low" class="numeric">$1.38</td>
        <td data-title="Volume" class="numeric">9,395</td>
    </tr>
    <tr>
        <td data-title="Code">AAC</td>
        <td data-title="Company">AUSTRALIAN AGRICULTURAL COMPANY LIMITED.     </td>
        <td data-title="Price" class="numeric">$1.38</td>
        <td data-title="Change" class="numeric">-0.01</td>
        <td data-title="Change %" class="numeric">-0.36%</td>
        <td data-title="Open" class="numeric">$1.39</td>
        <td data-title="High" class="numeric">$1.39</td>
        <td data-title="Low" class="numeric">$1.38</td>
        <td data-title="Volume" class="numeric">9,395</td>
    </tr>
</tbody>
</table>

同样的css是:

<style>
@media only screen and (max-width: 800px) {

/* Force table to not be like tables anymore */
#no-more-tables table, 
#no-more-tables thead, 
#no-more-tables tbody, 
#no-more-tables th, 
#no-more-tables td, 
#no-more-tables tr { 
    display: block; 
}

/* Hide table headers (but not display: none;, for accessibility) */
#no-more-tables thead tr { 
    position: absolute;
    top: -9999px;
    left: -9999px;
}

#no-more-tables tr { border: 1px solid #ccc; }

#no-more-tables td { 
    /* Behave  like a "row" */
    border: none;
    border-bottom: 1px solid #eee; 
    position: relative;
    padding-left: 50%; 
    white-space: normal;
    text-align:left;
}

#no-more-tables td:before { 
    /* Now like a table header */
    position: absolute;
    /* Top/left values mimic padding */
    top: 6px;
    left: 6px;
    width: 45%; 
    padding-right: 10px; 
    white-space: nowrap;
    text-align:left;
    font-weight: bold;
}

/*
Label the data
*/
#no-more-tables td:before { content: attr(data-title); }
}
</style>    

表格转数据表的jQuery代码:

<script>
$(document).ready(function (){
$('#no-more-tables').DataTable({
"bSort" : false,
"paging" : false,
"info" : false,
"searching" : false,
"bProcessing" : true,
"scrollY":"400px",
"scrollCollapse": true,
"scrollX": false
});
});
</script>

【问题讨论】:

  • 检查这个 url 和它可能有用的完整代码:sanwebcorner.com/2016/12/…
  • 嘿..我检查了你给我的网址..我使用确切的方法使我的表格响应..但是当我添加一个滚动-Y选项以通过标题进行垂直滚动时变得可见我不想...请帮忙。
  • 为了给您一个很好的答案,如果您还没有看过How to Ask,它可能会对我们有所帮助。如果您可以提供minimal reproducible example,它可能也很有用。
  • 感谢您的指导..请找到上面的代码作为参考

标签: jquery css datatables


【解决方案1】:

当您使用数据表时,它正在更改 dom。 所以在css中用#no-more-tables_wrapper替换#no-more-tables。 您的代码将起作用。请检查确认

【讨论】:

  • 我试过 display:none;在媒体查询中..但没有用。
  • 请补充,如何启用垂直滚动
  • "scrollY":"400px", "scrollCollapse": true,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2019-06-10
  • 1970-01-01
  • 2020-02-27
相关资源
最近更新 更多