jquery 数据表自动换行不起作用
jquery datatables word wrap not working
如果值太长,我的数据表的列看起来会更宽。
我关注 this and this。
并设置宽度:
aTable = $("#printdata").dataTable({
"bAutoWidth" : false,
"bRetrieve" : true,
"scrollY": 200,
"scrollX": true,
"deferRender": true,
"scroller": {
loadingIndicator: true
},
"bServerSide": true,
"bProcessing": true,
"sAjaxSource": 'show2ndsampling.php',
"fnServerData": function (sSource,aoData,fnCallback){
$.ajax({
"dataType":'json',
"type":'POST',
"url":sSource,
"data":aoData,
"success":function(json){
fnCallback(json);
}
});
},
"order" : [[1,"desc"]],
"aoColumns" : [
/*serial*/{ "width": "30%", target : 3 }
]
但是我的数据表没有变化。
目前,我可以告诉您,显示数据的最佳方式是修改输出。
意思是:
- 如果您在
sql query
的基础上创建响应 -> 您应该优化
它并在您的查询中添加 space。
- 如果您在模板中执行 -> 在模板部分准备数据,例如 PHP
- 如果你在前端做,就用JS方式做。
PHP 方式:
$result = array( /* your result */);
foreach($result as &$answer ){
$answer = implode( ", ", explode( ",", $answer) );
}
JS 方式:
var result = [/* your result */];
for( var index = 0; index < result.length; i++ ){
result[index] = result[index].split(",").join(", ");
}
您可以使用 css 解决此问题。
table /* give class of table*/
{
table-layout: fixed;
}
table td
{
word-wrap:break-word;
overflow: hidden;
}
我会这样做
table.dataTable tbody td {
word-break: break-word;
vertical-align: top;
}
演示 -> http://jsfiddle.net/qh63k1sg/
这意味着 autoWidth
设置为 false 并且您已为列指定了固定宽度(如演示中和 OP 描述的那样,他使用 aoColumns
/ columns
).
如果值太长,我的数据表的列看起来会更宽。
我关注 this and this。 并设置宽度:
aTable = $("#printdata").dataTable({
"bAutoWidth" : false,
"bRetrieve" : true,
"scrollY": 200,
"scrollX": true,
"deferRender": true,
"scroller": {
loadingIndicator: true
},
"bServerSide": true,
"bProcessing": true,
"sAjaxSource": 'show2ndsampling.php',
"fnServerData": function (sSource,aoData,fnCallback){
$.ajax({
"dataType":'json',
"type":'POST',
"url":sSource,
"data":aoData,
"success":function(json){
fnCallback(json);
}
});
},
"order" : [[1,"desc"]],
"aoColumns" : [
/*serial*/{ "width": "30%", target : 3 }
]
但是我的数据表没有变化。
目前,我可以告诉您,显示数据的最佳方式是修改输出。
意思是:
- 如果您在
sql query
的基础上创建响应 -> 您应该优化 它并在您的查询中添加 space。 - 如果您在模板中执行 -> 在模板部分准备数据,例如 PHP
- 如果你在前端做,就用JS方式做。
PHP 方式:
$result = array( /* your result */);
foreach($result as &$answer ){
$answer = implode( ", ", explode( ",", $answer) );
}
JS 方式:
var result = [/* your result */];
for( var index = 0; index < result.length; i++ ){
result[index] = result[index].split(",").join(", ");
}
您可以使用 css 解决此问题。
table /* give class of table*/
{
table-layout: fixed;
}
table td
{
word-wrap:break-word;
overflow: hidden;
}
我会这样做
table.dataTable tbody td {
word-break: break-word;
vertical-align: top;
}
演示 -> http://jsfiddle.net/qh63k1sg/
这意味着 autoWidth
设置为 false 并且您已为列指定了固定宽度(如演示中和 OP 描述的那样,他使用 aoColumns
/ columns
).