Angular 6 - Display of form components based on section of radiobutton










0















Scenario:



  • I am working on a form using the angular material and angular 6

  • I would want to display the relevant text boxes as per the type choosen from the radio buttons which give them.

Code i have used for radio button



<div class="form-group m-form__group row">
<div class="m-radio-list">
<mat-radio-group>
<p>
<mat-radio-button value="newBusiness">New</mat-radio-button>
<mat-radio-button value="renewal">Renewal</mat-radio-button>
<mat-radio-button value="rollover">Old</mat-radio-button>
</p>
</mat-radio-group>
</div>
</div>


Todo :



  • So based on these 3 types, once a user selects a specific type for eg the new i want to display to him text boxes for name and number ,
    if another value then other text boxes.









share|improve this question




























    0















    Scenario:



    • I am working on a form using the angular material and angular 6

    • I would want to display the relevant text boxes as per the type choosen from the radio buttons which give them.

    Code i have used for radio button



    <div class="form-group m-form__group row">
    <div class="m-radio-list">
    <mat-radio-group>
    <p>
    <mat-radio-button value="newBusiness">New</mat-radio-button>
    <mat-radio-button value="renewal">Renewal</mat-radio-button>
    <mat-radio-button value="rollover">Old</mat-radio-button>
    </p>
    </mat-radio-group>
    </div>
    </div>


    Todo :



    • So based on these 3 types, once a user selects a specific type for eg the new i want to display to him text boxes for name and number ,
      if another value then other text boxes.









    share|improve this question


























      0












      0








      0








      Scenario:



      • I am working on a form using the angular material and angular 6

      • I would want to display the relevant text boxes as per the type choosen from the radio buttons which give them.

      Code i have used for radio button



      <div class="form-group m-form__group row">
      <div class="m-radio-list">
      <mat-radio-group>
      <p>
      <mat-radio-button value="newBusiness">New</mat-radio-button>
      <mat-radio-button value="renewal">Renewal</mat-radio-button>
      <mat-radio-button value="rollover">Old</mat-radio-button>
      </p>
      </mat-radio-group>
      </div>
      </div>


      Todo :



      • So based on these 3 types, once a user selects a specific type for eg the new i want to display to him text boxes for name and number ,
        if another value then other text boxes.









      share|improve this question
















      Scenario:



      • I am working on a form using the angular material and angular 6

      • I would want to display the relevant text boxes as per the type choosen from the radio buttons which give them.

      Code i have used for radio button



      <div class="form-group m-form__group row">
      <div class="m-radio-list">
      <mat-radio-group>
      <p>
      <mat-radio-button value="newBusiness">New</mat-radio-button>
      <mat-radio-button value="renewal">Renewal</mat-radio-button>
      <mat-radio-button value="rollover">Old</mat-radio-button>
      </p>
      </mat-radio-group>
      </div>
      </div>


      Todo :



      • So based on these 3 types, once a user selects a specific type for eg the new i want to display to him text boxes for name and number ,
        if another value then other text boxes.






      angular-material angular6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 10:45









      Abhishek Kumar

      1,053216




      1,053216










      asked Nov 13 '18 at 8:03









      anglrlearneranglrlearner

      205




      205






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Demo with Bydefault a checkbox selected



          Application Code : https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts

          (in this code, i have selected the 3rd checkbox bydefault, you can select the first if you want)



          Approach :



          • Make 2 div container, one for radio-buttons, other for showing the corresponding inputs container according to selected radio-button.

          • You can use change event like (change)="changeComboo($event)",
            i have just used the ngModel to bind value to a variable like [(ngModel)]="favoriteSeason".


          Update 1 (select bydefault first checkbox) :



          • As mat-radio-group is binded using [(ngModel)]="favoriteSeason",
            So in function ngOnint()- we can assign this favoriteSeason variable a default value through this.favoriteSeason = this.seasons[0]; (if we want to select the first checkbox).





          share|improve this answer

























          • thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:50











          • thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:56











          • @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

            – Abhishek Kumar
            Nov 15 '18 at 7:07











          • thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

            – anglrlearner
            Nov 16 '18 at 7:35











          • @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

            – Abhishek Kumar
            Nov 16 '18 at 8:22



















          0















          Try like this




          <div class="radio"> 
          <label>
          <input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
          New
          </label>
          </div>
          <div class="radio">
          <label>
          <input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
          Renewal
          </label>
          </div>

          <div *ngIf="isSelected('New')" >
          <input type="text"/> New radio button selected
          </div>

          <div *ngIf="isSelected('Renewal')">
          <input type="text"/> Renewal radio button selected
          </div>



          in your component.ts




          private selectedLink: string="New"; 

          setradio(e: string): void
          this.selectedLink = e;


          isSelected(name: string): boolean

          if (!this.selectedLink) // if no radio button is selected, always return false so every nothing is shown
          return false;


          return (this.selectedLink === name); // if current radio button is selected, return true, else return false






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276376%2fangular-6-display-of-form-components-based-on-section-of-radiobutton%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Demo with Bydefault a checkbox selected



            Application Code : https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts

            (in this code, i have selected the 3rd checkbox bydefault, you can select the first if you want)



            Approach :



            • Make 2 div container, one for radio-buttons, other for showing the corresponding inputs container according to selected radio-button.

            • You can use change event like (change)="changeComboo($event)",
              i have just used the ngModel to bind value to a variable like [(ngModel)]="favoriteSeason".


            Update 1 (select bydefault first checkbox) :



            • As mat-radio-group is binded using [(ngModel)]="favoriteSeason",
              So in function ngOnint()- we can assign this favoriteSeason variable a default value through this.favoriteSeason = this.seasons[0]; (if we want to select the first checkbox).





            share|improve this answer

























            • thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:50











            • thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:56











            • @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

              – Abhishek Kumar
              Nov 15 '18 at 7:07











            • thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

              – anglrlearner
              Nov 16 '18 at 7:35











            • @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

              – Abhishek Kumar
              Nov 16 '18 at 8:22
















            0














            Demo with Bydefault a checkbox selected



            Application Code : https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts

            (in this code, i have selected the 3rd checkbox bydefault, you can select the first if you want)



            Approach :



            • Make 2 div container, one for radio-buttons, other for showing the corresponding inputs container according to selected radio-button.

            • You can use change event like (change)="changeComboo($event)",
              i have just used the ngModel to bind value to a variable like [(ngModel)]="favoriteSeason".


            Update 1 (select bydefault first checkbox) :



            • As mat-radio-group is binded using [(ngModel)]="favoriteSeason",
              So in function ngOnint()- we can assign this favoriteSeason variable a default value through this.favoriteSeason = this.seasons[0]; (if we want to select the first checkbox).





            share|improve this answer

























            • thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:50











            • thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:56











            • @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

              – Abhishek Kumar
              Nov 15 '18 at 7:07











            • thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

              – anglrlearner
              Nov 16 '18 at 7:35











            • @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

              – Abhishek Kumar
              Nov 16 '18 at 8:22














            0












            0








            0







            Demo with Bydefault a checkbox selected



            Application Code : https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts

            (in this code, i have selected the 3rd checkbox bydefault, you can select the first if you want)



            Approach :



            • Make 2 div container, one for radio-buttons, other for showing the corresponding inputs container according to selected radio-button.

            • You can use change event like (change)="changeComboo($event)",
              i have just used the ngModel to bind value to a variable like [(ngModel)]="favoriteSeason".


            Update 1 (select bydefault first checkbox) :



            • As mat-radio-group is binded using [(ngModel)]="favoriteSeason",
              So in function ngOnint()- we can assign this favoriteSeason variable a default value through this.favoriteSeason = this.seasons[0]; (if we want to select the first checkbox).





            share|improve this answer















            Demo with Bydefault a checkbox selected



            Application Code : https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts

            (in this code, i have selected the 3rd checkbox bydefault, you can select the first if you want)



            Approach :



            • Make 2 div container, one for radio-buttons, other for showing the corresponding inputs container according to selected radio-button.

            • You can use change event like (change)="changeComboo($event)",
              i have just used the ngModel to bind value to a variable like [(ngModel)]="favoriteSeason".


            Update 1 (select bydefault first checkbox) :



            • As mat-radio-group is binded using [(ngModel)]="favoriteSeason",
              So in function ngOnint()- we can assign this favoriteSeason variable a default value through this.favoriteSeason = this.seasons[0]; (if we want to select the first checkbox).






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 8:21

























            answered Nov 13 '18 at 8:32









            Abhishek KumarAbhishek Kumar

            1,053216




            1,053216












            • thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:50











            • thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:56











            • @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

              – Abhishek Kumar
              Nov 15 '18 at 7:07











            • thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

              – anglrlearner
              Nov 16 '18 at 7:35











            • @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

              – Abhishek Kumar
              Nov 16 '18 at 8:22


















            • thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:50











            • thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

              – anglrlearner
              Nov 15 '18 at 6:56











            • @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

              – Abhishek Kumar
              Nov 15 '18 at 7:07











            • thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

              – anglrlearner
              Nov 16 '18 at 7:35











            • @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

              – Abhishek Kumar
              Nov 16 '18 at 8:22

















            thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:50





            thanks, also another assistance, if i would want to keep the first selection always open, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:50













            thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:56





            thanks, also another assistance, if i would want to keep the first selection open as default for everytime one loads the app, how could i manage that.

            – anglrlearner
            Nov 15 '18 at 6:56













            @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

            – Abhishek Kumar
            Nov 15 '18 at 7:07





            @anglrlearner please see the updated links in the answer, and if its helpful please upvote the answer. Suggest any changes if any.

            – Abhishek Kumar
            Nov 15 '18 at 7:07













            thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

            – anglrlearner
            Nov 16 '18 at 7:35





            thanks, i suppose u have mistakenly added another demo code with the logic of Fetch Data from random Api, and change paginator count, could ypu please check.

            – anglrlearner
            Nov 16 '18 at 7:35













            @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

            – Abhishek Kumar
            Nov 16 '18 at 8:22






            @anglrlearner i am so sorry, i put the wrong link. Please see the updated link.

            – Abhishek Kumar
            Nov 16 '18 at 8:22














            0















            Try like this




            <div class="radio"> 
            <label>
            <input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
            New
            </label>
            </div>
            <div class="radio">
            <label>
            <input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
            Renewal
            </label>
            </div>

            <div *ngIf="isSelected('New')" >
            <input type="text"/> New radio button selected
            </div>

            <div *ngIf="isSelected('Renewal')">
            <input type="text"/> Renewal radio button selected
            </div>



            in your component.ts




            private selectedLink: string="New"; 

            setradio(e: string): void
            this.selectedLink = e;


            isSelected(name: string): boolean

            if (!this.selectedLink) // if no radio button is selected, always return false so every nothing is shown
            return false;


            return (this.selectedLink === name); // if current radio button is selected, return true, else return false






            share|improve this answer





























              0















              Try like this




              <div class="radio"> 
              <label>
              <input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
              New
              </label>
              </div>
              <div class="radio">
              <label>
              <input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
              Renewal
              </label>
              </div>

              <div *ngIf="isSelected('New')" >
              <input type="text"/> New radio button selected
              </div>

              <div *ngIf="isSelected('Renewal')">
              <input type="text"/> Renewal radio button selected
              </div>



              in your component.ts




              private selectedLink: string="New"; 

              setradio(e: string): void
              this.selectedLink = e;


              isSelected(name: string): boolean

              if (!this.selectedLink) // if no radio button is selected, always return false so every nothing is shown
              return false;


              return (this.selectedLink === name); // if current radio button is selected, return true, else return false






              share|improve this answer



























                0












                0








                0








                Try like this




                <div class="radio"> 
                <label>
                <input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
                New
                </label>
                </div>
                <div class="radio">
                <label>
                <input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
                Renewal
                </label>
                </div>

                <div *ngIf="isSelected('New')" >
                <input type="text"/> New radio button selected
                </div>

                <div *ngIf="isSelected('Renewal')">
                <input type="text"/> Renewal radio button selected
                </div>



                in your component.ts




                private selectedLink: string="New"; 

                setradio(e: string): void
                this.selectedLink = e;


                isSelected(name: string): boolean

                if (!this.selectedLink) // if no radio button is selected, always return false so every nothing is shown
                return false;


                return (this.selectedLink === name); // if current radio button is selected, return true, else return false






                share|improve this answer
















                Try like this




                <div class="radio"> 
                <label>
                <input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
                New
                </label>
                </div>
                <div class="radio">
                <label>
                <input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
                Renewal
                </label>
                </div>

                <div *ngIf="isSelected('New')" >
                <input type="text"/> New radio button selected
                </div>

                <div *ngIf="isSelected('Renewal')">
                <input type="text"/> Renewal radio button selected
                </div>



                in your component.ts




                private selectedLink: string="New"; 

                setradio(e: string): void
                this.selectedLink = e;


                isSelected(name: string): boolean

                if (!this.selectedLink) // if no radio button is selected, always return false so every nothing is shown
                return false;


                return (this.selectedLink === name); // if current radio button is selected, return true, else return false







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 9:24









                Rafał Czabaj

                442414




                442414










                answered Nov 13 '18 at 8:22









                chethuchethu

                16011




                16011



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276376%2fangular-6-display-of-form-components-based-on-section-of-radiobutton%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto