-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refresh component B with s-val from component A #209
Comments
You can use a hidden input field to control the component's state. {#-- _components/entryOverlay --#}
{% set entryId = entryId ?? '' %}
{{ hiddenInput('entryId', entryId) }}
{% if entryId %}
{% set entry = craft.entries.id(entryId).one() %}
{% if entry %}
<p>{{entry.title}}</p>
{% endif %}
{% endif %} Which you can update via JavaScript. <button onclick="htmx.find('#entryOverlay input[name=entryId]').value = {{ entry.id }}; htmx.trigger('#entryOverlay', 'refresh');">
{{ entry.title }}
</button> |
Perfect! Thank you Ben. |
Hi, I have a follow up question. How can I use something like s-include="" on my javascript button in my entryList component? I didn't find or understand how to do it with the htmx api methods. I only need to send the entryId. <a onclick="htmx.find('#entryOverlay input[name=entryId]').value = {{ entry.id }}; htmx.trigger('#entryOverlay', 'refresh');">
{{ entry.title }}
</a>
{% endfor %} Because inside my entryOverlay I am loading another entryList component with related entries. And there the link is sending all the verbb comments input values again. |
If none of the comment input field values should ever be submitted via Sprig, then adding {{ sprig('_components/entryOverlay', {}, {
'id': 'entryOverlay',
's-include': '',
}) }} |
Thanks. I think this also prevents the link from sending the hidden input entryId value? It doesn't seem to work. My workaround now is that the entryOverlay is passing a variable to the second time I load the entryList. And there instead of the javascript Link I use this one. <button sprig
s-val:entryid="{{ entry.id }}"
s-target="#projectOverlay"
s-include="">
</button> Which seems to work at this point. Don't know why though and if it's a good solution. So I am using two different Links now depending where load the component. |
Ah right, in that case you can include that input field only. {{ sprig('_components/entryOverlay', {}, {
'id': 'entryOverlay',
's-include': 'input[name=entryId]',
}) }} |
Cool, that did it. Thanks! |
Hi Ben, I updated to Craft 5 and Sprig 3. I am getting a 500 error with this solution now.
|
It should continue to work in Sprig 3. Please create a new issue with some code I can use to reproduce locally, along with details, and I’ll take a look. |
Question
Hi Ben, I am trying to build a link which will load an entry into another component.
I have a list of entries wich is updated via sprig – just to give you some context.
Each list item has a link that should load the given entry into my overlay container.
This is a stripped down version of what I have. When I click the link it loads more entries from my list into the overlay component.
But it looks like my link is not passing the entryId to the entryOverlay component. Is it even possible to send a s-val from one component to another? I am sure I am just overlooking something in the docs.
So I am basically looking for button that refreshes another component with a new variable.
Additional context
I also tried a different button like this
But I it also doesn't pass the variable.
The text was updated successfully, but these errors were encountered: