Created
October 26, 2021 23:00
-
-
Save linikerunk/75c9a7f4e27b09aa3819f83893c554e6 to your computer and use it in GitHub Desktop.
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
| import { scope } from './scope'; | |
| import { uniqueId } from './helpers/ui'; | |
| import { BaseField } from './base'; | |
| import { ActionModal } from './modal'; | |
| export class TabelaDinamica { | |
| public baseHead; | |
| public baseBody; | |
| public callbackRemove; | |
| private tabelaId; | |
| constructor(props) { | |
| this.tabelaId = props.tabelaId; | |
| this.baseBody = $(this.tabelaId).find('tbody tr').first().clone(); | |
| this.baseHead = $(this.tabelaId).find('thead tr').first().clone(); | |
| this.callbackRemove = props.callbackRemove; | |
| this.limpaTabela(); | |
| } | |
| private limpaTabela() { | |
| $(this.tabelaId).find('thead tr').first().remove(); | |
| $(this.tabelaId).find('tbody tr').first().remove(); | |
| } | |
| public clonaLinha() { | |
| return this.baseBody.clone(); | |
| } | |
| public addItem(dadosTabela) { | |
| const linha = this.clonaLinha(); | |
| linha.find('td').each((index, element) => { | |
| const fieldInstance = $(element).find(`drop-down, drop-down-delete, drop-down-edit, drop-down-item, link-styleguide, tag`); | |
| const dataField = $(element).data('field'); | |
| const dataUrl = $(element).data('url'); | |
| const dataUrlChave = $(element).data('url_chave'); | |
| if (fieldInstance.length) { | |
| fieldInstance.each((index, el) => this.iniciarAcoes(dadosTabela, el)); | |
| } else { | |
| let valueField = dadosTabela[dataField]; | |
| let valueUrl = dataUrl + dadosTabela[dataUrlChave]; | |
| if (valueUrl && valueField) { | |
| valueField = `<a href=${valueUrl}>${valueField}</a>` | |
| } | |
| $(element).html(valueField); | |
| } | |
| }); | |
| $(this.tabelaId).find('thead').append(this.baseHead); | |
| $(this.tabelaId).find('tbody').append(linha); | |
| } | |
| public addItens(dadosTabela) { | |
| dadosTabela.forEach(dado => this.addItem(dado)); | |
| } | |
| private iniciarAcoes(dadosTabela, field) { | |
| var fs = this; | |
| const element = $(field); | |
| const id = element.prop('id'); | |
| let instance; | |
| let attr; | |
| for (const i of Object.keys(scope)) { | |
| if (i && scope[i][id]) { | |
| instance = scope[i][id]; | |
| attr = i; | |
| break; | |
| } | |
| } | |
| const newId = uniqueId(); | |
| element.prop('id', newId); | |
| if(element.children().data('remover')){ | |
| $(element).on('click', function(){ | |
| let index = $(this).parents("tr:first").index(); | |
| if(element.children().data('is_modal')){ | |
| let modalExcluir = new ActionModal({ | |
| component: $(`#${element.children().data('id_modal')}`), | |
| submitAction: function() { | |
| fs.removerItem(index); | |
| fs.callbackRemove(index); | |
| modalExcluir.close(); | |
| } | |
| }); | |
| modalExcluir.open(); | |
| } else{ | |
| fs.removerItem(index); | |
| fs.callbackRemove(index); | |
| } | |
| }); | |
| } | |
| if (instance) { | |
| const instanceCopy: BaseField = Object.assign(Object.create(instance), instance); | |
| scope[attr][newId] = instanceCopy; | |
| instanceCopy.component = element; | |
| instanceCopy.init(); | |
| } | |
| } | |
| public removerItem(index) { | |
| $(this.tabelaId).find(`tbody tr:eq(${index})`).remove(); | |
| } | |
| public clearTable(){ | |
| $(this.tabelaId).find('tbody tr').remove(); | |
| } | |
| public prepareRemove(index) { | |
| $(this.tabelaId).find(`tbody tr:eq(${index})`).addClass('removing-row'); | |
| } | |
| public removeMutipleRows() { | |
| $(this.tabelaId).find('.removing-row').remove(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment