0

I have a database that stores information about dishes of a gastronomy and the customer orders.

I want to write a form that allow the user-admin to insert the data about a plate, with name, ingredients and allergens. The last two accepts multiple values.

I'm using a formGroup that contains (for now):

          <mat-card class="container"  fxLayout.xs="column" >
            <form [formGroup]="piattoFormGrp" (ngSubmit)="onSubmit()" >
              <p>
                <mat-form-field>
                    <input formControlName="nome" matInput placeholder="Nome del piatto" value="">
                    <mat-hint>Es: spaghetti allo scoglio</mat-hint>
                  </mat-form-field>
              </p>
              ... 
              <button mat-raised-button type="submit" >Inserisci</button>
            </form>
          </mat-card>

Can you suggest me which type of input I should use to store data of multiple ingredients? I have to create an array with this values in order to use it with typescript and send it to my database with php. How can I do it?

In my angular project, I could manage this information with this model:

export class Piatto{
NomeP !: string;
Tipo !: string;
Ingredienti !: Array<any>;
Allergeni !: Array<any>;}

I've already used it to store data from server and put them into a table with ngFor*.

2
  • We have no idea if ingredients exist, that user can pick from, or does the user themself type ingredients? Either way, as you are using reactive form, use a FormArray to do it. Commented May 28, 2021 at 17:13
  • @AJT82 the user type the name of ingredients, my code check for each one if the ingredient already exists. If it doesn't exist, it saves it. I could consider to give the possibility to pick on from ingredients that already exists, with a suggestion list. Commented May 28, 2021 at 17:38

1 Answer 1

1

I would suggest using the 'select' element from angular material with multiple selections.

You can set a default ingredients list as drop-down options and add an input field at the end of the options. User can use that field to enter new ingredient which doesn't exist.

You can check this example: https://stackblitz.com/edit/angular-wbgxr9?file=src/app/select-multiple-example.html

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using your example and works. However there is only one problem: the ingredient I insert in the matInput doesn't appear in the list of ingredients selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.