Jquery is required for this function.
The function above will turn and array into a html table with optional th, thead and tfoot elements. The returned data is a jquery object ready for appending or whatever you want to do.
It accepts a simple nested array as the first argument and an array of options as the second.
var data = [
['Name', 'Age', 'Email'],
['John Doe', '27', '[email protected]'],
['Jane Doe', '29', '[email protected]']
];
var table = arrayToTable(data, {
thead: true,
attrs: {class: 'table'}
})
$('body').append(table);
Produces this:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>27</td>
<td>[email protected]</td>
</tr>
<tr>
<td>Jane Doe</td>
<td>29</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
- If you use this on a site wit untruested input you may want to change the
.html()calls to.text()to the values will be escaped
Nice lib, used it in my project after adding inputs to table cells, thanks!