Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
genuinefafa opened this issue Jun 23, 2017 · 21 comments
Open

Comments

@genuinefafa
Copy link

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.

@edcarroll
Copy link
Owner

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
Copy link
Owner

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

@genuinefafa
Copy link
Author

genuinefafa commented Jun 25, 2017

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>

@edcarroll
Copy link
Owner

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

@edcarroll edcarroll added this to the 0.9.0 milestone Jun 25, 2017
@genuinefafa
Copy link
Author

genuinefafa commented Jun 25, 2017

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>

@edcarroll
Copy link
Owner

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!

@genuinefafa
Copy link
Author

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

pst: this issue is still with label investigating

@edcarroll edcarroll changed the title Accessing context variables from within popup template feat(popup): Accessing context variables from within popup template Jun 26, 2017
@edcarroll edcarroll self-assigned this Jun 26, 2017
@edcarroll edcarroll removed this from the 0.9.0 milestone Jun 26, 2017
@edcarroll edcarroll reopened this Jun 26, 2017
@edcarroll edcarroll added this to the Needs Milestone milestone Jun 26, 2017
@edcarroll edcarroll modified the milestones: 0.9.1, Needs Milestone Jul 3, 2017
@edcarroll edcarroll modified the milestones: Needs Milestone, 0.9.3 Jul 19, 2017
@edcarroll edcarroll modified the milestones: 0.9.5, Needs Milestone Aug 11, 2017
@edcarroll
Copy link
Owner

@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
Copy link
Owner

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

@genuinefafa
Copy link
Author

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

@Gimly
Copy link

Gimly commented Aug 25, 2017

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.

@genuinefafa
Copy link
Author

genuinefafa commented Aug 25, 2017 via email

@Gimly
Copy link

Gimly commented Aug 25, 2017

@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.

@edcarroll
Copy link
Owner

@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 edcarroll reopened this Aug 26, 2017
@edcarroll edcarroll modified the milestones: 0.9.7, 0.9.5 Aug 26, 2017
@arron21
Copy link

arron21 commented Feb 26, 2018

@edcarroll any news on this?

@bikash8gupta
Copy link

Any news on this?

@gabrieldoty
Copy link
Contributor

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

@aimoradajr
Copy link

Still not working for me. Halp

@rahul-uttarkar
Copy link

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

@janpapenbrock
Copy link

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>

@Belouccio
Copy link

@janpapenbrock thank you man) you saved my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants