2016-06-21 169 views
1

如果值太長,我的數據表的列看起來更寬。jquery datatables換行不起作用

enter image description here

我有以下thisthis。 和設置寬度:

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 } 
    ] 

但在我的數據表沒有改變。

+0

這意味着你的單詞沒有空格。這就是爲什麼會發生。 – xAqweRx

+1

單詞有空格時,單詞換行正在工作。 –

+1

請發佈您的完整數據表初始化代碼 – Yuri

回答

2

我會做這個

table.dataTable tbody td { 
    word-break: break-word; 
    vertical-align: top; 
} 

演示 - >http://jsfiddle.net/qh63k1sg/

這意味着autoWidth被設置爲false,你給列一個固定的寬度(如演示和OP所描述的他與aoColumns/columns)。

+0

是的正確我設置'autowidth:false'。 – nunu

1

現在,我可以告訴你,顯示數據的最佳方法是修改你的輸出。

這意味着:

  1. 如果你創建的sql query基準響應 - >您應該優化 它和你的quesry添加一個空格。
  2. 如果你在模板中做準備 - >在templete部分上準備數據,例如PHP
  3. 如果你在前端部分做它,以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(", "); 
} 
0

您可以用CSS解決這個問題。

table /* give class of table*/ 
{ 
    table-layout: fixed; 
} 

table td 
{ 
    word-wrap:break-word; 
    overflow: hidden; 
}