【问题标题】:the width of first column of jQuery dataTable cannot be setjQuery dataTable 第一列的宽度不能设置
【发布时间】:2016-12-15 05:38:23
【问题描述】:

在我的项目中,jQuery dataTable 的第一列中添加了一个复选框,为用户提供多选功能。但是,它的宽度是不受控制的,当全屏时它太大了,我只想让它总是以 45px 显示。我尝试在 jQuery 和 css 中修复它,但都失败了。

我的尝试:

//jQuery
$('.dataTable').dataTable( {
  "columnDefs": [
    { "width": "45", "targets": 0 }
  ]
} ); 

//css
.dataTable > thead > tr > th:first-child {
    width: 45px!important;
}

有什么想法吗?

【问题讨论】:

  • 你试过{ "width": "45px", "targets": 0 };即在您的值之后添加 px。
  • @vijayP 当然,没有任何反应
  • 您是否尝试过文档页面上提供的其他选项:datatables.net/reference/option/columns.width
  • 以百分比定义宽度,例如宽度:20%。

标签: javascript jquery html css datatables


【解决方案1】:

在 js 中试试这个:

$('#datatable').dataTable( {
  "columnDefs": [
    { "width": "45px", "targets": 0 }
  ]
} );

或在css中:

#datatable td:nth-child(1)
    {
        width: 45px;
    }

【讨论】:

  • td:nth-child(9) 是什么意思?
  • 这是错误的。是1
【解决方案2】:

有什么想法吗?

是的。你不能依赖width;如果其他元素缩小了空间或提供了比所需更多的空间,浏览器不一定会保留width。但是,如果您定义了 min-width,那么您可以确定 width 默认设置为该值:

table.dataTable tbody td:first-child {
  min-width: 45px !important;
}

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 2012-07-18
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多