I'm trying to add as many rows as users I have in my database to a table. I'm getting the users' info from the backend via ajax request, then when the response (JSON) arrive my code pass it to I silly template I'd created using underscore.js.
After underscore rendered the template this is what i got:
<tr data-id="29">
<td>email@themail.ma</td>
<td>
<ul style="display:inline-block; padding-left:0; margin-bottom:0">
<li style="display:inline-block;"><a href="#"></a></li>
</ul>
</td>
<td>Activo</td>
<td>No caduca</td>
<td>
<span data-placement="left" data-toggle="tooltip" title="Eliminar usuario">
<a class="icon-trash" href="#" role="button"
data-toggle="modal"
data-target="#js-confirm-modal"
data-action="js-del-user"
data-msg="Desea eliminar el usuario?"></a>
</span>
<span data-placement="left" data-toggle="tooltip" title="Cambiar contraseña">
<a class="icon-key" href="#" role="button"
data-toggle="modal"
data-target="#js-confirm-modal"
data-action="js-chg-pass"
data-msg="Cambiar contraseña del usuario?"></a>
</span>
<span data-placement="left" data-toggle="tooltip" title="Bloquear usuario">
<a class="icon-lock" href="#" role="button"
data-toggle="modal"
data-target="#js-confirm-modal"
data-action="js-block-user"
data-msg="Desea bloquear el usuario?"></a>
</span>
<span data-placement="left" data-toggle="tooltip" title="Desbloquear usuario">
<a class="icon-lock-open" href="#" role="button"
data-toggle="modal"
data-target="#js-confirm-modal"
data-action="js-unblock-user"
data-msg="Desea desbloquear el usuario?"></a>
</span>
</td>
</tr>
So far so good, but when I do something like this:
tbody.innerHTML = html;
// or
let parseHTML = function(str) {
var tmp = document.implementation.createHTMLDocument();
tmp.body.innerHTML = str;
return tmp.body.children;
};
parseHTML(html); // and then adding the returned codes to my tbody
It just looses the html table format (td, tr tags, etc)
Thanks to @jaredgorski I realized why the html code was losing the formating, so this is the solution best fit my problem so far. Any suggestion would be appreciated.
function convert2Element (strHTML) {
let table = document.createElement('table');
table.appendChild(document.createElement('tbody'));
table.querySelector('tbody').innerHTML = strHTML;
return table.querySelector('tbody tr');
}
Then I can append the returned value like and treat it like the object it is.