From bb78fb82c2238af313d633ef965117565631d64e Mon Sep 17 00:00:00 2001 From: Jong-Ha Ahn Date: Mon, 17 Mar 2014 10:12:46 +0900 Subject: [PATCH] Fixed missing tbody bugs and change testcase. --- demo/index.html | 100 +++++++------ dist/jquery.jsontotable.min.js | 4 +- src/jquery.jsontotable.js | 16 ++- test/jquery.jsontotable_test.js | 243 ++++++++++++++++++++------------ 4 files changed, 226 insertions(+), 137 deletions(-) diff --git a/demo/index.html b/demo/index.html index cf60c2c..c263757 100644 --- a/demo/index.html +++ b/demo/index.html @@ -16,47 +16,65 @@
- - - + + + diff --git a/dist/jquery.jsontotable.min.js b/dist/jquery.jsontotable.min.js index 4f537dc..2ded362 100644 --- a/dist/jquery.jsontotable.min.js +++ b/dist/jquery.jsontotable.min.js @@ -1,4 +1,4 @@ -/*! jQuery JSON to Table - v1.0.1 - 2014-01-04 +/*! jQuery JSON to Table - v1.0.2 - 2014-03-17 * https://github.com/jongha/jquery-jsontotable * Copyright (c) 2014 Jong-Ha Ahn; Licensed MIT */ -!function(a){a.jsontotable=function(b,c){var d=a.extend({id:null,header:!0,className:null},c);c=a.extend(d,c);var e=b;if("string"==typeof e&&(e=a.parseJSON(e)),c.id&&e.length){var f,g,h=a("
");for(c.className&&h.addClass(c.className),a.fn.appendTr=function(b,c){var d=c?"thead":"tbody",e=c?"th":"td";g=a("");for(var f in b)g.append("<"+e+">"+b[f]+"");return a(this).append(a("<"+d+">").append(g)),this},c.header&&h.appendTr(e[0],!0),f=0;f");for(c.className&&h.addClass(c.className),a.fn.appendTr=function(b,c){var d=c?"thead":"tbody",e=c?"th":"td";g=a("");for(var f in b)"function"!=typeof b[f]&&g.append("<"+e+">"+b[f]+"");if(c)a(this).append(a("<"+d+">").append(g));else{var h=a(this).find("tbody");0===h.length&&(h=a(this).append("")),h.append(g)}return this},c.header&&h.appendTr(e[0],!0),f=c.header?1:0;f"); for(var key in rowData) { - if(typeof rowData[key] !== "function"){ /* ADDED: this wrapper to account for people bootstrapping the ECMA Array model otherwise functions get converted to strings and show up in the object list / output */ + if(typeof rowData[key] !== "function") { /* ADDED: this wrapper to account for people bootstrapping the ECMA Array model otherwise functions get converted to strings and show up in the object list / output */ row.append("<" + rowTag + ">" + rowData[key] + ""); } } - if(isHeader){ /* ADDED: IF/ELSE to eliminate repetitive TBODY tags for every row */ - $(this).append($("<" + frameTag + ">").append(row).append("")); - }else{ - $(this).find("tbody").append(row); //always append data rows to the first tbody tag + if(isHeader) { /* ADDED: IF/ELSE to eliminate repetitive TBODY tags for every row */ + $(this).append($("<" + frameTag + ">").append(row)); + + }else { + var tbody = $(this).find("tbody"); + if(tbody.length === 0) { + tbody = $(this).append(""); + } + + tbody.append(row); //always append data rows to the first tbody tag } return this; diff --git a/test/jquery.jsontotable_test.js b/test/jquery.jsontotable_test.js index f7f44c8..1691b50 100644 --- a/test/jquery.jsontotable_test.js +++ b/test/jquery.jsontotable_test.js @@ -1,91 +1,156 @@ (function($) { - test("Test1 for Array", function() { - var target = $("#test1"); - var arr = [[1, 2, 3]]; - - target.empty(); - $.jsontotable(arr, { id: "#test1", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 1); - equal(target.find("tr").length, 1); - equal(target.find("td").length, 3); - equal(target.text(), arr[0].join("")); - - arr = [[1, 2, 3], [1, 2, 3]]; - target.empty(); - $.jsontotable(arr, { id: "#test1", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 2); - equal(target.find("td").length, 6); - equal(target.text(), arr[0].join("") + arr[1].join("")); - - target.empty(); - $.jsontotable(arr, { id: "#test1" }); - equal(target.find("thead").length, 1); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 3); - equal(target.find("td").length, 6); - equal(target.text(), arr[0].join("") + arr[0].join("") + arr[1].join("")); - }); - - test("Test2 for String", function() { - var target = $("#test2"); - var str = "[[1, 2, 3]]"; - - target.empty(); - $.jsontotable(str, { id: "#test2", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 1); - equal(target.find("tr").length, 1); - equal(target.find("td").length, 3); - equal(target.text(), str.replace(/[\[\], ]/gi, "")); - - str = "[[1, 2, 3], [1, 2, 3]]"; - target.empty(); - $.jsontotable(str, { id: "#test2", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 2); - equal(target.find("td").length, 6); - equal(target.text(), str.replace(/[\[\], ]/gi, "")); - - target.empty(); - $.jsontotable(str, { id: "#test2" }); - equal(target.find("thead").length, 1); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 3); - equal(target.find("td").length, 6); - equal(target.find("tbody").text(), str.replace(/[\[\], ]/gi, "")); - }); - - test("Test3 for Dictionary", function() { - var target = $("#test3"); - var str = '[{ "a": 1, "b": 2, "c": 3 }]'; - - target.empty(); - $.jsontotable(str, { id: "#test3", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 1); - equal(target.find("tr").length, 1); - equal(target.find("td").length, 3); - equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); - - str = '[{ "a": 1, "b": 2, "c": 3 }, { "a": 1, "b": 2, "c": 3 }]'; - target.empty(); - $.jsontotable(str, { id: "#test3", header: false }); - equal(target.find("thead").length, 0); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 2); - equal(target.find("td").length, 6); - equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); - - target.empty(); - $.jsontotable(str, { id: "#test3" }); - equal(target.find("thead").length, 1); - equal(target.find("tbody").length, 2); - equal(target.find("tr").length, 3); - equal(target.find("td").length, 6); - equal(target.find("tbody").text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); - }); + test("Test1 for Array", function() { + var target = $("#test1"); + var arr = [[1, 2, 3]]; + + target.empty(); + $.jsontotable(arr, { id: "#test1", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 3); + equal(target.text(), arr[0].join("")); + + target.empty(); + $.jsontotable(arr, { id: "#test1", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 0); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 0); + equal(target.text(), arr[0].join("")); + + arr = [[1, 2, 3], [1, 2, 3]]; + target.empty(); + $.jsontotable(arr, { id: "#test1", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 6); + equal(target.text(), arr[0].join("") + arr[1].join("")); + + target.empty(); + $.jsontotable(arr, { id: "#test1", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), arr[0].join("") + arr[1].join("")); + + target.empty(); + $.jsontotable(arr, { id: "#test1" }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), arr[0].join("") + arr[1].join("")); + }); + + test("Test2 for String", function() { + var target = $("#test2"); + var str = "[[1, 2, 3]]"; + + target.empty(); + $.jsontotable(str, { id: "#test2", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\[\], ]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test2", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 0); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 0); + equal(target.text(), str.replace(/[\[\], ]/gi, "")); + + + str = "[[1, 2, 3], [1, 2, 3]]"; + target.empty(); + $.jsontotable(str, { id: "#test2", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 6); + equal(target.text(), str.replace(/[\[\], ]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test2", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\[\], ]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test2" }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\[\], ]/gi, "")); + }); + + test("Test3 for Dictionary", function() { + var target = $("#test3"); + var str = '[{ "a": 1, "b": 2, "c": 3 }]'; + + target.empty(); + $.jsontotable(str, { id: "#test3", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test3", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 0); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 1); + equal(target.find("td").length, 0); + equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); + + str = '[{ "a": 1, "b": 2, "c": 3 }, { "a": 1, "b": 2, "c": 3 }]'; + + target.empty(); + $.jsontotable(str, { id: "#test3", header: false }); + equal(target.find("thead").length, 0); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 0); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 6); + equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test3", header: true }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); + + target.empty(); + $.jsontotable(str, { id: "#test3" }); + equal(target.find("thead").length, 1); + equal(target.find("tbody").length, 1); + equal(target.find("th").length, 3); + equal(target.find("tr").length, 2); + equal(target.find("td").length, 3); + equal(target.text(), str.replace(/[\{\}\"\[\], abc:]/gi, "")); + }); }(jQuery));