jQuery DataTables fixed size for ONE of the columns?

可紊 提交于 2019-11-28 10:36:21

问题


I have a DataTables table where I want it to autosize as default (when resizing window and according to initial content etc.), except for one special column, for which I'd like to have a fixed width.

It seems, if I set a width using columnDefs, the table is initially autosized according to content, but it no longer adapts to a changed window width. Doesn't seem to matter if I specify a % width or a pixel width.


回答1:


If you want precise control over the table column widths you must use CSS:

table.dataTable thead th:nth-child(4),
table.dataTable tbody td:nth-child(4) {
  width: 200px;
  max-width: 200px;
  min-width: 200px;
}  

If you inspect the above column you will see the that computed width is larger than 200px. This is the padding kicking in, so you must reset padding-left and padding-right to get exactly 200px.

If you want to shrink a column to a width beyond "reasonable" you must add som additional CSS :

table.dataTable {
  table-layout: fixed;
}

Why this is needed you can read about here -> https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout#Values

table.dataTable thead th:nth-child(2),
table.dataTable tbody td:nth-child(2) {
  word-break: break-all;
  overflow-wrap: break-word;
  width: 20px;
  max-width: 20px;
  padding-right: 0px;
  padding-left: 0px;
}

demo -> http://jsfiddle.net/n4j1wgu5/



来源:https://stackoverflow.com/questions/47674920/jquery-datatables-fixed-size-for-one-of-the-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!