Angular reset dropdown values primeng












1















Hello comunity I need some help with Angular and Primeng components, the scenario is the next.



I have a model:



export class Properties {
public IdFittingConnector: number;
public IdProperty: number;
public IdGroup: number;
public OrderAparitionNumber: number;
public OrderCodePos: number;
public ConnectorDescription: string;
public PropertyDescription;
public Values: FittingPropertiesValues;
public Disabled: boolean;
public SelectedData: FittingPropertiesValues;
}


This model is stored in an array so foreach item in the array I create an select/dropdown



<div *ngFor="let item of arrFittingsCombos;  let i = index">
<h4>{{item.PropertyDescription}}</h4>
<p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
</div>


Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".



Now the big deal is the next.



I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...



if (selectedOption.IdProperty == 5) {
let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

if (selectedOption.NumberValue > diam) {
alert("Seleccion invalida, la selección es mayor que el primer diametro");
this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
this.searchBtnDisabled = true;
}
}


Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.



Thanks a lot for you time and I hope you can give me a bit of light.










share|improve this question



























    1















    Hello comunity I need some help with Angular and Primeng components, the scenario is the next.



    I have a model:



    export class Properties {
    public IdFittingConnector: number;
    public IdProperty: number;
    public IdGroup: number;
    public OrderAparitionNumber: number;
    public OrderCodePos: number;
    public ConnectorDescription: string;
    public PropertyDescription;
    public Values: FittingPropertiesValues;
    public Disabled: boolean;
    public SelectedData: FittingPropertiesValues;
    }


    This model is stored in an array so foreach item in the array I create an select/dropdown



    <div *ngFor="let item of arrFittingsCombos;  let i = index">
    <h4>{{item.PropertyDescription}}</h4>
    <p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
    </div>


    Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".



    Now the big deal is the next.



    I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...



    if (selectedOption.IdProperty == 5) {
    let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

    if (selectedOption.NumberValue > diam) {
    alert("Seleccion invalida, la selección es mayor que el primer diametro");
    this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
    this.searchBtnDisabled = true;
    }
    }


    Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
    First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.



    Thanks a lot for you time and I hope you can give me a bit of light.










    share|improve this question

























      1












      1








      1








      Hello comunity I need some help with Angular and Primeng components, the scenario is the next.



      I have a model:



      export class Properties {
      public IdFittingConnector: number;
      public IdProperty: number;
      public IdGroup: number;
      public OrderAparitionNumber: number;
      public OrderCodePos: number;
      public ConnectorDescription: string;
      public PropertyDescription;
      public Values: FittingPropertiesValues;
      public Disabled: boolean;
      public SelectedData: FittingPropertiesValues;
      }


      This model is stored in an array so foreach item in the array I create an select/dropdown



      <div *ngFor="let item of arrFittingsCombos;  let i = index">
      <h4>{{item.PropertyDescription}}</h4>
      <p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
      </div>


      Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".



      Now the big deal is the next.



      I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...



      if (selectedOption.IdProperty == 5) {
      let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

      if (selectedOption.NumberValue > diam) {
      alert("Seleccion invalida, la selección es mayor que el primer diametro");
      this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
      this.searchBtnDisabled = true;
      }
      }


      Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
      First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.



      Thanks a lot for you time and I hope you can give me a bit of light.










      share|improve this question














      Hello comunity I need some help with Angular and Primeng components, the scenario is the next.



      I have a model:



      export class Properties {
      public IdFittingConnector: number;
      public IdProperty: number;
      public IdGroup: number;
      public OrderAparitionNumber: number;
      public OrderCodePos: number;
      public ConnectorDescription: string;
      public PropertyDescription;
      public Values: FittingPropertiesValues;
      public Disabled: boolean;
      public SelectedData: FittingPropertiesValues;
      }


      This model is stored in an array so foreach item in the array I create an select/dropdown



      <div *ngFor="let item of arrFittingsCombos;  let i = index">
      <h4>{{item.PropertyDescription}}</h4>
      <p-dropdown [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
      </div>


      Like you can see guys, in the model I have a prop that store the dropdown values called "Values" and on the same model I store the selected option on the prop called "SeloectedData".



      Now the big deal is the next.



      I have a couple of validations and the problem is that I have two dropdowns but if the second value is > the first drop value I need to reset the second dropdown. I show you the logic code...



      if (selectedOption.IdProperty == 5) {
      let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

      if (selectedOption.NumberValue > diam) {
      alert("Seleccion invalida, la selección es mayor que el primer diametro");
      this.arrFittingsCombos.filter(x => x.IdProperty == 5)[0].SelectedData = null;
      this.searchBtnDisabled = true;
      }
      }


      Like you can see I'm resetting the [(ngModel)] and in effect it does but in the dropdown the showed label still exist. What I'm doing wrong? If the first time the validations are ok
      First DropDown Value < Second DropDown Value and after that I change the second dropdown value to > first DropDown value the behavior is the desired one but after that the showed label never reset anymore.



      Thanks a lot for you time and I hope you can give me a bit of light.







      javascript angular typescript primeng






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 23:35









      Fernando AguilarFernando Aguilar

      947716




      947716
























          2 Answers
          2






          active

          oldest

          votes


















          0














          why this doesn't work



          This is because you are mutation the property SelectedData on a new array.



          Calling .filter on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.



          possible solution



          I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...



          What you can do is replacing the arrFittingsCombos with your mutated array, like so:



          const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)

          newArray[0].SelectedData = null

          this.arrFittingsCombos = newArray





          share|improve this answer

































            0














            I have the solution in the HTML I've added the angular identifier:



            #dropdown_i 

            <div *ngFor="let item of arrFittingsCombos; let i = index">
            <h4>{{item.PropertyDescription}}</h4>
            <p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
            </div>


            then in the typescript code I receive the dropdown in the parameter.



            OnFittingComboChange(event, dropdown: Dropdown) {
            //TODO logic code here...
            }


            finally using a method of the dropdown called dropdown.clear() I can reset the picked option.



            if (drop.value != null &&  selectedOption.IdProperty == 5) {
            let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

            if (selectedOption.NumberValue > diam) {
            this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
            drop.clear(null);
            this.searchBtnDisabled = true;
            }
            }


            now when the condition is ok the element is cleared correctly and the behavior is the desired.






            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%2f53439091%2fangular-reset-dropdown-values-primeng%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














              why this doesn't work



              This is because you are mutation the property SelectedData on a new array.



              Calling .filter on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.



              possible solution



              I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...



              What you can do is replacing the arrFittingsCombos with your mutated array, like so:



              const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)

              newArray[0].SelectedData = null

              this.arrFittingsCombos = newArray





              share|improve this answer






























                0














                why this doesn't work



                This is because you are mutation the property SelectedData on a new array.



                Calling .filter on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.



                possible solution



                I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...



                What you can do is replacing the arrFittingsCombos with your mutated array, like so:



                const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)

                newArray[0].SelectedData = null

                this.arrFittingsCombos = newArray





                share|improve this answer




























                  0












                  0








                  0







                  why this doesn't work



                  This is because you are mutation the property SelectedData on a new array.



                  Calling .filter on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.



                  possible solution



                  I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...



                  What you can do is replacing the arrFittingsCombos with your mutated array, like so:



                  const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)

                  newArray[0].SelectedData = null

                  this.arrFittingsCombos = newArray





                  share|improve this answer















                  why this doesn't work



                  This is because you are mutation the property SelectedData on a new array.



                  Calling .filter on an array always returns a filtered copy of the array. So when you mutate this, the ngModel isn't aware of it.



                  possible solution



                  I work mostly in react so I'm not sure if mutating a property on your class instance is "accepted" by angular. But lets assume it is...



                  What you can do is replacing the arrFittingsCombos with your mutated array, like so:



                  const newArray = this.arrFittingsCombos.filter(x => x.IdProperty == 5)

                  newArray[0].SelectedData = null

                  this.arrFittingsCombos = newArray






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 23:48

























                  answered Nov 22 '18 at 23:42









                  Y. GherbiY. Gherbi

                  21228




                  21228

























                      0














                      I have the solution in the HTML I've added the angular identifier:



                      #dropdown_i 

                      <div *ngFor="let item of arrFittingsCombos; let i = index">
                      <h4>{{item.PropertyDescription}}</h4>
                      <p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
                      </div>


                      then in the typescript code I receive the dropdown in the parameter.



                      OnFittingComboChange(event, dropdown: Dropdown) {
                      //TODO logic code here...
                      }


                      finally using a method of the dropdown called dropdown.clear() I can reset the picked option.



                      if (drop.value != null &&  selectedOption.IdProperty == 5) {
                      let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

                      if (selectedOption.NumberValue > diam) {
                      this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
                      drop.clear(null);
                      this.searchBtnDisabled = true;
                      }
                      }


                      now when the condition is ok the element is cleared correctly and the behavior is the desired.






                      share|improve this answer




























                        0














                        I have the solution in the HTML I've added the angular identifier:



                        #dropdown_i 

                        <div *ngFor="let item of arrFittingsCombos; let i = index">
                        <h4>{{item.PropertyDescription}}</h4>
                        <p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
                        </div>


                        then in the typescript code I receive the dropdown in the parameter.



                        OnFittingComboChange(event, dropdown: Dropdown) {
                        //TODO logic code here...
                        }


                        finally using a method of the dropdown called dropdown.clear() I can reset the picked option.



                        if (drop.value != null &&  selectedOption.IdProperty == 5) {
                        let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

                        if (selectedOption.NumberValue > diam) {
                        this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
                        drop.clear(null);
                        this.searchBtnDisabled = true;
                        }
                        }


                        now when the condition is ok the element is cleared correctly and the behavior is the desired.






                        share|improve this answer


























                          0












                          0








                          0







                          I have the solution in the HTML I've added the angular identifier:



                          #dropdown_i 

                          <div *ngFor="let item of arrFittingsCombos; let i = index">
                          <h4>{{item.PropertyDescription}}</h4>
                          <p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
                          </div>


                          then in the typescript code I receive the dropdown in the parameter.



                          OnFittingComboChange(event, dropdown: Dropdown) {
                          //TODO logic code here...
                          }


                          finally using a method of the dropdown called dropdown.clear() I can reset the picked option.



                          if (drop.value != null &&  selectedOption.IdProperty == 5) {
                          let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

                          if (selectedOption.NumberValue > diam) {
                          this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
                          drop.clear(null);
                          this.searchBtnDisabled = true;
                          }
                          }


                          now when the condition is ok the element is cleared correctly and the behavior is the desired.






                          share|improve this answer













                          I have the solution in the HTML I've added the angular identifier:



                          #dropdown_i 

                          <div *ngFor="let item of arrFittingsCombos; let i = index">
                          <h4>{{item.PropertyDescription}}</h4>
                          <p-dropdown #dropdown_i [(disabled)]="item.Disabled" [options]="item.Values" optionLabel="IrredutibleFractionValue" [(ngModel)]="item.SelectedData" [showClear]="true" (onChange)="OnFittingComboChange($event, drop_i)" placeholder="Select Item" [style]="{'width':'100%'}"></p-dropdown>
                          </div>


                          then in the typescript code I receive the dropdown in the parameter.



                          OnFittingComboChange(event, dropdown: Dropdown) {
                          //TODO logic code here...
                          }


                          finally using a method of the dropdown called dropdown.clear() I can reset the picked option.



                          if (drop.value != null &&  selectedOption.IdProperty == 5) {
                          let diam: number = this.arrFittingsCombos.filter(x => x.IdProperty == 1)[0].SelectedData.NumberValue;

                          if (selectedOption.NumberValue > diam) {
                          this.messageService.add({ severity: 'warn', summary: 'Validación', detail: 'Seleccion invalida, la selección es mayor que el primer diametro' });
                          drop.clear(null);
                          this.searchBtnDisabled = true;
                          }
                          }


                          now when the condition is ok the element is cleared correctly and the behavior is the desired.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 23 '18 at 18:46









                          Fernando AguilarFernando Aguilar

                          947716




                          947716






























                              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%2f53439091%2fangular-reset-dropdown-values-primeng%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







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin