Git Product home page Git Product logo

ngx-recursive-form's Introduction

Ngx Recursive Form

Angular recursive form at any level based on your json input. Demo

image

Install

Note: Ngx recursive form supports only ng-zorro-antd for now. So, you need to install it beforehand.

$ npm install ng-zorro-antd
$ npm install ngx-recursive-form --save

Import the NgxRecursiveFormModule:

import { NgxRecursiveFormModule } from 'ngx-recursive-form';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [AppComponent],
  imports: [NgxRecursiveFormModule, ReactiveFormsModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

Initialize form in your component.ts file:

import { NgxRecursiveFormService } from 'ngx-recursive-form';
import { FormGroup } from '@angular/forms';

public form: FormGroup;
public formJson: any;
constructor(public ngxFormService: NgxRecursiveFormService) {}

ngOnInit() {
  this.ngxFormService.initNgxRecursiveForm(this.formJson) // JSON schema as first parameter, Default JSON value as second parameter (optional)
    .subscribe(res => {
      this.form = res.form;
      this.formJson = res.orderedFormJson;
    }, err => {
      console.log(err);
    });
}

Create tag in your component.html file:

<form nz-form [formGroup]="form" (ngSubmit)="submit()" *ngIf="form">
  <ngx-recursive-form [form]="form" [formJson]="formJson"></ngx-recursive-form>
  <button nz-button type="submit">Submit</button>
</form>

License

MIT

ngx-recursive-form's People

Contributors

amitmerchant1990 avatar hsbalar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ngx-recursive-form's Issues

Three or more level nested forms

[
    {
        "name": "dataSource",
        "label": "Label",
        "defaultValue": [
            {
                "name": "name1",
                "label": "Label",
                "mobile": 12345,
                "address": "address1",
                "verified": {
                    "status": true,
                    "entity": "entity",
                    "entityType": "entityType",
                    "comment": "comment"
                }
            },
            {
                "name": "name2",
                "label": "Label",
                "mobile": 456789,
                "address": "address2",
                "verified": {
                    "status": true,
                    "entity": "entity2",
                    "entityType": "entityType2",
                    "comment": "comment2"
                }
            }
        ],
        "type": "array",
        "parameters": [
            {
                "name": "name",
                "label": "Label",
                "defaultValue": "name1",
                "type": "string"
            },
            {
                "name": "mobile",
                "label": "Label",
                "defaultValue": 12345,
                "type": "number"
            },
            {
                "name": "address",
                "label": "Label",
                "defaultValue": "address1",
                "type": "string"
            },
            {
                "name": "verified",
                "label": "Label",
                "parameters": [
                    {
                        "name": "status",
                        "label": "Label",
                        "defaultValue": true,
                        "type": "boolean"
                    },
                    {
                        "name": "entity",
                        "label": "Label",
                        "defaultValue": "entity",
                        "type": "string"
                    },
                    {
                        "name": "entityType",
                        "label": "Label",
                        "defaultValue": "entityType",
                        "type": "string"
                    },
                    {
                        "name": "comment",
                        "label": "Label",
                        "defaultValue": "comment",
                        "type": "string"
                    }
                ],
                "type": "object"
            }
        ]
    }
]

This json schema is not rendered by ngx-recursive-form

tested at : https://www.hiteshbalar.com/ngx-recursive-form/

the original JSON is


 [{
  name: 'name1',
  mobile: 12345,
  address: 'address1',
  verified: {
    status: true,
    entity: 'entity',
    entityType: 'entityType',
    comment: 'comment',
  }
},
{
  name: 'name2',
  mobile: 456789,
  address: 'address2',
  verified: {
    status: true,
    entity: 'entity2',
    entityType: 'entityType2',
    comment: 'comment2',
  }
}]


can you please help ?

Regarding the FormArray

Hello, I have been trying to create FormArray with this library. but i am unclear how it handles array

while generating form -- does it create FormArray or FormGroup if an array is present in json?

What I tried?
https://www.hiteshbalar.com/ngx-recursive-form/

{
                "order": 3,
                "name": "language",
                "label": "Languages you know",
                "type": "array",
                "parameters": [
                    {
                        "name": "languageName",
                        "label": "Language Name",
                        "type": "string",
                        "placeholder": "Input language name"
                    },
                    {
                        "name": "experience",
                        "label": "Total Experience",
                        "type": "number",
                        "placeholder": "Total experience in year"
                    }
                ],
                "defaultValue": [
                    {
                        "languageName": "JavaScript",
                        "experience": 4
                    },
                    {
                        "languageName": "has",
                        "experience": 3
                    },
                    {
                        "languageName": "not",
                        "experience": 2
                    }
                ]
            }

This works.
But I do this in my componet files is returned as FormGroup. Instead it should be FormArray.

Please clarify.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.