
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
function TXLManager($table){
var $this = this;
$this.$table = $table;
if(typeof window.jQuery == 'undefined')
throw "TXLManager Error - jQuery must be required!"
$this.addTopRowInTable = function(data, style, colspan){
if(data == null) data = "";
var css = {
border: "0",
background: "inherit"
};
if(style && !style.length){
$.each(Object.keys(style), function(index, key){
css[key] = style[key];
});
}
if(typeof data != "string" && data.length){
if((style && !style.length) || typeof style == "string")
throw "Cannot use style of object type with multiple data!";
else if(style && data.length != style.length)
throw "Missmatched style array! style.length must equals to data.length!";
else if((colspan && !colspan.length) || typeof colspan == "string")
throw "Cannot use colspan of object type with multiple data!";
else if(colspan && data.length != colspan.length)
throw "Missmatched colspan array! colspan.length must equals to data.length!";
}
var tr = $this.$table.find("tbody tr");
if(tr.length){
var $tr = $(tr[0]);
var cols = $tr.children("td");
if(!cols) return false;
cols = cols.length;
var $newTr = $("<tr/>");
if(typeof data != "string" && data.length){
$.each(data, function(index, data){
var tmpCSS = {};
$.each(Object.keys(css), function(idx, key){
tmpCSS[key] = css[key];
});
$.each(Object.keys(style[index]), function(idx, key){
tmpCSS[key] = style[index][key];
});
var $newTd = null;
if(colspan)
$newTd = $("<td/>", { colspan: colspan[index] });
else $newTd = $("<td/>");
$newTd.text(data);
$newTd.css(tmpCSS);
$newTr.append($newTd);
});
}else{
var $newTd = $("<td/>",{
colspan: cols
});
$newTd.text(data);
$newTd.css(css);
$newTr.append($newTd);
}
var html = $newTr.wrap("<div>").parent().html();
$newTr.unwrap();
$newTr.remove();
if($this.$table.children("thead").length){
$this.$table.children("thead").html(
html
+ $this.$table.children("thead").html()
);
}else{
$this.$table.children("tbody").html(
html
+ $this.$table.children("tbody").html()
);
}
}
return $this;
}
$this.addLeftColInTable = function(data, style){
if(data == null) data = "";
var css = {
border: "0",
background: "inherit",
width: "10px"
};
if(style){
$.each(Object.keys(style), function(index, key){
css[key] = style[key];
});
}
if($this.$table.find("colgroup").length){
var $colgroup = $table.children("colgroup");
var $col = $("<col/>");
$col.css(css);
var html = $col.wrap("<div>").parent().html();
$col.unwrap();
$col.remove();
$colgroup.html(html + $colgroup.html());
}
if($this.$table.find("thead").length){
$.each($($this.$table.find("thead")).children("tr"), function(index, tr){
var $tr = $(tr);
var $newTd = $("<td>");
$newTd.text(data);
$newTd.css(css);
var html = $newTd.wrap("<div>").parent().html();
$newTd.unwrap();
$newTd.remove();
$tr.html(html + $tr.html());
});
}
if($this.$table.find("tbody").length){
$.each($($this.$table.find("tbody")).children("tr"), function(index, tr){
var $tr = $(tr);
var $newTd = $("<td>");
$newTd.text(data);
$newTd.css(css);
var html = $newTd.wrap("<div>").parent().html();
$newTd.unwrap();
$newTd.remove();
$tr.html(html + $tr.html());
});
}
return $this;
}
$this.wrapTable = function(data, style){
if(data == null) data = "";
var css = {
border: "0",
background: "inherit",
width: "10px"
};
if(style){
$.each(Object.keys(style), function(index, key){
css[key] = style[key];
});
}
var tr = $this.$table.find("tbody tr");
if(!tr.length) return;
var $tr = $(tr[0]);
var cols = $tr.children("td");
if(!cols) return false;
cols = cols.length;
if($this.$table.find("colgroup").length){
var $colgroup = $table.children("colgroup");
var $col = $("<col/>");
$col.css(css);
$colgroup.append($col);
}
if($this.$table.find("thead").length){
$.each($($this.$table.find("thead")).children("tr"), function(index, tr){
var $tr = $(tr);
var $newTd = $("<td>");
$newTd.text(data);
$newTd.css(css);
$tr.append($newTd);
});
}
if($this.$table.find("tbody").length){
$.each($($this.$table.find("tbody")).children("tr"), function(index, tr){
var $tr = $(tr);
var $newTd = $("<td>");
$newTd.text(data);
$newTd.css(css);
$tr.append($newTd);
});
var $tbody = $($this.$table.find("tbody")[0]);
var $newTr = $("<tr/>");
for(var i = 0; i < cols + 1; i++){
var $newTd = $("<td>");
$newTd.text(data);
$newTd.css({
border: "0",
background: "inherit"
});
$newTr.append($newTd);
}
$tbody.append($newTr);
}
return $this;
}
$this.removeAllColumnAt = function(idx){
var colgroup = $this.$table.find("colgroup col");
var thead = $this.$table.find("thead tr");
var tbody = $this.$table.find("tbody tr");
if(colgroup && colgroup.length && colgroup.length > idx)
$(colgroup[idx]).remove();
if(thead && thead.length){
var tr = $(thead[0]).children("th");
if(tr && tr.length && tr.length > idx) $(tr[idx]).remove();
}
if(tbody && tbody.length){
$.each(tbody, function(index, tr){
var td = $(tr).children("td");
if(td && td.length && td.length > idx) $(td[idx]).remove();
});
}
return $this;
}
$this.setCSSToTable = function(selector, style){
var $elements = $($this.$table.find(selector));
$.each($elements, function(index, element){
$(element).css(style);
});
return $this;
}
$this.exportTableToExcel = function(filename, splitter){
var tab_text='<table style="display: none; border: 0; border-collapse:collapse;">';
var textRange; var j=0;
tab = $this.$table[0]; // id of table
for(j = 0 ; j < tab.rows.length ; j++)
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
if(splitter) tab_text= tab_text.replace(/<br\s*[\/]?>/gi, splitter);//"\r\n");
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
var userAgent = navigator.userAgent.toLowerCase();
var ie_Version = parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]);
if(ie_Version <= 9){
var txtArea1 = window.open("#", "_blank", "toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no, width=10, height=10, visible=none", '');
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
txtArea1.document.execCommand("SaveAs", true, filename + ".xls");
txtArea1.close();
} else {
var uri = 'data:application/vnd.ms-excel;base64,'
var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{table}</body></html>';
var ctx = {worksheet: filename || 'Worksheet', table: tab_text};
var blob = new Blob([TXLManager.utils.format(template, ctx)],
{type:"application/vnd.ms-excel;charset=utf-8"});
window.navigator.msSaveOrOpenBlob(blob, filename + '.xls');
}
}
else{ //other browser not tested on IE 11
var uri = 'data:application/vnd.ms-excel,' + encodeURIComponent(tab_text);
var link = document.createElement("a");
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = filename + ".xls";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
return $this;
};
$this.getHTML = function(){
var $temp = $("<div/>");
var html = $this.$table.wrap($temp).parent().html();
$this.$table.unwrap();
$temp.remove();
return html;
}
return {
addTopRowInTable: $this.addTopRowInTable,
addLeftColInTable: $this.addLeftColInTable,
wrapTable: $this.wrapTable,
removeAllColumnAt: $this.removeAllColumnAt,
setCSSToTable: $this.setCSSToTable,
exportTableToExcel: $this.exportTableToExcel,
getHTML: $this.getHTML
};
}
TXLManager.utils = {};
TXLManager.utils.base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) };
TXLManager.utils.format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) };