Table2csv: Convert tables to CSV data
Parameters: A JSON object
{
' Repeatchar ': ' Split cell padding character ',//default is NULL to populate cell values in each split cell, if given string is populated with a given string
' Callback ': ' Callback function '//passed in two parameters (Csv,data), respectively, CSV string and data two-dimensional array
}
(function ($) {$.fn.table2csv=function (option) {if (option==null) option={}; if (option.repeatchar== ") Option.repeatchar= "-";//Calculate row number var rows=0;//row var cols=0;//column this.find ("tr"). each (function () {Rows+=1;var tr=$ (this); var n=tr.find ("td,th"). Length;cols=math.max (Cols,n);}); /document.title= ("Row/column:" +rows+ "/" +cols);//empty array prepare var data=new array (); for (Var i=0;i<rows;i++) {var line=new array (); for (Var j=0;j<cols;j++) Line.push (");d Ata.push (line);} Data is populated with Var i=0;//current line coordinates this.find ("tr"). each (function () {var tr=$ (this); var j=0;//the current column coordinates tr.find ("td,th"). each (function () {var td=$ (this), Var colspan=1;var rowspan=1;var value=td.text (), if (Td.attr ("colspan")!=null) Colspan=parseint ( Td.attr ("colspan")), if (Td.attr ("rowspan")!=null) Rowspan=parseint (td.attr ("rowspan"));//Locate the first cell in the CSV array for which there is no data for ( var p=0;p<data[i].length;p++) {if (data[i][p]== ') {j=p; break; }}data[i][j]=value;//padding value if (colspan+rowspan==2) return; If the cell is not merged, return and continue each//the cell range var Fic=i+rowspan; Alert (value+i+ ":" +j); for (Var fi=i;fi<fic;fi++){var fjc=j+colspan;//alert ("+j+" \ r \ n "+fic+": "+FJC"), for (Var fj=j;fj<fjc;fj++) {if ((fj==j) && (fi==i)) Continue data[fi][fj]= (option.repeatchar==null)? Value:option.repeatChar; Alert (Test (data)); }}}); i++;}); /fill complete//Generate Debug data var str= ""; for (var i in data) {Str+=data[i].join (","); str+= "\ r \ n";} if (Option.callback!=null) Option.callback (str,data);};/ $.fn.csv2table=function (param) {var isarray=param instanceof Array;var data=null;if (IsArray)//array directly with Data=param;else{//csv string is converted to array data=new (); var lines=param.split (' \ r \ n '); for (Var i=0;i< lines.length;i++) {var cells=lines[i].split (', ');d ata.push (cells);}} var table= "<table border= ' 1 >"; for (var i=0;i<data.length;i++) {table+= "<tr>"; var cells=data[i];for ( var j=0;j<cells.length;j++) table+= ("<td>" +cells[j]+ "</td>") table+= "</tr>";} table+= "</table>" table+= (IsArray? ") <!--Arrays--":" <!--string--"); this.html (table);}}) (JQuery);
Test the interface (convert the contents of the table to CSV and then the CSV as the second table):
JQuery table2csv Plugin (table to CSV) perfectly supports colspan and rowspan