Primeng: dropdown in model driven forms

Created on 22 Aug 2016  路  8Comments  路  Source: primefaces/primeng

i set value for dropdown is ok in the beginning. But i click a button to change the value , the value is not change in dropdown.

Most helpful comment

my code:

this.form.controls[dfield].updateValue(value);
this.form.controls[dfield].updateValueAndValidity();

as you say, it is ok. the value is changed in dropdown. But my ide (Webstorm) has a error "Property 'updateValue' does not exist on type 'AbstractControl' " . why... ORZ....

All 8 comments

Can you show code of how your're creating the form, how you're populating the dropdown and how you're changing the value for the dropdown?

my-mock-data

newData = {
      f7: {
                type: "Buildin",
                label: "xxx",
                id: "12",
                dfield: "f7",
                value:["7"],
                options: [
                    {label: 'choose', value: '0'},
                    {label: '7', value: '7'},
                    {label: '8', value: '8'},
                    {label: '9', value: '9'},
                ]               
      }
}

dynamic-form.html

<div [formGroup]="form">
    <p-dropdown  [options]="data.options" [formControlName]="data.dfield"></p-dropdown>
</div>

dynamic-form.ts

toFormGroup(data: FormBase<any>[] ) {
        let group: any = {};

        for(let key in data){
            group[data[key]["dfield"]] = new FormControl(data[key]["value"] || "");
        }

        return new FormGroup(group);
    }

this.form = this.toFormGroup(this.newData);

dynamic-form.service

 return new FormBuildin({
            label: data.label,
            id: data.id,
            dfield: data.dfield,
            value: data.value,
            options:data.options
        });

So much for that ,all is ok. Next, i will change the value.Actually,

this.newData["f7"]["value"] = ["8"];

* i only change the value for it in newData.But the value is not change in dropdown. the label is not change in your sound code. where is my mistake?**

In order to set the selected value of any formgroup control you would need to do something like this

this.form.controls["controlname"].updateValue('newvalue'); this.form.controls["controlname"].updateValueAndValidity();

my code:

this.form.controls[dfield].updateValue(value);
this.form.controls[dfield].updateValueAndValidity();

as you say, it is ok. the value is changed in dropdown. But my ide (Webstorm) has a error "Property 'updateValue' does not exist on type 'AbstractControl' " . why... ORZ....

@wuyannan538 The webstorm IDE can be bugged, relax. In the next updates of the IDE, they will fix it

@marciioluucas i don't think so.. i more would like to believe... my fault...not ide..

@wuyannan538 I use Visual Studio (with Resharper) and it shows the same intellisense error. It's probably something wrong in the typings that the IDE is not getting correct. Don't worry about it as long as it works (it's not your fault).

Please use forum for cases like these, thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Helayxa picture Helayxa  路  3Comments

SchneMa picture SchneMa  路  3Comments

cyberrranger picture cyberrranger  路  3Comments

Helayxa picture Helayxa  路  3Comments

philly-vanilly picture philly-vanilly  路  3Comments