{"id":1231,"date":"2023-01-21T14:00:06","date_gmt":"2023-01-21T10:30:06","guid":{"rendered":"https:\/\/m-shaeri.ir\/blog\/?p=1231"},"modified":"2023-01-23T13:51:48","modified_gmt":"2023-01-23T10:21:48","slug":"flexitable-a-jquery-plugin-converting-dynamic-json-data-to-html-table","status":"publish","type":"post","link":"https:\/\/mshaeri.com\/blog\/flexitable-a-jquery-plugin-converting-dynamic-json-data-to-html-table\/","title":{"rendered":"FlexiTable- A jQuery Plugin Converting Dynamic JSON With Nested Objects To HTML Table"},"content":{"rendered":"\n<p>JSON or JavaScript Object Notation is a simple human readable data format that&nbsp;is mostly used to transfer\/store data in web applications. It might happened in a project that you deal with JSON data received from back-end API, where you need to convert the data into an easy to read HTML table to display data for users. The problem comes when the JSON contains dynamic fields and nested objects that makes it difficult to parse the data and construct a table structure showing the data. For that purpose I developed a tiny plugin that generate HTML table of JSON data loaded from server. I put the <\/p>\n\n\n\n<p> In this article, I&#8217;ll be introducing <a href=\"https:\/\/github.com\/birddevelper\/FlexiTable\" data-type=\"URL\" data-id=\"https:\/\/github.com\/birddevelper\/FlexiTable\">Flexitable<\/a>, a jQuery plugin that I developed recently to convert complex JSON data into an HTML table easily. Let&#8217;s get started.<\/p>\n\n\n\n<p>Assume we have following JSON array  :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">[\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\"Id\"&nbsp;:&nbsp;1,\n&nbsp;&nbsp;&nbsp;&nbsp;\"Name\"&nbsp;:&nbsp;\"Alex&nbsp;Jeferson\",\n&nbsp;&nbsp;&nbsp;&nbsp;\"Mathematics\"&nbsp;:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S1\"&nbsp;:&nbsp;14,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S2\"&nbsp;:&nbsp;18\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;\"Chemistry\"&nbsp;:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S1\"&nbsp;:&nbsp;19,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S2\"&nbsp;:&nbsp;17\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;\"Average\"&nbsp;:&nbsp;17\n},\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\"Id\"&nbsp;:&nbsp;2,\n&nbsp;&nbsp;&nbsp;&nbsp;\"Name\"&nbsp;:&nbsp;\"Joe Mesh\",\n&nbsp;&nbsp;&nbsp;&nbsp;\"Age\"&nbsp;:&nbsp;29,\n&nbsp;&nbsp;&nbsp;&nbsp;\"Mathematics\"&nbsp;:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S1\"&nbsp;:&nbsp;10,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S2\"&nbsp;:&nbsp;12\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;\"Chemistry\"&nbsp;:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S1\"&nbsp;:&nbsp;14,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"S2\"&nbsp;:&nbsp;18\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;\"Average\"&nbsp;:&nbsp;15\n}\n]<\/code><\/pre>\n\n\n\n<p>As you can see , records in the above JSON data contain fields <strong>Mathematics <\/strong>and <strong>Chemistry<\/strong> that their value are an Object instead of primitives. To show these kind of fields it is better to have parent and sub-header structure in order to make data easy to read and understandable. So, we need to display it in our front-end like below table :<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\">\n<table class=\"has-black-color has-text-color\" border=\"1\" width=\"90%\" style=\"text-align:center\">\n<tr style=\"background-color:#eeeeee\"><td  rowspan=\"2\">ID<\/td><td  rowspan=\"2\">Name<\/td><td colspan=\"2\">Mathematics<\/td><td colspan=\"2\">Chemistry<\/td><td rowspan=\"2\">Average<\/td><\/tr>\n<tr style=\"background-color:#eeeeee\"><td> Semester 1<\/td><td>Semester 2<\/td><td>Semester 1<\/td><td>Semester 2<\/td><\/tr>\n<tbody>\n<tr><td>5005<\/td><td>Alex Jeferson<\/td><td>14<\/td><td>18<\/td><td>19<\/td><td>17<\/td><td>17<\/td><\/tr>\n<tr><td>5016<\/td><td>Joe Mesh<\/td><td>10<\/td><td>12<\/td><td>14<\/td><td>18<\/td><td>15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>With <a href=\"https:\/\/github.com\/birddevelper\/FlexiTable\" data-type=\"URL\" data-id=\"https:\/\/github.com\/birddevelper\/FlexiTable\">Flexitable <\/a>it&#8217;s a peace of a cake to construct such tables. Let&#8217;s go to the point, the <strong>Flexitable<\/strong>.<\/p>\n\n\n\n<p>The  <strong>Flexitable <\/strong>can be called on any container element such as Div to inject the desired table into it. It also can be called as stand alone function returning the Table DOM element : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ Call Flexitable on a container to embed the table in the \n$(\"#targetDiv\").flexiTable({...});\n\n\/\/ Calling the Flexitable as a stand alone function\nvar dataTable;\n$.flexiTable({...}).then((generatedTable) =&gt; { dataTable = generatedTable;});<\/code><\/pre>\n\n\n\n<p>Flexitable accepts Json as Javascript array and also it is able to fetch the JSON from a given URL :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\/\/ calling Flexitable with a java script json array\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Sam\"}, {\"id\" : 2, \"Name\" : \"Joe\"}];\n$(\"#targetDiv\").flexiTable({\n               <strong>data <\/strong>: jsonData\n});\n\n\n\/\/ calling Flexitable with a url\nvar dataURL= \"https:\/\/pathTOUrl.com\/json\";\n$(\"#targetDiv\").flexiTable({\n               <strong>data <\/strong>: dataURL\n});<\/code><\/pre>\n\n\n\n<p>You may need the table structure to place records cell vertically like below picture :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/m-shaeri.ir\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"308\" src=\"https:\/\/m-shaeri.ir\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic-1024x308.jpg\" alt=\"\" class=\"wp-image-1245\" srcset=\"https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic-1024x308.jpg 1024w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic-300x90.jpg 300w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic-768x231.jpg 768w, https:\/\/mshaeri.com\/blog\/wp-content\/uploads\/2023\/01\/Flexitable.jquery.plugin.to_.display.nested.json_.object.dynamic.jpg 1349w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>Flexitable showing the json data with nested objects vertically<\/figcaption><\/figure>\n\n\n\n<p>Flexitable has solution for this need. put the verticalHeaders parameter in options :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Sam\"}, {\"id\" : 2, \"Name\" : \"Joe\"}];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               <strong>verticalHeaders <\/strong>: true\n});<\/code><\/pre>\n\n\n\n<p>In case that you use the plugin to retrieve data from a URL, you can set a refresh period option in millisecond to retrieve the data periodically :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Sam\"}, {\"id\" : 2, \"Name\" : \"Joe\"}];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               <strong>refreshPriod <\/strong>: 30000\n});<\/code><\/pre>\n\n\n\n<p>The titles name can be customized by mapping their path to a name :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Joe\", grade : { \"Math\" : 11, \"Sport\" : 15 }},\n                {\"id\" : 2, \"Name\" : \"Sam\", grade : { \"Math\" : 12, \"Sport\" : 18 }}];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               refreshPriod : 30000,\n               <strong>columnsTitle <\/strong>: {'id' : 'Student ID', 'grade.Math' : 'Mathematics'}\n});<\/code><\/pre>\n\n\n\n<p>For fields containing an array, you can separate array items in the cell using arraySeparator :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\n\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Joe\", absence: [\"12th may\", \"5th jun\"]},\n                {\"id\" : 2, \"Name\" : \"Sam\", absence: [\"13th Sep\", \"1st May\"]];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               refreshPriod : 30000,\n               <strong>arraySeparator<\/strong> : \", \"\n});\n<\/code><\/pre>\n\n\n\n<p>RTL or LTR direction of the table can be defined using rtl parameter :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Joe\", absence: [\"12th may\", \"5th jun\"]},\n                {\"id\" : 2, \"Name\" : \"Sam\", absence: [\"13th Sep\", \"1st May\"]];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               refreshPriod : 30000,\n               <strong>rtl<\/strong>: true\n});\n<\/code><\/pre>\n\n\n\n<p>Finally the Id and CSS class of the table :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [{\"id\" : 1, \"Name\" : \"Joe\", absence: [\"12th may\", \"5th jun\"]},\n                {\"id\" : 2, \"Name\" : \"Sam\", absence: [\"13th Sep\", \"1st May\"]];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               refreshPriod : 30000,\n               <strong>tableID<\/strong>: \"reportTable\",\n               <strong>tableCssClass <\/strong>: \"report colored\"\n});\n<\/code><\/pre>\n\n\n\n<p>If you have JSON array of primitive type without key-value pair, the table sets header title for them by indexing staring from zero (0). Then you can choose a custom title for them using their index and <strong>columnsTitle <\/strong>paramter :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">\nvar jsonData = [[\"Alex\",12,13],[\"David\",15,14],[\"Jone\",13,11]];\n$(\"#targetDiv\").flexiTable({\n               data : jsonData,\n               verticalHeaders : true,\n               refreshPriod : 30000,\n               <strong>columnsTitle <\/strong>: {'0' : 'Student Name', '1' : 'Mathematics', '2' : 'Sport'}\n});<\/code><\/pre>\n\n\n\n<p>At last but not the least, import the plugin in npm :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"><code>npm i jqueryflexitable<\/code><\/code><\/pre>\n\n\n\n<p>And in HTML page :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;script\u00a0src=\"jquery.flexitable.js\"\u00a0>&lt;\/script><\/code><\/pre>\n\n\n\n<p>That\u2019s it. Hope you find this jquery plugin useful. Thanks for reading. \u263a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON or JavaScript Object Notation is a simple human readable data format that&nbsp;is mostly used to transfer\/store data in web applications. It might happened in &hellip; <\/p>\n","protected":false},"author":1,"featured_media":1246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,1,180,74,75,94,35,110],"tags":[184,46,76,77,67,182,68],"_links":{"self":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/1231"}],"collection":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/comments?post=1231"}],"version-history":[{"count":0,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/posts\/1231\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media\/1246"}],"wp:attachment":[{"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/media?parent=1231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/categories?post=1231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mshaeri.com\/blog\/wp-json\/wp\/v2\/tags?post=1231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}