Elm*_*mor 22 javascript datatables jquery-datatables
我正在尝试设置列宽,如下所示:
var per_page = $("table").data("per_page");
$(".table").dataTable({
"aoColumnDefs": [
{ "sWidth": "100px", "aTargets": [ 1 ] },
{ "sWidth": "100px", "aTargets": [ 2 ] },
{ "sWidth": "100px", "aTargets": [ 3 ] },
{ "sWidth": "100px", "aTargets": [ 4 ] },
{ "sWidth": "100px", "aTargets": [ 5 ] },
{ "sWidth": "100px", "aTargets": [ 6 ] },
{ "sWidth": "100px", "aTargets": [ 7 ] }
],
"aoColumns" : [
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
{ "sWidth": "100px"},
],
bJQueryUI: true,
iDisplayLength: per_page,
"fnDrawCallback": function( oSettings ) {
if (oSettings._iDisplayLength == per_page)
return true
else {
$.post($(this).data("url"), {iDisplayLength: oSettings._iDisplayLength})
.done(function(data){
if (data.success)
per_page = oSettings._iDisplayLength;
});
}
}
})
Run Code Online (Sandbox Code Playgroud)
但是产生的列宽并不是我想要设置的.请问你能帮帮我吗?
更新1
我已按如下方式更新了我的初始化代码,但在IE 9上遇到了奇怪的行为:即获取最长的字段,将其分成行,并将其长度作为此列的所有行的默认值.
var per_page = $("table").data("per_page");
$(".table").dataTable({
sScrollX: "100%",
aoColumns : [
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
],
bJQueryUI: true,
iDisplayLength: per_page,
"fnDrawCallback": function( oSettings ) {
if (oSettings._iDisplayLength == per_page)
return true
else {
$.post($(this).data("url"), {iDisplayLength: oSettings._iDisplayLength})
.done(function(data){
if (data.success)
per_page = oSettings._iDisplayLength;
});
}
}
})
Run Code Online (Sandbox Code Playgroud)
更新2
我已更新代码,如下所示,ie 9中的结果是数据表的标题调整为新大小,但表的其余部分未被更改所取代,请参见屏幕截图http://gyazo.com/ 282967b051366b18634d4e778205c938
初始化代码:
var per_page = $("table").data("per_page");
var datTable = $(".table").dataTable({
sScrollX: "100%",
sScrollX: "500px",
aoColumnDefs: [
{ bSortable: false, aTargets: [ 4, 5,6 ] },
{ sWidth: "16%", aTargets: [ 1, 2,3,4,5,6 ] },
],
bJQueryUI: true,
sAutoWidth: false,
iDisplayLength: per_page,
"fnDrawCallback": function( oSettings ) {
if (oSettings._iDisplayLength == per_page)
return true
else {
$.post($(this).data("url"), {iDisplayLength: oSettings._iDisplayLength})
.done(function(data){
if (data.success)
per_page = oSettings._iDisplayLength;
});
}
}
})
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
Bal*_*zar 31
我在你的更新2中看到你有用sAutoWidth,但我觉得你错了bAutoWidth.试着改变这个.
.table如果问题仍然存在,您还可以添加CSS规则.
当内容的宽度大于列的标题时,您还应该小心.
所以类似于1和2的组合:
$('.table').dataTable({
bAutoWidth: false,
aoColumns : [
{ sWidth: '15%' },
{ sWidth: '15%' },
{ sWidth: '15%' },
{ sWidth: '15%' },
{ sWidth: '15%' },
{ sWidth: '15%' },
{ sWidth: '10%' }
]
});
Run Code Online (Sandbox Code Playgroud)
小智 17
你应该使用" bAutoWidth"的属性数据表,并给宽度在每个%TD /列
$(".table").dataTable({"bAutoWidth": false ,
aoColumns : [
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "15%"},
{ "sWidth": "10%"},
]
});
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助.
我的方法
$('#table_1').DataTable({
processing: true,
serverSide: true,
ajax: 'customer/data',
columns: [
{ data: 'id', name: 'id' , width: '50px', class: 'text-right' },
{ data: 'name', name: 'name' width: '50px', class: 'text-right' }
]
});
Run Code Online (Sandbox Code Playgroud)
这是我可以让它工作的唯一方法:
JS:
"bAutoWidth": false,
columnDefs: [
{ "width": "100px", "targets": [0] }
]
Run Code Online (Sandbox Code Playgroud)
CSS:
#yourTable {
table-layout: fixed !important;
word-wrap: break-word;
}
Run Code Online (Sandbox Code Playgroud)
CSS 部分不一定是干净的,但它可以完成工作。
我可以告诉你另一种简单的方法来修复html本身数据表的宽度。
使用
<colgroup>
<col width="3%">
<col width="3%">
</colgroup>
Run Code Online (Sandbox Code Playgroud)
下面是数据表的示例代码:
<table class="table datatable">
<colgroup>
<col width="33%">
<col width="33%">
<col width="33%">
<col width="33%">
</colgroup>
<thead>
<tr>
<th>User Id</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
<tr>
<th>alpha</th>
<th>beta</th>
<th>gama</th>
<th>delta</th>
</tr>
<tr>
<th>alpha</th>
<th>beta</th>
<th>gama</th>
<th>delta</th>
</tr>
<tr>
<th>alpha</th>
<th>beta</th>
<th>gama</th>
<th>delta</th>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88136 次 |
| 最近记录: |