Skip to content

Instantly share code, notes, and snippets.

@mcunha98
Created February 25, 2020 19:01
Show Gist options
  • Select an option

  • Save mcunha98/ff6ba44bbcbcf121ff5bfe6a33d424a1 to your computer and use it in GitHub Desktop.

Select an option

Save mcunha98/ff6ba44bbcbcf121ff5bfe6a33d424a1 to your computer and use it in GitHub Desktop.
append rows to html table based on array
(function($) {
$.fn.tableappend = function(options) {
options = $.extend({
items: null,
}, options);
if (!$(this).is('table'))
{
console.error('Objeto ' + $(this).attr('id') + ' nao e tabela !');
return;
}
var thead = $(this).find('th');
var tbody = $(this).find('tbody');
var items = options.items;
items.forEach(function(item){
var tr = $('<tr>');
var index = 0;
$.each(item, function(key, prop) {
var classe = thead.eq(index).attr('class') == undefined ? '' : ' class="' + thead.eq(index).attr('class') + '"';
$('<td' + classe + '>').html(prop).appendTo(tr);
index++;
});
tbody.append(tr);
});
if (items.length > 0)
{
$(this).find('.table-is-empty').hide();
}
else
{
$(this).find('.table-is-empty').show();
}
}
}(jQuery));
//Exemplo de uso:
//$('#tabela-parcela').tableappend({items: response.data.items});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment