").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.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 = $("
");
$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 = $(" | ");
$newTd.text(data);
$newTd.css(css);
$tr.append($newTd);
});
var $tbody = $($this.$table.find("tbody")[0]);
var $newTr = $(" | |
");
for(var i = 0; i < cols + 1; i++){
var $newTd = $("
");
$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='';
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+"";
tab_text=tab_text+" ";
tab_text= tab_text.replace(/]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/ ]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/]*>|<\/input>/gi, ""); // reomves input params
if(splitter) tab_text= tab_text.replace(/ /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 = '{table}';
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 = $("");
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]; }) }; |