jQuery Datatables show html content
Asked Answered
F

6

9

I have a jquery datatable on my page, which uses server side processing to retrieve data. In this case, one of the columns contains html content, thus my server responses looks like this:

"aaData": [ [1, "aaa", "<span class="myclass">html here</span>" ], ...

I tryed with

"aoColumnDefs": [ "aTargets":[2], "sType": "html" }

But I still see the cell content as if it were plain string. What can I do?

Fricke answered 7/9, 2012 at 14:49 Comment(0)
D
10

I update SamuGG's answer, for new datatable version:

"aoColumnDefs": [ {
                     "aTargets": [ 5 ],
                     "mRender": function ( data, type, full ) {
                      return $("<div/>").html(data).text(); 
                      }
            } ]
Draco answered 22/12, 2016 at 8:42 Comment(0)
F
8

Made a working version with

"aoColumnDefs": [ 
    { "aTargets": [2], 
      "sType": "html", 
      "fnRender": function(o, val) { 
          return $("<div/>").html(o.aData[2]).text();
      } 
    }
]

decoding back the html with jQuery.

Fricke answered 7/9, 2012 at 18:29 Comment(0)
A
1

You need to just add below line before rawColumns() or make(true)

->escapeColumns('aaData')

Use this method and pass column name thats'it

Anyhow answered 5/6, 2021 at 3:12 Comment(0)
W
0
var renderAsHtml = function (data, type, full) {
return decHTMLifEnc(data);
}; 
var isEncHTML = function(str) {
    if(str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1)
        return true;
    else
        return false;
};

var decHTMLifEnc = function(str){
    if(isEncHTML(str))
        return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    return str;
}

pass renderAsHtml as function reference to fnRender. This is also works

Whitebait answered 10/1, 2014 at 12:26 Comment(0)
W
0

This is how I render HTML tags in my datatable columns:

ajax: "getData?userobjid=<%=session.getAttribute("userobjectid")%>&alpha=&selectsuppliersflag="+selectsuppliersflag,
columns: [
            { data:  null, render: function ( data, type, row ) {
                if(data.storeFrontUrl == 'undefined' || data.storeFrontUrl==null){
                        return "<label>"+data.companyName+"</label>";
                         }
                        else{
                        return "<a href='JavaScript:newPopup(\"directorylist/view/"+data.storeFrontUrl+"\");'>"+data.companyName+"</a>";
                     }
                    }},
Willdon answered 14/7, 2015 at 9:52 Comment(0)
W
0

Add .text() on render function

render: function(data, type, sop, meta) {
         var sutsut = '<div class="text-wrap width-200">' + sop.info + '</div>';
        return $(sutsut).text();                                            
           } 
Westbrooks answered 30/4, 2023 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.