-2

Here is Json schema :

{
    "_id" : ObjectId("59031d77fd5e1c0b3c005d15"),

    "resume_data" : {

     "work_experience" : [ 
            {
                "company" : "example",
                "website" : "example.com",
                "position" : "Internship",
                "highlights" : "Learn To Create API In Laravel Framework. and also Learn Angular 2 for Front end Development.",
                "project_experience" : [ 
                    {
                        "projectName" : "Fb Project",
                        "teamMember" : "5",
                        "technology" : "PHP,Laravel-5,Angular-2,MongoDb",
                        "projectPosition" : "Back-end Developer"
                    }
                ]
            }
        ]

   }
}

Here is image: enter image description here

I have reference of this answer but i don't know about nested form data. can anyone explain how to implement it.

Community
  • 1
  • 1
Manu Patel
  • 21
  • 5
  • 1
    What have you tried? This is not a free coding service ;) – AT82 May 22 '17 at 08:27
  • @AJT_82 Sir, i have tried so many times by making child component on parent but did not get success so that's why i am asking this one – Manu Patel May 22 '17 at 08:32
  • 1
    Okay, if you have tried, could you show that code and point out what is not working in code. – AT82 May 22 '17 at 08:35
  • if you try then please provide plnkr Link here.. – RïshïKêsh Kümar May 22 '17 at 09:28
  • @RïshïKêshKümar, ohk I will provide plunker link in few moments – Manu Patel May 22 '17 at 09:33
  • https://plnkr.co/edit/clTbNP7MHBbBbrUp20vr?p=preview but i want arary in array. Ex: { "name": "", "addresses": [ { "street": "", "postcode": "" }, addresses: [ { "street": "", "postcode": "" } ] ] } – Manu Patel May 22 '17 at 10:10
  • @ManuPatel That build doesn't really make sense, I mean addresses is an array, and then you want the same named array inside that array? Could you check that build you want? And I suggest you'd rename either? – AT82 May 22 '17 at 11:56
  • @AJT_82 I am Thinking.. He actually Want Another Array named 'project_experience' Inside work_experience array... according to his Json schema .. Like ( work_experience is Parent Array and project_experience is child of that array ) – RïshïKêsh Kümar May 22 '17 at 12:10
  • @AJT_82, Yes Sir addresses is an array and i want another array inside addresses array having different name ... – Manu Patel May 22 '17 at 12:11
  • @RïshïKêshKümar Exactly right ! – Manu Patel May 22 '17 at 12:19
  • @RïshïKêshKümar Ah, yeah very true, I understand now! I was just so stuck on the plunker and the comment about the plunker! :P – AT82 May 22 '17 at 13:12
  • @ManuPatel, the question and answer you linked in your question should be able to help you. I mean, you just need to create another formArray inside the first formArray. The code would be the same :) – AT82 May 22 '17 at 13:14
  • @AJT_82 Sir, I already tried so many times but i failed so please Can u implement it using plunker? I hope you will help me, Thank you Sir...Waiting for your answer – Manu Patel May 23 '17 at 05:27
  • If you create a working plunker with YOUR code, which showcases what you have tried and what is not working, then I can help. As said, we are not here to provide ready code, but help you with issues you might have. To do that you have to show us your code :) – AT82 May 23 '17 at 08:13
  • @AJT_82 Sir, This is the plunker link "https://plnkr.co/edit/vSODkb8i7o54X3fST9VF?p=preview" which is best suitable with my solution but let me know how can i set data which is fetch from database on ngOnInit() .Thanks ! – Manu Patel May 23 '17 at 11:01
  • But this plunker code does not at all match the JSON you are receiving at all? – AT82 May 23 '17 at 12:35
  • @AJT_82 Sir, Thank you so much for your reply but i can't understand. I want to set data on ngOnInit() which i'm fetching from database ..How it is possible ? – Manu Patel May 23 '17 at 12:56

2 Answers2

3

Here is your code, which sets the data you are receiving from backend, here I have stored it in a variable data.

Please notice, this is a shortened version of your form, but the basics are there, you only need to add the few missing properties in corresponding form arrays.

The build of the empty form looks is just a FormArray named work_experience matching your json structure:

this.myForm = this.fb.group({
  work_experience: this.fb.array([])
})

We add the fields when you are receiving the data, call a function called setWorkExperience in the callback when receiving data:

setWorkExperience(){
  // get the formarray
  let control = <FormArray>this.myForm.controls.work_experience;
  // iterate the array 'work_experience' from your JSON and push new formgroup with properties and the inner form array
  this.data.work_experience.forEach(x => {
    // add the rest of your properties also below
    control.push(this.fb.group({company: x.company, project_experience: this.setFormArray(x)}))
  })
}

setFormArray is called from the previous function, where we patch the data with from project_experience to the inner form array:

setFormArray(x) {
  // create local array which is returned with all the values from the 'project_experience' from your JSON
  let arr = new FormArray([])
  x.project_experience.map(y => {
    // add the rest of your properties below
    arr.push(this.fb.group({projectName: y.projectName}))
  })
  return arr;
}

The template would then look like this:

<form [formGroup]="myForm">
  <!-- Outmost array iterated -->
  <div formArrayName="work_experience">
    <div *ngFor="let a of myForm.get('work_experience').controls; let i=index">
      <h3>COMPANY {{i+1}}: </h3>
      <div formGroupName="{{i}}">
        <label>Company Name: </label>
        <input formControlName="company" /><span><button (click)="deleteCompany(i)">Delete Company</button></span><br><br>
        <!-- inner formarray iterated -->
        <div formArrayName="project_experience">
          <div *ngFor="let b of myForm.controls.work_experience.controls[i].controls.project_experience.controls; let j=index">
            <h4>PROJECT {{j+1}}</h4>
            <div formGroupName="{{j}}">
              <label>Project Name:</label>
              <input formControlName="projectName" /><span><button (click)="deleteProject(a.controls.project_experience, j)">Delete Project</button></span>
            </div>
          </div>
          <button (click)="addNewProject(a.controls.project_experience)">Add new Project</button>
        </div>
      </div>
    </div>
  </div>   
</form>

In the template you can see the buttons for add and delete of projects and companies. Adding and deleting companies are straightforward, where initCompany() returns a formGroup:

deleteCompany(index) {
  let control = <FormArray>this.myForm.controls.work_experience;
  control.removeAt(index)
}

addNewCompany() {
  let control = <FormArray>this.myForm.controls.work_experience;
  control.push(this.initCompany())
}

In the add project we pass as parameter from the template the current formArray control, to which we just push a new FormGroup:

addNewProject(control) {
  control.push(this.initProject())
}

In the delete function we pass the current formarray as well as the index of the project we want to delete:

deleteProject(control, index) {
  control.removeAt(index)
}

That should cover pretty much everything.

Plunker

AT82
  • 71,416
  • 24
  • 140
  • 167
  • 2
    **I do want to add**, that usually I do not answer (and not others) by giving such a complete solution as I did here by the amount of effort you showed, but since I was feeling kind today I did provide this code. **But please notice** this for the future and do not perhaps expect people to code for you. – AT82 May 23 '17 at 15:44
  • @AJT_82 Sir, Thanks a lot ! I really appreciated, I always keep your suggestion in mind ! – Manu Patel May 24 '17 at 05:03
0

Please Check it Out This

Plunker Here

Json Store Like This

{
  "name": "",
  "work_experience": [
    {
      "name": "asdasd",
      "project_experience": [
        {
          "Project_Name": "asdasdasd"
        },
        {
          "Project_Name": "asdasdasd"
        }
      ]
    }
  ]
}
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess May 25 '17 at 07:56