0

I have a generic method to get data from back-end and pass it to an array

Here is the code of the method, where I pass method for getting data, array and default value, where I need to write the first element id

    interface fillDropdownOptionsInterface {
    method: Observable<ListResultDtoOfDropdownOptionDto>;
    items: SelectItem[];
    defaultvalue?: number;
}
    public static fillDropdownOptionsObj(ref: fillDropdownOptionsInterface): void {
        ref.method.subscribe(r =>
            r.items.forEach(value => {
                ref.items.push({
                    label: value.name,
                    value: value.id,
                });
                ref.defaultvalue = ref.items[0].value;
            })
        );
    }

here is how I call it

  getQuoteTypes(): void {
    const obj = {
        method: this._quoteService.getQuoteTypesOptions(),
        items: this.quoteTypes,
        defaultvalue: this.quote.quoteTypeId,
    };
    DropdownHelpers.fillDropdownOptionsObj(obj);
}

and then I call it in the OnInit method.

My problem that when I want to get quoteTypeId at an HTML template like this, I got nothing.

<div class="col-4">
                        <p-dropdown
                            [disabled]="!quoteTypes"
                            [options]="quoteTypes"
                            autoWidth="false"
                            styleClass="w-100"
                            name="quoteType"
                            [autoWidth]="true"
                            [(ngModel)]="quote.quoteTypeId"
                        ></p-dropdown>
                    </div>
                    {{ quote.quoteTypeId }}

I was trying to pass a pre-initialized value to pass it to function, and after this function need to rewrite it and pop outside rewrote value. Like this

this.quote.quoteTypeId = 99;

But on view, I still see 99.

So I guess the problem in this - function, not pop outside rewrote value (I mean this.quote.quoteTypeId). How I can fix this?

1
  • when you call to fillDropdownOptionsObj, you send an object that has no value outside the function getQuoteTypes. The object makes reference to this.quoteTypes (that is an array, OK) but make reference to this.quote.quoteTypeId, that is a string (or number), so only change obj.defaultvalue, NOT this.quote.quoteTypeId, you must, after call the function fillDropdownOptionsObj, equal this.quote.quoteTypeId==obj.defaultvalue Commented Jun 24, 2019 at 6:33

1 Answer 1

1

In your helperDropdownHelpers.fillDropdownOptionsObj(obj) you overwrite the value of default but it seems that you wanted to assign the the the value to quote.quoteTypeId.

Maybe you can try the following:

<div class="col-4">
  <p-dropdown
     [disabled]="!obj.items"
     [options]="obj.items"
     autoWidth="false"
     styleClass="w-100"
     name="quoteType"
     [autoWidth]="true"
     [(ngModel)]="obj.defaultvalue">
 </p-dropdown>
</div>
{{ quote.quoteTypeId }}

I changed every place referencing to quote to reference to obj instead.

Second suggestion:

Change this.quote.quoteTypeId to this.quote:

 getQuoteTypes(): void {
    const obj = {
        method: this._quoteService.getQuoteTypesOptions(),
        items: this.quoteTypes,
        defaultvalue: this.quote,
    };
    DropdownHelpers.fillDropdownOptionsObj(obj);
}

And then, in your handler, assign your id like so ref.defaultvalue.quoteTypeId = ref.items[0].value:

public static fillDropdownOptionsObj(ref: fillDropdownOptionsInterface): void {
        ref.method.subscribe(r =>
            r.items.forEach(value => {
                ref.items.push({
                    label: value.name,
                    value: value.id,
                });
                ref.defaultvalue.quoteTypeId = ref.items[0].value;
            })
        );
    }
Sign up to request clarification or add additional context in comments.

4 Comments

obj is not visible in component html
made an edit to the answer adding a "second suggestion"
But it good for one field quoteTypeId. I need more generic stuff, so I can pass quoteTypeId or customerTypeId , or smth else, at your approach, I need to write 2 functions. One for quoteTypeId and other for customerTypeId , etc.
you can set another property of ref.defaultvalue, i.e.: ref.defaultvalue.customerTypeId. In any case, the provided code is very specific so this is pretty much as far as i can assist. You won't get a good answer with a partial code example...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.