简体   繁体   中英

Datatable Jquery special character encoding and decoding to HTML

I found out that html special characters such as '&' are not being decoded when we fetch value from the table object using API functions but instead of they are in ASCII or Unicode form. This is my simple initialization for the dataTable.

var otable = $('#vtable').DataTable({
        "dom": '<"top"lBf<"clear">>rt<"bottom"ip<"clear">>'
}); 

See this Fiddle .

What should be best solution to decode html special character before passing it to the data processing?

Encode HTML entities:

var valEncoded = $('<div>').text(val).html();

Decode HTML entities:

var valDecoded = $('<div>').html(val).text();

See this example for code and demonstration.

See updated jsFiddle for demonstration on how it could be used in your project.

Using this answer of Stackoverflow, the solution for question is the function below to decode the html characters from a row of data.

function decodeHtml(str) {
    var map =
    {
        '&amp;': '&',
        '&lt;': '<',
        '&gt;': '>',
        '&quot;': '"',
        '&#039;': "'"
    };
    return str.replace(/&amp;|&lt;|&gt;|&quot;|&#039;/g, function (m) { return map[m]; 
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM