DataTables - StripHtml Issue
HTML
<link rel="stylesheet" href="//cdn.datatables.net/1.10.8/css/dataTables.jqueryui.min.css">
<link rel="stylesheet" href="//cdn.datatables.net/buttons/1.0.1/css/buttons.jqueryui.min.css">
<script src="//cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.10.8/js/dataTables.jqueryui.min.js"></script>
<script src="//cdn.datatables.net/buttons/1.0.1/js/dataTables.buttons.min.js"></script>
<script src="//cdn.datatables.net/buttons/1.0.1/js/buttons.jqueryui.min.js"></script>
<script src="//cdn.datatables.net/buttons/1.0.1/js/buttons.flash.min.js"></script>
<script src="//cdn.datatables.net/buttons/1.0.1/js/buttons.html5.min.js"></script>
<script src="//cdn.datatables.net/buttons/1.0.1/js/buttons.print.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<table width="100%" class="display" id="example" cellspacing="0"></table>
CSS
body {
font: 90%/1.45em"Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
color: #333;
background-color: #fff;
}
div.container {
min-width: 980px;
margin: 0 auto;
}
JavaScript
String.prototype.replaceAll = function(s, r, c) {
return this.replace(new RegExp(s.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (c ? "gi" : "g")), (typeof(r) == "string") ? r.replace(/\$/g, "$$$$") : r);
};
String.prototype.htmlEncode = function() {
return this.replaceAll("&", "&", false).replaceAll("\"", """, false).replaceAll("'", "'", false).replaceAll("<", "<", false).replaceAll(">", ">", false);
};
$.fn.DataTable.render.htmlEncode = function () {
return {
display: function (data, type, row, meta) {
if (typeof data === "string" && data.trim() !== "") {
return data.htmlEncode();
}
else {
return data;
}
}
};
};
var dataSet = [
[ "Tiger < & > Nixon", "Tiger < & > > Nixon" ],
[ "Tiger <faketag> Nixon", "Tiger <faketag> Nixon" ],
[ "Tiger <faketag>Nixon</faketag>", "Tiger <faketag>Nixon</faketag>" ]
];
$(document).ready(function () {
var table = $('#example').DataTable({
dom: "Brt",
data: dataSet,
columns: [
{ title: "HTMLEncoded" },
{ title: "Not HTMLEncoded" }
],
columnDefs: [
{ targets: 0, render: $.fn.DataTable.render.htmlEncode() }
],
buttons: [
{
extend: "copy",
exportOptions: {
stripHtml: false,
orthogonal: "display"
}
},
{
extend: "csv",
exportOptions: {
stripHtml: false
}
},
{
extend: "excel",
exportOptions: {
stripHtml: false
}
},
{
extend: "pdf",
exportOptions: {
stripHtml: true
}
}
]
});
});