I am new to angular and i am trying to make a dynamic table to display data.
Right i have it (somewhat working) with static data.
I was inspired from this example here: https://stackblitz.com/angular/lrpjroljdly?embed=1&file=app/table-selection-example.html
const COLUMN_DATA: IClusterColumnOrient[] = [...Array(10).keys()].map((_, index) =>({
columnName: (Math.random() + 1).toString(36).substring(7),
schemaName: (Math.random() + 1).toString(36).substring(7),
columnType: (Math.random() + 1).toString(36).substring(7),
id: (Math.random() + 1).toString(36).substring(7),
tableName: (Math.random() + 1).toString(36).substring(7),
databaseName: (Math.random() + 1).toString(36).substring(7),
databaseId: (Math.random() + 1).toString(36).substring(7)
}))
@Component({
selector: 'column-search',
templateUrl: './column-search.component.html',
styleUrls: ['./column-search.component.scss'],
})
export class ColumnSearchComponent implements OnInit{
displayedColumns: string[] = ['select', 'columnName', 'columnType', 'tableName', 'schemaName'];
selection = new SelectionModel<IClusterColumnOrient>(true, []);
@ViewChild('searchInput', { static: true }) searchInput: ElementRef;
columnsData: IClusterColumnOrient[]
isSearching: boolean
columns: string[]
dataSource = new MatTableDataSource<IClusterColumnOrient>(COLUMN_DATA);
This works fine, but when i want to use dynamic data i initialize an empty array, and what for user input to query my backend dynamically.
constructor(private httpClient: HttpClient){
this.isSearching = false
this.columnsData = []
//Hardcoded as of now, we can import and parse from backend in the future
}
ngOnInit(){
console.log(this.columnsData)
fromEvent(this.searchInput.nativeElement, 'keyup').pipe(
//Find type for js event
map((event: any) =>{
return event.target.value
}),
debounceTime(700),
distinctUntilChanged(),
.....
When i try to substitute the COLUMN_DATA with this.columnData i get an error like such:
Property 'columnsData' is used before its initialization.
How can i substitute this with dynamic data?
columnsData: IClusterColumnOrient[] = [];. I assume the error comes from TSlint, ESlint or something similar, which ensures you don't use properties that were not initialized. The code, if compiled, would work correctly, but the strict checks ensure you don't make any mistakes. You can probably disable it somewhere in your linting options.