1

My datatable's column looks wider if the value is too long.

enter image description here

i have following this and this. And setting the width :

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

But there is no change in my datatable.

Community
  • 1
  • 1
nunu
  • 2,703
  • 9
  • 33
  • 55

3 Answers3

8

I would do this

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

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

This is implied that autoWidth is set to false and you have given the columns a fixed width (as in the demo and as OP have described he does with aoColumns / columns).

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
1

For now, I can tell you, that the best way for displaying data is modifying your output.

It means:

  1. if you create response on base of sql query -> you should optimize it and add a space in your quesry.
  2. If you do it in template -> prepare data on templeate part, example on PHP
  3. If you do it on frontend part, make it in JS way.

PHP way :

$result = array( /* your result */);
foreach($result as &$answer ){
   $answer = implode( ", ", explode( ",", $answer) );
}

JS way :

var result = [/* your result */];
for( var index = 0; index < result.length; i++ ){
  result[index] = result[index].split(",").join(", ");
}
xAqweRx
  • 1,236
  • 1
  • 10
  • 23
1

You can fix this with css.

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

table td
{
  word-wrap:break-word;   
  overflow: hidden;
}
neer
  • 4,031
  • 6
  • 20
  • 34