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

Adding trigger functions in Feature Popup overlay #1067

Open
liyunooi opened this issue Jun 3, 2024 · 2 comments
Open

Adding trigger functions in Feature Popup overlay #1067

liyunooi opened this issue Jun 3, 2024 · 2 comments

Comments

@liyunooi
Copy link

liyunooi commented Jun 3, 2024

Hi,

I am using your library to display some information via Feature popup overlay.
I would like to be able to set a function to trigger a call-to-action upon clicking on the value of an attribute inside the Feature popup overlay.

May I know how would this be handled?

I tried using direct handling by setting the value as a string like so:
<a href="http://google.com" target="_blank">${value}</a>

But it seems like the popup overlay does not accept direct html modifications to the value.

Is there any workaround for this?

Thank you.

@liyunooi liyunooi changed the title Adding trigger functions in Popup overlay Adding trigger functions in Feature Popup overlay Jun 3, 2024
@Viglino
Copy link
Owner

Viglino commented Jun 19, 2024

You can set a link as value using templates and listen to click event on popup show:

var popup = new ol.Overlay.PopupFeature({
    ...
    template: {
         attributes: {
          'myatt': { 
            title: 'My attribute',  // attribute's title
            format: function(val, f) { 
              return '<a>' + val + '</a>'
            }
          },
        }
    }
});
map.addOverlay (popup);

// Set listener on show
popup.on('show', (e) => {
    popup.element.querySelector('A').addEventListener('click', console.log)
})

Viglino added a commit that referenced this issue Jun 19, 2024
@Viglino
Copy link
Owner

Viglino commented Jun 19, 2024

PS I've added a 'attribute' event in the last commit 9ca2774 to make it easier:

popup.on('attribute', function(e) {
  // the attribute
  var value = e.feature.get(e.attribute)
}

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

2 participants