primeng icon indicating copy to clipboard operation
primeng copied to clipboard

p-splitter: dynamic update of panelSizes should trigger rerender

Open Zukzuk opened this issue 2 years ago • 3 comments

Describe the feature you would like to see added

The primeNG Splitter component offers the 'panelSizes' property. The value unfortunately is only evaluated on initialization of the component. I would like to request a change so I can dynamically resize the template by updating the value of the panelSizes property.

Is your feature request related to a problem?

My problem is that I am about to offer a feature in which I want to be able to dynamically choose the size of my panel layout. Right now I can't do that with the Splitter, although I feel like I should be able to.

Describe the solution you'd like

import { Component } from '@angular/core';
@Component({
    selector: 'splitter-horizontal-demo',
    template: '
    <p-button (click)="toggleSecondary()">Toggle Secondary</p-button>
    <p-splitter [panelSizes]="panelSizes" [minSizes]="minSizes">
        <ng-template pTemplate>
            <div>Panel 1</div>
        </ng-template>
        <ng-template pTemplate>
            <div *ngIf="isSecondaryActive">Panel 2</div>
        </ng-template>
    </p-splitter>
    '
})
export class SplitterHorizontalDemo {
  isSecondaryActive!: boolean;
  panelSizes: number[] = [100, 0];
  minSizes: number[] = [50, 0]

  toggleSecondary() {
    this.isSecondaryActive = !this.isSecondaryActive;
    if (this.isSecondaryActive) ? [50, 50] : [100, 0]
  }
}

Describe alternatives you have considered

Creating my own Splitter in html/css/ts. I don't like this option since I have a complete component lib called PrimeNG ;)

Additional context

No response

Zukzuk avatar Jul 07 '23 12:07 Zukzuk

Hi,

Just a guess, this enhancement might be done by using Angular Signals easily.

cetincakiroglu avatar Jul 10 '23 08:07 cetincakiroglu

I used ngFor to create panels by iterating an array, Now I want to delete panels dynamically. I tried to delete it from this array, but the UI is not updated. However, Dynamic Addition is working.

AnilBabu-TMMC avatar Jan 04 '24 12:01 AnilBabu-TMMC

I was looking for this feature also... guess it's not possible then.

nchaigne avatar May 11 '24 05:05 nchaigne

Sad to see this is not already a feature. Did someone of you find another way to trigger a rerender?

chrisbrugger avatar Jun 01 '24 19:06 chrisbrugger

@chrisbrugger Try to set a ViewChild reference on the p-splitter and call detectChanges() on the ChangeDetectorRef. For me it worked, I know it's not the best/right way, but it does the job :)

@ViewChild('splitter') splitterRef: Splitter; ...perform addition/deletion on the pnels array of the Splitter... setTimeout(() => { this.splitterRef.cd.detectChanges(); });

MrLoba81 avatar Sep 09 '24 14:09 MrLoba81

Unfortunately, the @ViewChild approach with calling detectChanges on the splitters didn't work for me. For anyone else that might come across this issue, I simply bound a boolean variable to an *ngIf in the splitter and toggled it as updates were coming in using setTimeout. Hope this helps!

Template:

<p-splitter *ngIf="splitterRerenderHack" [panelSizes]="panelSizeArray">
    <ng-container *ngFor="let panel of panelSizeArray">
        <ng-template pTemplate>
            content
        </ng-template>
    </ng-container>
</p-splitter>

Component:

panelSizeArray = this.someSignalStore.panelSizeArray;
splitterRerenderHack = true;

constructor() {
    effect(() => {
        this.panelSizeArray = this.someSignalStore.panelSizeArray;
        this.splitterRerenderHack = false;
        setTimeout(() => {
            this.splitterRerenderHack = true;
        });
    });
}

cheekrm avatar Oct 11 '24 04:10 cheekrm