[英]Word wrap column data in Javascript datatable
我有一個JS數據表,我們輸入客戶信息,在某些情況下,一些客戶參考是這樣的
reference_text=%26reference%5Ftext%3D%2526reference%255Ftext%253Dtest%252520ipsum%25252C%25252008%25252DAug%25252D08%2546%26&
這打破了我的html表並強制列不成比例增長,因為它不包含空格,我在JS小提琴中設置了一個例子來說明這個問題,有沒有辦法可以強制這個列以一致的格式與另一個或包裝文本,使其適合列?
$(document).ready(function () {
if ($('#report_gen_user').length) {
$('#report_gen_user').dataTable(
{
"iDisplayLength": -1,
initComplete: function () {
var api = this.api();
api.columns().indexes().flatten().each(function (i) {
if (i == 2 || i == 9) {
var column = api.column(i);
var select = $('<select><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
$(".hidefooter").html("");
}
});
},
"aLengthMenu": [
[15, 25, 35, 50, 100, -1],
[15, 25, 35, 50, 100, "All"]
],
"aoColumnDefs": [{
"bVisible": false,
"aTargets": []
}],
"aaSorting": [],
"fnFooterCallback": function (nRow, aasData, iStart, iEnd, aiDisplay) {
var columnas = [1, 5]; //the columns you wish to add
for (var j in columnas) {
var columnaActual = columnas[j];
var total = 0;
var allTimeTotal = 0;
for (var i = iStart; i < iEnd; i++) {
total = total + parseFloat(aasData[aiDisplay[i]][columnaActual]);
}
total = total.toFixed(2);
for (var counter in aasData) {
allTimeTotal = allTimeTotal + parseFloat(aasData[counter][columnaActual]);
//console.log((aasData[counter][columnaActual]));
}
allTimeTotal = allTimeTotal.toFixed(2);
if (total == allTimeTotal) {
$($(nRow).children().get(columnaActual)).html(' '+total);
} else {
$($(nRow).children().get(columnaActual)).html(' '+total + ' (' + allTimeTotal + ')');
}
} // end
}
}
);
}
})
將autoWidth
設置為false並定義您喜歡的column
width
:
var table = $('#example').DataTable({
autoWidth: false,
columns : [
{ width : '50px' },
{ width : '50px' },
{ width : '50px' },
{ width : '50px' },
{ width : '50px' },
{ width : '50px' }
]
});
那么,最重要的 - 添加這個CSS:
table.dataTable tbody td {
word-break: break-word;
vertical-align: top;
}
word-break
是重要的部分, vertical-top
是眼睛:)
演示 - > http://jsfiddle.net/qh63k1sg/
在你的小提琴中,上面似乎不起作用,但這是因為你將每個字符串作為值添加到<select>
,最終會更長。 為防止這種情況,請在插入<option>
值之前剪掉長串; 你可以添加...
到最后:
column.data().unique().sort().each(function (d, j) {
if (d.length>25) { d=d.substring(0,25)+'...' }
select.append('<option value="' + d + '">' + d + '</option>')
});
你的小提琴分叉 - > http://jsfiddle.net/bdwd1ee7/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.