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.

3
  • It means that you don't have spaces in your word. That's why this happens. Commented Jun 21, 2016 at 8:21
  • 1
    Word wrap is working when word have space. Commented Jun 21, 2016 at 8:22
  • 1
    Please post your full Datatables initialization code Commented Jun 21, 2016 at 9:11

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

Comments

2

You can fix this with css.

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

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

Comments

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(", ");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.