0

I have an Angular app with PrimeNG. I am using the PickList component like this:

<p-pickList
  [source]="source"
  [target]="target"
>
  <ng-template
    let-item
    pTemplate="item">
    {{item | json}}
  </ng-template>
</p-pickList>

<h2>source</h2>
<ul>
  <li *ngFor="let s of source">{{s|json}}</li>
</ul>

<h2>target</h2>
<ul>
  <li *ngFor="let t of target">{{t|json}}</li>
</ul>

The component itself is straightforward:

@Component({
  selector: 'app-hello',
  templateUrl: './hello.component.html',
  styleUrls: ['./hello.component.css'],
})
export class HelloComponent {
  source: string[];
  target: string[] = [];

  constructor() {
    this.source = [
      "foo",
      "bar",
      "baz",
    ];
  }
}

I do not use two-way-binding here, so how does PrimeNG updates source and target property?

On my main project source and target are @Input() properties and thus I don't want some sub component to fiddle with it. How is it possible that PrimeNG changes those values?

Apollo
  • 1,296
  • 2
  • 11
  • 24

2 Answers2

1

You could replicate the values of source and target in your HelloComponent, then use these copies for the underlying PrimeNG PickList widget. This allows you to pass updates to the HelloComponent that will be communicated down to the PrimeNG widget, but changes to those arrays within the PrimeNG widget won't impact the original input array.

I believe in your original code, the original array that's being passed as an input to HelloComponent, then passed into the PrimeNG widget, is being passed by a "copy of a reference."

private _sourceOptions: string[];
@Input set sourceOptions(options: string[]) {
    // Ensure we're not passing a reference to the array down because it may still
    // be changed. Create a new array with the same items. This can also help with
    // firing change detection in the PrimeNG widget.
    this._sourceOptions = options.slice(0);
}

get sourceOptions(): string[] {
    return this._sourceOptions;
}
<!-- Component template file -->
<p-pickList [source]="sourceOptions" [target]="target">
  <ng-template let-item pTemplate="item">
    {{item | json}}
  </ng-template>
</p-pickList>
Zulfe
  • 820
  • 7
  • 25
0

Odds are that within the component for primeNG there is an OnChange listener, and just generally speaking when something changes within one @Input it does trigger an onChange event.

As the Angular doc says (https://angular.io/api/core/OnChanges) anytime a data bound property changes it fires onChange. In this case here, target is a databound property.

Also, what do you mean by changing values? If you select foo it turns into foobar? Under the hood primeNG is not manipulating the data you passed it, it probably has its own internal store of how it displays the data for its picker list component.

SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • With changing I mean, when I move one item from left to right (see: https://primefaces.org/primeng/showcase/#/picklist) the element that I move gets removed from source and added to target. Thus, PickList changing the contents of the input properties. – Apollo Jan 24 '22 at 14:54
  • Also have your component output the JSON variant of your source array to make sure it does not change. I am going to bet that it does not, and that instead under the hood primeNG has two arrays one for what is on the left, the other for what is on the right – SomeStudent Jan 24 '22 at 14:59
  • This output was only for visualization. It doesn't matter here. But I found it: https://github.com/primefaces/primeng/blob/master/src/app/components/picklist/picklist.ts#L533 The @Input() parameters gets changed. I don't think that this is good practice. – Apollo Jan 24 '22 at 15:01
  • 1
    Not much you can do about that. You can always have a master list that contains everything and then have another array that you pass in as the source – SomeStudent Jan 24 '22 at 16:02