Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

datatables

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!

like image 611
Sam S. Avatar asked Sep 25 '15 22:09

Sam S.


2 Answers

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.

like image 80
annoyingmouse Avatar answered Sep 24 '22 07:09

annoyingmouse


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
    ]
});
like image 43
Merrin K Avatar answered Sep 24 '22 07:09

Merrin K