I am trying to add a custom child Angular component to a GridLayout which is defined in the parent's template. The parent needs to support a few components which it knows how to build and interact with, but it must be able to place them when and where instructed within the GridLayout.
- I can specify my SampleComponent child within the template, and it displays.
- If I try adding my SampleComponent to the GridLayout using code behind, the grid.addChildChild logic gives no errors, but the component fails to display.
- If I add a Button to the grid using code behind, it displays as expected.
I understand that I am trying to load a Component vs. a Button, but my component does extend ContentView. I have seen some discussions around Builders, but they seemed to be compiling from source code, where my child components are built with my parent, and they use .xml templating. I did try looking at ComponentBuilder, but I cannot find any documentation that helps me understand how I might use it.
The most relevant 3 functions of my sample are below, initiated by the user keying in 'sample' or 'button' and clicking ADD to fire onTap().:
onTap() {
this.textField.nativeElement.dismissSoftInput();
let name = this.textField.nativeElement.text.toLowerCase();
var component: any;
console.log(`Adding component ${name}`);
switch( name ) {
case 'sample':
component = this.buildSample();
break;
case 'button':
component = this.buildButton();
break;
default:
console.error("User keyed in invalid response");
return;
}
console.log("Adding component to grid");
let grid: GridLayout = this.gridField.nativeElement;
grid.addRow( new ItemSpec( 1, GridUnitType.AUTO ));
let label = new Label();
label.text = name;
grid.addChild( label );
GridLayout.setRow( label, this.row );
grid.addChild( component );
GridLayout.setRow( component, this.row );
GridLayout.setColumn( component, 1 );
this.row++
}
private buildButton(): Button {
let button = new Button();
button.text = `Button for row${this.row}`;
return button;
}
private buildSample(): SampleComponent {
let sample = new SampleComponent();
sample.setting = 259;
return sample;
}