Skip to content

Commit dec8f74

Browse files
smithk86softvar
authored andcommitted
added thead and tbody tags (#30)
* added thead and tbody tags * updated README
1 parent 12e5d16 commit dec8f74

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Output:
128128

129129
.. code-block:: bash
130130
131-
<table border="1"><tr><th>sample</th><td><table border="1"><tr><th>a</th><th>c</th><th>b</th></tr><tr><td>1</td><td>3</td><td>2</td></tr><tr><td>5</td><td>7</td><td>6</td></tr></table></td></tr></table>
131+
<table border="1"><tr><th>sample</th><td><table border="1"><thead><tr><th>b</th><th>c</th><th>a</th></tr></thead><tbody><tr><td>2</td><td>3</td><td>1</td></tr><tr><td>6</td><td>7</td><td>5</td></tr></tbody></table></td></tr></table>
132132
133133
======== ======= =======
134134
a c b
@@ -224,6 +224,9 @@ Contributors
224224
* Fixed issue with one-item lists not rendering correctly.
225225
* General code cleanup, fixed all naming conventions and coding standards to adhere to PEP8 conventions.
226226

227+
3. Kyle Smith: [@smithk86](https://github.com/smithk86)
228+
* Added thead and tbody tags to group header and content rows when creating a table from an array of objects.
229+
227230
Copyright and License
228231
---------------------
229232

json2html/jsonconv.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ def convert_list(self, list_input):
136136
column_headers = self.column_headers_from_list_of_dicts(list_input)
137137
if column_headers is not None:
138138
converted_output += self.table_init_markup
139+
converted_output += '<thead>'
139140
converted_output += '<tr><th>' + '</th><th>'.join(column_headers) + '</th></tr>'
141+
converted_output += '</thead>'
142+
converted_output += '<tbody>'
140143
for list_entry in list_input:
141144
converted_output += '<tr><td>'
142145
converted_output += '</td><td>'.join([self.convert_json_node(list_entry[column_header]) for column_header in
143146
column_headers])
144147
converted_output += '</td></tr>'
148+
converted_output += '</tbody>'
145149
converted_output += '</table>'
146150
return converted_output
147151

test/clubbing_keys_of_array_of_objects.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
<th>sample</th>
44
<td>
55
<table border="1">
6-
<tr>
7-
<th>name</th>
8-
<th>desc</th>
9-
<th>lang</th>
10-
</tr>
11-
<tr>
12-
<td>json2html</td>
13-
<td>coverts json 2 html table format</td>
14-
<td>python</td>
15-
</tr>
16-
<tr>
17-
<td>testing</td>
18-
<td>clubbing same keys of array of objects</td>
19-
<td>python</td>
20-
</tr>
6+
<thead>
7+
<tr>
8+
<th>name</th>
9+
<th>desc</th>
10+
<th>lang</th>
11+
</tr>
12+
</thead>
13+
<tbody>
14+
<tr>
15+
<td>json2html</td>
16+
<td>coverts json 2 html table format</td>
17+
<td>python</td>
18+
</tr>
19+
<tr>
20+
<td>testing</td>
21+
<td>clubbing same keys of array of objects</td>
22+
<td>python</td>
23+
</tr>
24+
</tbody>
2125
</table>
2226
</td>
2327
</tr>

test/clubbing_top_level.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<table border="1"><tr><th>aaa</th><th>ccc</th></tr><tr><td>1</td><td>2</td></tr><tr><td>bbb</td><td>2</td></tr></table>
1+
<table border="1"><thead><tr><th>aaa</th><th>ccc</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr><tr><td>bbb</td><td>2</td></tr></tbody></table>

test/run_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ def items(self):
133133
#clubbed with single element
134134
self.assertEqual(
135135
json2html.convert([binary_dict([1, 2], u"blübi")]),
136-
u'<table border="1"><tr><th>one</th><th>two</th></tr><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr></table>'
136+
u'<table border="1"><thead><tr><th>one</th><th>two</th></tr></thead><tbody><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr></tbody></table>'
137137
)
138138
#clubbed with two elements
139139
self.assertEqual(
140140
json2html.convert([
141141
binary_dict([1, 2], u"blübi"),
142142
binary_dict("foo", "bar")
143143
]),
144-
u'<table border="1"><tr><th>one</th><th>two</th></tr><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr><tr><td>foo</td><td>bar</td></tr></table>'
144+
u'<table border="1"><thead><tr><th>one</th><th>two</th></tr></thead><tbody><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr><tr><td>foo</td><td>bar</td></tr></tbody></table>'
145145
)
146146
#not clubbed, second element has different keys
147147
self.assertEqual(

0 commit comments

Comments
 (0)