//json to array(印花娛樂代理人專用) (function ($) { $.fn.tabletojson = function (options) { var defaults = { headers: null, attribHeaders: null, returnElement: null, complete: null }; var options = $.extend(defaults, options); var selector = this; var jsonRowItem = ""; var jsonItem = new Array(); var jsonRow = new Array(); var heads = []; var rowCounter = 1; var comma = ","; var json = ""; if (options.headers != null) { options.headers = options.headers.split(' ').join(''); heads = options.headers.split(","); } var rows = $(":not(tfoot) > tr", this).length; $(":not(tfoot) > tr", this).each(function (i, tr) { jsonRowItem = ""; if (this.parentNode.tagName == "TFOOT") { return; } if (this.parentNode.tagName == "THEAD") { if (options.headers == null) { $('th', tr).each(function (i, th) { heads[heads.length] = $(th).html(); }); } }else { if(i>1){ if (options.attribHeaders != null) { var h = eval("(" + options.attribHeaders + ")"); for (z in h) { heads[heads.length] = h[z]; } } rowCounter++ var headCounter = 0; jsonRowItem = "{"; jsonItem.length = 0; $('td', tr).each(function (i, td) { if( i>0) { var re = / /gi; var v = $(td).html().replace(re, '') jsonItem[jsonItem.length] = "\"" + heads[headCounter] + "\":\"" + v + "\""; headCounter++; } }); if (options.attribHeaders != null) { for (z in h) { jsonItem[jsonItem.length] = "\"" + heads[headCounter] + "\":\"" + tr[z] + "\""; headCounter++; } } jsonRowItem += jsonItem.join(","); jsonRowItem += "}"; jsonRow[jsonRow.length] = jsonRowItem; } } }); json += "[" + jsonRow.join(",") + "]"; if (options.complete != null) { options.complete(json); } if (options.returnElement == null){ return json; }else { $(options.returnElement).val(json); return this; } } })(jQuery);