转载声明:文章链接:https://blog.csdn.net/lyxuefeng/article/details/108732099
需求:在dataTable的表格中,有时内容数据过长导致表格整体溢出界面。需要把过长的内容限定在一个范围。
法1:设置td的css样式(缺点是不能看到所有数据):
td {
    overflow:hidden;
    white-space:nowrap;
    text-overflow:ellipsis;
    max-width: 300px;//最大宽度
}法2:给返回的数据做截取,同时所有数据显示在title属性中,如:
"columns": [
            {
                "data": 'sqlParamValue',
                "visible": true,
                "render": function (data, type, row) {
                    //数据字符超过30截取
                    if (data.length > 30) {
                        return "<span title='" + data + "' style='text-decoration: none;'>" + data.trim().substr(0, 30) + "..." + "</span>";
                    } else {
                        return data;
                    }
                }
            },
]附:取消自动列宽并自行设置(但仅设置列宽却仍然溢出)
"autoWidth": false, //取消自动列宽
"columns": [
            {
                "data": 'sqlParamValue',
                "visible": true,
                "width": "30%",//设置该列宽度
                "render": function (data, type, row) {
                    if (data.length > 30) {
                        return "<span title='" + data + "' style='text-decoration: none;'>" + data.trim().substr(0, 30) + "..." + "</span>";
                    } else {
                        return data;
                    }
                }
            },
]
                
                    
                            
                            
                                    
                                    
帖子还没人回复快来抢沙发