Created
February 25, 2020 19:01
-
-
Save mcunha98/ff6ba44bbcbcf121ff5bfe6a33d424a1 to your computer and use it in GitHub Desktop.
append rows to html table based on array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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