jquery datatable - 设置列宽和换行文本

Vim*_*esh 7 html javascript css jquery datatables

我们在jquery数据表中使用Scroller插件来允许虚拟滚动.但是,我们要求支持单元格内的文本换行,因为用户希望查看整个文本而不是截断的文本.我发现默认情况下它不会包装文本.有没有办法支持这个功能?任何可能的解决方法?

提琴手 https://jsfiddle.net/Vimalan/2koex0bt/1/

HTML代码:

<table id="example" class="display" cellspacing="0" width="100%"></table>
Run Code Online (Sandbox Code Playgroud)

js代码:

var detailsSample = "DataTables and its extensions are extremely configurable libraries and almost every aspect of the enhancements they make to HTML tables can be customised. Features can be enabled, disabled or customised to meet your exact needs for your table implementations."
var dataList = [];

for (var i=1; i<=500; i++)
{
    var dataRow = {};

    dataRow.ID = i;
    dataRow.FirstName = "First Name " + i;
    dataRow.LastName = "Last Name " + i;

    if (i%5 ==0)
    {
        dataRow.Details = detailsSample;
    }
    else
    {
        dataRow.Details = "Large text "+i;
    }
    dataRow.Country = "Country Name";

    dataList.push(dataRow);
}

$('#example').DataTable( {
                data:                       dataList,
        deferRender:    true,
        scrollX:                true,
        scrollY:        200,
        scrollCollapse: true,
        scroller:       true,
        searching:          false,
        paging:                 true,
        info:                   false,
        columns: [
                { title: "ID", data: "ID" },
                { title: "First Name", data: "FirstName" },
                { title: "Change Summary", data: "LastName"},
                { title: "Details", data: "Details", "width": "400px"  },
                { title: "Country", data: "Country" }               
            ]
    } );
Run Code Online (Sandbox Code Playgroud)

期望

在上表中,"Details"列的宽度应始终为400px,并且该列中的文本应该换行.

任何建议将不胜感激.

Vim*_*esh 20

通过将列数据包装在div中并设置div的white-space,width css属性来找到解决方案.

提琴手:https ://jsfiddle.net/Vimalan/2koex0bt/6/

JS

$('#example').DataTable( {
        data:           dataList,
        deferRender:    true,
        scrollX:        true,
        scrollY:        200,
        scrollCollapse: true,
        scroller:       true,
        searching:      false,
        paging:         true,
        info:           false,
        columns: [
                { title: "ID", data: "ID" },
                { title: "First Name", data: "FirstName" },
                { title: "Change Summary", data: "LastName"},
                { title: "Details", data: "Details" },
                { title: "Country", data: "Country" }               
            ],
            columnDefs: [
                {
                    render: function (data, type, full, meta) {
                        return "<div class='text-wrap width-200'>" + data + "</div>";
                    },
                    targets: 3
                }
             ]
    } );
Run Code Online (Sandbox Code Playgroud)

CSS

.text-wrap{
    white-space:normal;
}
.width-200{
    width:200px;
}
Run Code Online (Sandbox Code Playgroud)

  • 在看到你的答案之前,我根本不知道"渲染".非常实用!解决了我在列中遇到的很多其他问题.在我看来,这是让他们按照你想要的方式行事最可靠的方式. (2认同)

Kei*_*ith 9

不知道是否添加样式表,但是将表格布局设置为固定并添加空格。当前,您正在使用white-space:no-wrap否定您要尝试执行的操作。另外,我为所有td设置了最大宽度,因此,只要有溢出就会发生这种情况。

在jQUery的末尾添加

https://jsfiddle.net/2koex0bt/5/

$(document).ready(function(){
    $('#example').DataTable();
    $('#example td').css('white-space','initial');
});
Run Code Online (Sandbox Code Playgroud)

如果您只想使用api,请执行此操作(再添加CSS来更改样式)。

如果要使用CSS进行此操作,请执行以下操作:

table {
  table-layout:fixed;
}
table td {
  word-wrap: break-word;
  max-width: 400px;
}
#example td {
  white-space:inherit;
}
Run Code Online (Sandbox Code Playgroud)