How to make nested model for complicated form - Angular 4 -


i have got pretty complicated angular form-configurator 3 columns:

1) categories - shows major categories

2) items - shows items selected major category

3) parameters show parameters selected item

key functionality:

  • items selected several different categories;
  • there "enable" checkbox in each item's parameters, several items selected simultaniously in each category;
  • also each item has several common parameters , have individual parameters (arbitrary number of them);

i need create model should able map json this:

{    "project_id": "1",    "name": "some name",    "category_1": [      {        "param_1": 10,        "items": [          {            "common_param_1": "echo",             "common_param_2": "simple hello",             "individual_params": {               "name": "john",               "age": "18"              // arbitrary number of other individual params here            },            "common_param_3": 10.0,            "common_param_4": 1000,            "common_param_5": 50.0          }          // arbitrary number of items here        ],        "param_2": "seq"      }    ]    // several other categories  }

current state of form ngmodel:

<form #myform="ngform" (ngsubmit)="onsubmit(myform)">    <h1>add config</h1>    name: <input type="text" name="name" ngmodel class="text_input" placeholder="item configuration name here..">    schedule: <input type="text" name="schedule" ngmodel class="text_input" placeholder="mm/dd/yyyy hh:mm:ss">      <table class="selection_table">      <tr>        <td width=15%>          <div style="height: 100%">            <b>categorys:</b>            <select name="category" size="10" ngmodel style="position: relative; width: 100%; height: 93.5%" (click)="select_category($event)">              <option *ngfor="let category of categories" value="{{category}}">{{category}}</option>            </select>          </div>        </td>        <td width=35%>          <div style="height: 100%">            <b>items:</b>            <select name="items" ngmodel size="10" style="position: relative; width: 100%; height: 70%" (click)="select_item($event)">              <option *ngfor="let item of items" value="{{item}}">{{item}}</option>            </select>            <div style="height: 24%">              <div style="float: left"><input type="checkbox">show selected items</div>              <div style="float: left; margin-top: 22px; width: 150px">param_1: <input type="text" name="param_1" ngmodel="180" size="5" value="180" style="text-align: center"></div>              <div style="float: right; margin-top: 1px;text-align: left;width: 130px">                <div><input type="radio" name="param_2">mode_1</div>                <div><input type="radio" name="param_2">mode_2</div>              </div>            </div>          </div>        </td>        <td width=50%>          <div style="height: 100%">            <b>parameters</b>            <div>              common param 3              <input type="range" name="common_param_3_slider" ngmodel="100" min="1" max="100" value="100" oninput="this.nextelementsibling.value=this.value" style="position: relative; top:7px; width:330px">              <input type="text" name="common_param_3_value" value="100" oninput="this.previouselementsibling.value=this.value" style="width: 30px; text-align: center">%            </div>            <div>              common param 5              <input type="range" name="common_param_5_slider" ngmodel="100" min="1" max="100" value="100" oninput="this.nextelementsibling.value=this.value" style="position: relative; top:7px; width:330px">              <input type="text" name="common_param_5_value" value="100" oninput="this.previouselementsibling.value=this.value" style="width: 30px; text-align: center">%            </div>              <div style="position: relative; margin-top: 30px; text-align: right; width: 250px">              <div *ngfor="let param of params" style="margin-top: 10px">                {{param | lowercase}}:                <select name="{{param}}" ngmodel style="position: relative">                  <option *ngfor="let option of options[param]" value="{{option}}">{{option}}</option>                </select>              </div>              <div style="margin-top: 10px;">common_param_5: <input type="text" name="common_param_5" ngmodel="30" size="5" value="30" style="text-align: center"></div>            </div>            <div style="position:absolute;top: 420px; left: 1000px"><input type="checkbox" (click)="select_enable($event)">enable</div>          </div>        </td>      </tr>    </table>    <div style="position: relative; left:446px">      <button type="submit">save</button>    </div>

currently i've got handler onsubmit():

onsubmit(myform: ngform) {      console.log(myform.value);  }
maps "flat" json , ngmodel created form above doesn't support nesting.

i've tried make model separate class, didn't find how make "dynamic" support arbitrary number of paramters , items in different categories.

could please share thoughts on how solve problem?

thanks! :)

i didn't spend lot of time digging through html, general answer question use ngmodelgroup. allows nest controls match nested structure.

here example:

<fieldset ngmodelgroup="address">   <label>street:</label>   <input type="text" name="street" ngmodel>    <label>zip:</label>   <input type="text" name="zip" ngmodel>    <label>city:</label>   <input type="text" name="city" ngmodel> </fieldset> 

this example blog post: https://blog.thoughtram.io/angular/2016/03/21/template-driven-forms-in-angular-2.html#ngmodelgroup-directive


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -