ng2-semantic-ui icon indicating copy to clipboard operation
ng2-semantic-ui copied to clipboard

feat(popup): Accessing context variables from within popup template

Open genuinefafa opened this issue 7 years ago • 21 comments

I'm creating a site where a popup uses a template that needs access to data from scope (let's thing of it as a popup assigned for every item in a *ngFor).

How can I do that? Couldn't find it in the docs.

genuinefafa avatar Jun 23 '17 21:06 genuinefafa

Have you tried defining the <ng-template> inside the *ngFor, like so:

<div *ngFor="let item of items">
    <ng-template #template>
        <p>{{ item.name }}</p>
    </ng-template>
    <button class="ui button" suiPopup [popupTemplate]="template">Action</button>
</div>

edcarroll avatar Jun 25 '17 13:06 edcarroll

Just made a plunkr demonstrating this - does that suit your use case?

edcarroll avatar Jun 25 '17 13:06 edcarroll

Ok @edcarroll you've got this one. I was trying to use the template outside the element, using it inside the iteration did the trick, nice catch. Maybe add it to the docs? either way this issue will work as a heads up for anyone looking for a solution.

My exact use case is via sui-select and is written like this

<ng-template let-option #selectTemplate>
	<ng-template #popupTemplate>
		<div class="header">{{ option.field }}</div>
		<div class="content">
			{{ option.otherField }}
		</div>
	</ng-template>
	<div suiPopup [popupTemplate]="popupTemplate" popupBasic="true" popupPlacement="bottom right">
			{{ option.field }} ({{ option.id }})
	</div>
</ng-template>					

<sui-select [(ngModel)]="selected" [options]="options" [optionTemplate]="selectTemplate" labelField="field" [isSearchable]="true" #select>
	<sui-select-option *ngFor="let option of select.availableOptions" [value]="option">
	</sui-select-option>
</sui-select>

genuinefafa avatar Jun 25 '17 17:06 genuinefafa

Glad that's sorted it, and yes good idea, will add a note to the docs

edcarroll avatar Jun 25 '17 22:06 edcarroll

One idea just crash my mind... what if I wanna reuse templates? this workaround will not fix the underlying issue...

What about adding a let-thingie in order to pass variables to the popup? Maybe just like sui-select does with its own options via let-option.

Will result in a code like this

<!-- look for let-var and its usage -->
<ng-template let-var #popupTemplate>
	<div class="header">{{ var.field }}</div>
	<div class="content">
		{{ var.otherField }}
	</div>
</ng-template>
<!-- look for [value] attr -->
<div suiPopup [popupTemplate]="popupTemplate" popupBasic="true" [value]="option">
		{{ option.field }} ({{ option.id }})
</div>

genuinefafa avatar Jun 25 '17 23:06 genuinefafa

This isn't a bad idea actually... Though I've made a tonne of changes to the popup component in the datepicker, so will leave this for now until I've merged those changes, otherwise it's futile!

edcarroll avatar Jun 26 '17 12:06 edcarroll

Ok then, let's wait for the next release! How can we follow this feature request?

pst: this issue is still with label investigating

genuinefafa avatar Jun 26 '17 13:06 genuinefafa

@genuinefafa this will be in the next release. You can bind a context by setting popupTemplateContext, and you can access it in the template like so:

<ng-template let-popup #popupTemplate>
    {{ popup.context.myVariable }}
</ng-template>

edcarroll avatar Aug 11 '17 05:08 edcarroll

This is now live in 0.9.5. Let me know if you have any further issues 😄

edcarroll avatar Aug 14 '17 14:08 edcarroll

I see no example using context in docs. Can we add it there too?

genuinefafa avatar Aug 22 '17 16:08 genuinefafa

I've just tried making it to work, with no luck. Here's the code I have:

...

<tr *ngFor="let person of people">
    <td>{{ person.id }}</td>
    <td>{{ person.name }}</td>
    <td>
        <div class="ui buttons">
            <a class="ui button" [routerLink]="['edit',person.id]">
                <i class="edit icon"></i> Edit
            </a>
            <button class="ui icon button" suiPopup [popupTemplate]="confirmDeleteTemplate" [popupTemplateContext]="person" popupTrigger="outsideClick" #popup="suiPopup">
                <i class="delete icon"></i> Delete
            </button>
        </div>
    </td>
</tr>

...

<ng-template let-popup #confirmDeleteTemplate>
    <div class="header">Delete person</div>
    <div class="content">
        <p>You are deleting this person, are you sure?</p>
        <div class="ui buttons">
            <button class="ui positive button" (click)="deletePerson(popup.context.id)">Yes</button>
            <button class="ui negative button" (click)="popup.close()">No</button>
        </div>
    </div>
</ng-template>

I'm getting an error because context is undefined, so it seems that the context is not correctly set. Any idea to why that would be? I'm using version 0.9.6

Thanks in advance.

Gimly avatar Aug 25 '17 08:08 Gimly

are you using latest version of semantic-ui? can you provide an example plunkr?

On Fri, Aug 25, 2017 at 5:47 AM, Xavier Hahn [email protected] wrote:

I've just tried making it to work, with no luck. Here's the code I have:

...

{{ person.id }} {{ person.name }}
Edit

...

<ng-template let-popup #confirmDeleteTemplate>

Delete person

You are deleting this person, are you sure?

<button class="ui positive button" (click)="deletePerson(popup.context.id)">Yes <button class="ui negative button" (click)="popup.close()">No

I'm getting an error because context is undefined, so it seems that the context is not correctly set. Any idea to why that would be?

Thanks in advance.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/edcarroll/ng2-semantic-ui/issues/127#issuecomment-324860363, or mute the thread https://github.com/notifications/unsubscribe-auth/ABeGXeHoiEcIZxQRXtNUqlBwtCHbf7ZCks5sboo-gaJpZM4OEDll .

genuinefafa avatar Aug 25 '17 13:08 genuinefafa

@genuinefafa Yes, I'm indeed using the latest version of semantic-ui.

Here's a plunkr that exhibits the issue. http://plnkr.co/edit/l2T5T4GoriTWcigPNECj?p=preview

Thanks for your help.

Gimly avatar Aug 25 '17 14:08 Gimly

@Gimly thanks for the plunkr. Hmmm, looks like a bug. Will reopen for now and assess properly when I get a chance. Sorry for the delay!

edcarroll avatar Aug 26 '17 22:08 edcarroll

@edcarroll any news on this?

arron21 avatar Feb 26 '18 21:02 arron21

Any news on this?

bikash8gupta avatar Mar 31 '18 13:03 bikash8gupta

@edcarroll - is this still an outstanding bug? I am seeing similar behavior in the current alpha build

gabrieldoty avatar Sep 08 '18 16:09 gabrieldoty

Still not working for me. Halp

aimoradajr avatar Oct 29 '18 09:10 aimoradajr

I have checked its still not fixed. I am waiting for it too..

rahul-uttarkar avatar Sep 04 '19 09:09 rahul-uttarkar

Just in case anyone is stumbling across this again. This is how it works:

<div suiPopup [popupTemplate]="popupTemplate" [popupTemplateContext]="{ myVariable: 42 }">
  hover here
</div>

<ng-template let-context="context" #popupTemplate>
    {{ context.myVariable }}
</ng-template>

janpapenbrock avatar Jan 18 '21 15:01 janpapenbrock

@janpapenbrock thank you man) you saved my day!

Belouccio avatar Jan 29 '21 13:01 Belouccio