1

I am trying to create a page which users can use to create custom forms. I will be giving the user a drop-down menu. From that drop-down menu, user will be able to select the type of question - like text box, radio button, check box, date and etc. - and then based on that selection, I want to add that type of input into my DOM form. On submit, I want to store the value of these questions in JSON format.

Any suggestions? What is best approach to tackle this or how I can implement it?

2
  • Use delta objects quilljs.com/docs/delta Commented Sep 18, 2018 at 10:53
  • Sending forms is pretty easy. They are already sent as JSON by default. Commented Sep 18, 2018 at 11:10

3 Answers 3

1

You don't need any package, the best way to do this is like this:

<div ngbDropdown class="nav-item dropdown cursor">
    <a class="nav-link" ngbDropdownToggle>
        DropDown
    </a>
    <div ngbDropdownMenu class="dropdown-menu">
        <a class="dropdown-item" (click)="option1 = !option1">
            Form 1
        </a>
        <a class="dropdown-item" (click)="option2 = !option2">
            Form 2
        </a>
    </div>
</div>

<form #form="ngForm">
    <div class="form-option1" *ngIf="option1Selected">
        <!-- YOUR FORM 1 -->
    </div>
    <div class="form-option2" *ngIf="option2Selected">
        <!-- YOUR FORM 2 -->
    </div>
    ...

    <button type="submit"></button>
</form>

And them in your component:

option1 = "false";
option2 = "false";

The doc of *ngIf is here

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

1 Comment

Literally the sanest answer here. I can't believe how quick people are to simply link some arbitrary packages and components otherwise.
0

Have a look at ng-dynamic-forms. I've used it on a project before and it really helped creating forms dynamically.

It even provides an import / export functionality which you could use to store the form/questions including the provided answers in JSON format.

sidenote: I am in no way connected to the mentioned project.

1 Comment

My initial question would be... why? How does this project make it conveniently easier to create dynamic forms? It's already pretty easy with the basic tools Angular provides. EDIT: I think people are TOO quick at giving people just a recommendation to some absolutely unnecessary project. Before you build up a chain of dependencies, you should try to use what you're given.
0

EDIT: I feel an example like in this stackblitz shows how much control you have in terms of dynamic forms with the basic Angular utilities.

https://stackblitz.com/edit/deep-nested-reactive-form?file=app%2Fapp.component.html

Reactive Forms are your answer. There is no perfect way to do what you want. But I picked out a particular piece of an example from an old project. So, on one point I check for the type of comparators I have available due to my first choice. After that I check if I need a field that requires a simple input field or a datepicker. There are SO many ways to do this.

<div class="col-7">
    <!-- Text Input Field for most cases that don't involve time comparisons -->
    <div *ngIf="!doesRequireDateInput(i) && checkIfComparatorOptionIsEmptyOrNull(i)">
        <div class="string-input-field" formArrayName="values">
            <div [formGroupName]="j" *ngFor="let val of getValues(condition); let j = index">

                <div class="input-group">
                    <input formControlName="value" type="text" class="form-control py-0" placeholder="Search for...">
                </div>
            </div>
        </div>
    </div>

    <!-- Date Input Field -->
    <div *ngIf="doesRequireDateInput(i) && checkIfComparatorOptionIsEmptyOrNull(i)">
        <div class="string-input-field row" formArrayName="values">
            <div [formGroupName]="j" *ngFor="let val of getValues(condition); let j = index">

                <div *ngIf="j === 0" class="input-group mb-3">
                    <input type="text" formControlName="value" name="dateFrom" class="form-control py-0" style="text-align: center" [owlDateTimeTrigger]="dt3"
                     [owlDateTime]="dt3">
                    <owl-date-time [pickerType]="'calendar'" #dt3></owl-date-time>


                    <div class="input-group-append">
                        <span class="input-group-text" style="border-bottom-right-radius: 0px; border-top-right-radius: 0px" id="date-for-input">
                            <i class="fa fa-calendar" aria-hidden="true"></i>
                        </span>
                    </div>
                </div>

                <div *ngIf="j === 1" class="input-group mb-3">
                    <input type="text" formControlName="value" name="dateFrom" class="form-control py-0" style="text-align: center" [owlDateTimeTrigger]="dt3"
                     [owlDateTime]="dt3">
                    <owl-date-time [pickerType]="'calendar'" #dt3></owl-date-time>
                </div>
            </div>
        </div>
    </div>
</div>

Comments

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.