Estoy desarrollando una operación donde el usuario puede mostrar y ocultar la columna de notas haciendo clic en el botón. El ocultamiento funciona. Cuando desoculto, aparece el error Cannot find control with unspecified name attribute . ¿Pueden por favor ayudarme a resolver este problema?
Mi código:
// HTML <button type="button" (click)="showNotes('note')">show</button> <button type="button" (click)="hideNotes('note')">hide</button> // TS private beforeMonthsColumns: EditColumns[] = [ { attribute: 'accountNumber', name: 'Konto', object: null, disabledRanges: true, disabledAssignment: false }, { attribute: 'name', name: 'Bezeichnung', object: null, disabledRanges: true, disabledAssignment: false }, { attribute: 'kagNumber', name: 'KAG', object: null, disabledRanges: false, disabledAssignment: false } ]; private monthColumns: EditColumns[] = [ { attribute: '1', name: 'Jan', object: 'values', disabledRanges: true, disabledAssignment: false }, { attribute: '2', name: 'Feb', object: 'values', disabledRanges: true, disabledAssignment: false }, ]; private afterMonthsColumns: EditColumns[] = [ { attribute: 'checkJumpingAccount', name: 'Sp.-Konto', object: null, disabledRanges: false, disabledAssignment: false }, { attribute: 'note', name: 'Anmerkung', object: null, disabledRanges: false, disabledAssignment: false }, ]; // Merged arrays public displayedColumns: EditColumns[] = [ ...this.beforeMonthsColumns, ...this.monthColumns, ...this.afterMonthsColumns ]; /** * Click to show notes */ showNotes(columnName: any) { this.displayedColumns.push(columnName); } /** * Click to hide notes */ hideNotes(columnName: any) { const colIndex = this.displayedColumns.findIndex((col) => col.attribute === columnName); if (colIndex > 0) { this.displayedColumns.splice(colIndex, 1); } else { console.log('Notes are hidden! Please use the showNotes function to show the Notes column again!') } }Esto solo enviará una cadena de 'note' a this.displayedColumns
(click)="showNotes('note')" ¿Qué tal si en lugar de simplemente empalmar y tirar la columna en hideNotes() , ¿por qué no almacenar la columna 'oculta' en una matriz de hiddenColumns[] ? Y cuando se llama a showNotes , agrega la columna oculta de hiddenColumns[] de nuevo a displayColumns[ displayedColumns[] .
showNotes(columnName: any) { for (let col of this.hiddenColumns) { if (columnName == col.attribute) { this.displayedColumns.push(col); } } } hiddenColumns = []; hideNotes(columnName: any) { const colIndex = this.displayedColumns.findIndex((col) => col.attribute === columnName); if (colIndex > 0) { this.hiddenColumns.push(this.displayedColumns[colIndex]) this.displayedColumns.splice(colIndex, 1); } else { console.log('Notes are hidden! Please use the showNotes function to show the Notes column again!') } }