add mat angular

Bastien OLLIER 1 year ago
parent 7b1bdfda5a
commit 098734c5cf

@ -11,26 +11,34 @@
<textarea matInput id="description" type="text" formControlName="description" required></textarea> <textarea matInput id="description" type="text" formControlName="description" required></textarea>
</mat-form-field> </mat-form-field>
</div> </div>
<div> <div>
<label for="description">Ingrédients</label>
<mat-form-field> <mat-form-field>
<label for="description">Ingrédients</label> <mat-label>Ingrédients</mat-label>
<ul> <mat-select matInput id="selectedIngredient" formControlName="selectedIngredient">
@for (ingredient of ingredientEntries; track ingredient.idIngredient) {
<li>#{{ ingredient.idIngredient }}
<input matInput [(ngModel)]="ingredient.quantity" type="number" [ngModelOptions]="{standalone: true}">
</li>
}
</ul>
</mat-form-field>
<mat-form-field>
<select matInput id="selectedIngredient" formControlName="selectedIngredient">
@for (ingredient of ingredients; track ingredient.id) { @for (ingredient of ingredients; track ingredient.id) {
<option matInput [ngValue]="ingredient.id">{{ ingredient.name }}</option> <mat-option value="{{ingredient.id}}">{{ ingredient.name }}</mat-option>
} }
</select> </mat-select>
</mat-form-field> </mat-form-field>
<button type="button" (click)="onAddIngredient()">Add</button> <button type="button" (click)="onAddIngredient()">Add</button>
<ul>
@for (ingredient of ingredientEntries; track ingredient.idIngredient) {
<li>
{{ getIngredient(ingredient.idIngredient).name }}
<mat-form-field>
<input matInput [(ngModel)]="ingredient.quantity" type="number" [ngModelOptions]="{standalone: true}">
</mat-form-field>
</li>
}
</ul>
</div> </div>
<button class="button" type="submit">Ajouter</button> <button class="button" type="submit">Ajouter</button>
</form> </form>

@ -23,6 +23,10 @@ export class RecipeAddComponent {
ingredientEntries: IngredientEntry[] = []; ingredientEntries: IngredientEntry[] = [];
ingredients: Ingredient[] = []; ingredients: Ingredient[] = [];
getIngredient(n: number): Ingredient {
return this.ingredients.find(v => v.id === n)!
}
#recipeId: number = -1; #recipeId: number = -1;
@Input() @Input()
set recipeId(recipeId: string) { set recipeId(recipeId: string) {
@ -41,7 +45,6 @@ export class RecipeAddComponent {
constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) { constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {
this.ingredients = this.recipes.getAllIngredients(); this.ingredients = this.recipes.getAllIngredients();
console.log(this.ingredients);
} }
onSubmit(): void { onSubmit(): void {
@ -65,13 +68,19 @@ export class RecipeAddComponent {
onAddIngredient(): void { onAddIngredient(): void {
const value = this.createForm.value; const value = this.createForm.value;
console.log(value);
if (!value.selectedIngredient) { if (!value.selectedIngredient) {
return; return;
} }
const id = parseInt(value.selectedIngredient!); const id = parseInt(value.selectedIngredient!);
if (this.ingredientEntries.find((ingredient) => ingredient.idIngredient === id)) { if (this.ingredientEntries.find((ingredient) => ingredient.idIngredient === id)) {
console.log("oh");
return; return;
} }
console.log("ah");
console.log(this.ingredients.find(v => v.id === id)?.name)
console.log(this.ingredients);
this.ingredientEntries.push({ this.ingredientEntries.push({
idIngredient: id, idIngredient: id,
idRecipe: -1, idRecipe: -1,

Loading…
Cancel
Save