'How can I wrap text wrapping on a jquery datatable?
I'm trying to display some long text with help of a datatable, but somehow it is not beeing wraped. This means that instead of getting multiple rows of my text, I just get a long line. When I go to my DB and edit my Description field (Field type is text), I can see the text in multiple rows.
I already tried fixing the width of the table to 100%, but then the text line just keeps going outside the table. I also tried adding word-wrap and flex-wrap, and defining a text-wrap class on my CSS, but it also didn't work.
Any idea how can I solve this?
<div style="font-size:18px;">
<table class="table-striped table-hover" id="product_table" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Group</th>
<th>Price</th>
<th>Action</th>
<th class="none">Description DE</th>
<th class="none">Description EN</th>
<th class="none">Description ES</th>
</tr>
</thead>
<tbody>
@foreach($ProductList as $product)
<tr>
<td>{{$product['ProductName']}}</td>
<td>{{$product['ProductGroupName']}}</td>
<td>{{$product['ProductListPrice']}}</td>
<td ><button class="btn btn-primary waves-effect w-md waves-light w-sm-5" data-toggle="modal" data-target="#Edit" data-id="{{$product['ProductID']}}" data-name="{{$product['ProductName']}}" data-group="{{$product['ProductGroupName']}}"
data-price="{{$product['ProductListPrice']}}" data-descde="{{$product['ProductDescriptionDE']}}" data-descen="{{$product['ProductDescriptionEN']}}" data-desces="{{$product['ProductDescriptionES']}}">Edit</button>
<td>{{$product['ProductDescriptionDE']}}</td>
<td>{{$product['ProductDescriptionEN']}}</td>
<td>{{$product['ProductDescriptionES']}}</td>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#product_table').DataTable({
columnDefs: [{
"className": "dt-center",
"targets": "_all"
}],
pageLength: 50,
responsive: true
});
});
</script>
Solution 1:[1]
try to add wrap class in your table.
<table class="table-striped table-hover wrap" id="product_table" cellspacing="0" width="100%">
https://datatables.net/forums/discussion/43760/force-datatable-to-wrap-text#Comment_115199
Solution 2:[2]
Here is my solution, it works on all kinds of table:
table.dataTable tbody td {
word-break: break-word; word-break: break-all; white-space: normal;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Md.Sukel Ali |
| Solution 2 | Bigyellowbee |
