How can I implement new line in a column (DataTables)

孤人 提交于 2020-12-06 03:48:15

问题


I have an SQL query that grabs some data (language column in dbtable table). The query uses GROUP_CONCAT, so one cell has several results, e.g.

"Ajax, jQuery, HTML, CSS".

What I want to do is to show the result in new lines, like:

"Ajax
jQuery
HTML
CSS"

How can I do that?

I tried to make it by changing "columns": [{ "data": "id" }, { "data": "languages" }... but it didn't work.

I also tried to fix it by adding "< br >" in query as a Separator, but didn't work.

Thank you!


回答1:


You can use columns.render function for the column like this:

var table = $('#example').DataTable({
    columns: [
        null,
        null,
        {
            "render": function(data, type, row){
                return data.split(", ").join("<br/>");
            }
        }
    ]
});

Working example.

Hope that helps and that I've understood your problem.




回答2:


@annoyingmouse answer is perfect taking into account the question description!

Just an extra answer following "strictly" the question (as it is) which says:

How can I implement new line in a column (DataTables)

You have just to add a wrap class on the <table> tag. Tada! Each column text will be wrapped when reaching its predefined width.




回答3:


To implement new line in a column for DataTables

Assume that there are 7 columns in a row and the 4th column has the data

"Ajax, jQuery, HTML, CSS"

so the 3 columns before and 3 columns after has to be made null in the code

$('#example').DataTable({
    columns: [
        null, null, null,
        {   
            "render": function(data, type, row){
                return data.split(", ").join("<br/>");
            }
        },
        null, null, null
    ]
});


来源:https://stackoverflow.com/questions/32791577/how-can-i-implement-new-line-in-a-column-datatables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!