Home > Software design >  How to add sliced string value to dataSource array in angular?
How to add sliced string value to dataSource array in angular?

Time:05-19

Sliced String

ngOnInit(): void {
    let someArray = [1, "string", false];

    for (let entry of someArray) {
        console.log(entry); // 1, "string", false
    }

    var ba =  this.QuestionOptions;
    var ab = this.QuestionOptions[0].option_description;
    ab = ab.replace("{",'');
    ab = ab.replace("}",'');
    ab=ab.replace('"row":','');
    ab=ab.replace('"columns":','');
    ab=ab.replace(/'/g, "");
    ab=ab.replace(/"/g,"");
    var aa = ab.split(",");
    var rows = aa[0].split("\\n"); 
    var cols = aa[1].split("\\n");
    
    console.log(aa);
   

Output comes in different values. And now I want to get that inputed output to this dataSource array in angular.

dataSource = [];

CodePudding user response:

Your question is not clear. First what is the output after you slice the string. second for what are you using the data-source for ? is it for angular material ex-Table view ? If yes then its best to pass an array as [datasource] = "datasource" and you need to create and interface for the datasource for different fields.

CodePudding user response:

This is an example of how you pass value to your Table for Angular Material. Check out Angular materials docs for more info on how you can further configure the Table in Angular

export interface PeriodicElement {
      name: string;
      position: number;
      weight: number;
      symbol: string;
    }
    
    const ELEMENT_DATA: PeriodicElement[] = [
      {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
      {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
    ];

    displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
      dataSource = ELEMENT_DATA;

In your HTML file

<table mat-table [dataSource]="dataSource" >
  • Related