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

标签 javascript jquery html css datatables

我们正在为我们的 jquery 数据表使用 Scroller 插件以允许虚拟滚动。但是,我们需要支持单元格内的文本换行,因为用户希望查看整个文本而不是 chop 的文本。我观察到它默认不换行文本。有没有办法支持这个功能?任何可能的解决方法?

fiddler https://jsfiddle.net/Vimalan/2koex0bt/1/

html代码:

<table id="example" class="display" cellspacing="0" width="100%"></table>

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

期待

在上表中,“详细信息”列的宽度应始终为 400 像素,并且该列中的文本应换行。

任何建议将不胜感激。

最佳答案

通过将列数据包装在 div 中并为 div 设置 white-space、width css 属性找到了解决方案。

fiddler : 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
                }
             ]
    } );

CSS

.text-wrap{
    white-space:normal;
}
.width-200{
    width:200px;
}

关于javascript - jquery datatable - 设置列宽和换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666677/

相关文章:

javascript - 从 JavaScript 数组打印值

javascript - jQuery 追加 url 参数的问题

javascript - 如何在父项扩展时使 Bootstrap 中的插件保持不变

javascript - 如何用 HTML 解析 XML 的示例代码?

javascript - 修复嵌入音频以在 iOS 上运行

javascript - 取消 JavaScript 中的加载页面

返回不同类型的 JavaScript 函数(Array 与 NodeList)

javascript - 如何选择类为 "xxx"的所有复选框?

jquery - 拥有适用于所有设备的图像背景的正确方法是什么?

JavaScript:如何使用 HREF 移动 DIV?