mysterious-toddler-20573
01/31/2023, 11:10 PMlittle-kilobyte-16316
02/01/2023, 12:01 AMicy-motherboard-95213
02/01/2023, 12:15 AMthousands-teacher-15959
02/01/2023, 12:12 PMthousands-teacher-15959
02/01/2023, 1:04 PMsparse-psychiatrist-6723
02/01/2023, 5:20 PMhx-include
attribute to use "closest tr" with other CSS selectors?
This works:
hx-include="#filter,#sorter"
This doesn't:
hx-include="#filter,#sorter,closest tr"
mysterious-toddler-20573
02/01/2023, 5:34 PMclosest
isn't a valid CSS selector, so mixing and matching like that won't work, unfortunatelysparse-psychiatrist-6723
02/01/2023, 5:35 PMmysterious-toddler-20573
02/01/2023, 5:35 PMsparse-psychiatrist-6723
02/01/2023, 5:36 PMmysterious-toddler-20573
02/01/2023, 5:36 PMhtmx.onLoad()
instead, so new content is processed correctlymysterious-toddler-20573
02/01/2023, 5:36 PMchange
event is what's triggered, it should workrich-traffic-81552
02/01/2023, 6:01 PMhx-trigger="change changed from:#start_date
and am struggling to get it to trigger. Here's the JS code in an event listener for a custom component I expect to trigger it:
javascript
const startDateInput = document.querySelector('[name="start_date"]');
startDateInput.value = isoDate(event.firstDay);
startDateInput.dispatchEvent(new Event("change", { bubbles: true }));
If I remove "changed" from the trigger it fires. does the "changed" in this case mean the div's value changed or the target has changed? because the target is definitely changingmysterious-toddler-20573
02/01/2023, 6:09 PMmysterious-toddler-20573
02/01/2023, 6:09 PMrich-traffic-81552
02/01/2023, 6:19 PMrich-traffic-81552
02/01/2023, 6:21 PMfrom:
modifier. if I'm listening to another element's change
i probably don't expect my value to have changedmysterious-toddler-20573
02/01/2023, 6:45 PMfrom:#foo changed:me
thousands-teacher-15959
02/01/2023, 8:21 PMadventurous-ocean-93733
02/01/2023, 8:46 PM<form hx-get="/query" hx-target="#result">
<input type="text" name="qt" value="<%=params[:qt]%>">
<input type="submit" value="Submit">
</form>
When the form is submitted with the query text (qt
) of foo
I want to update the url to (and add history item for) example.com/?qt=foo
so that the link can be shared and qt
is automatically populated.
This is what I've explored:
- hx-push-url="/?qt=...
but I can't see how to include the dynamic value
- hx-push-url="true"
but this understandably uses the path /query
and that's not what I want
- hyperscript, I can't find anyway to update the address bar and history with hyperscript but might have overlooked it?
I saw this comment from 1cg https://discord.com/channels/725789699527933952/725789747212976259/1069440045385265213 but I'm unsure if that would work for my use case and interested to see if there's a simpler method that I've overlooked.mysterious-toddler-20573
02/01/2023, 9:00 PMHX-Push-Url
response headeradventurous-ocean-93733
02/01/2023, 9:07 PMmysterious-toddler-20573
02/02/2023, 3:08 AMlittle-electrician-91324
02/02/2023, 8:52 AMth
, something like this:
html
<th id="firstNameHeader"
hx-get="/search?sortBy=FirstName&order=asc&source=firstNameHeader"
hx-target="#search-results"
hx-trigger="click"
hx-include="[name='search']"
hx-indicator=".htmx-indicator">
First Name <i class="bi bi-arrow-down-up"></i>
</th>
In the response, I could then return the "search-results" fragment, but also include the "firstNameHeader" th
fragment and replace it using an hx-swap-oob
. The reason for doing this would solely be to change the order
in hx-get
to desc
, i.e. hx-get="/search?sortBy=FirstName&order=desc&source=firstNameHeader"
.
While this would probably work, it becomes complex quite fast if there are additional filters, such as a new drop-down where you can select a "country" etc, while retaining the sort functionality.
Is there a nice way to achieve this in pure HTMX, or is it better to resort to some javascript in these cases? I'm thinking that the javascript in this case, would probably append the various filters and sort parameters to the search uri dynamically and then trigger the request. But I would like to avoid this if there's a clean way to do it with HTMX.stocky-dentist-80693
02/02/2023, 9:11 AM<form hx-get="/search" hx-target="#search-results">
tag - this will automatically include the search field (and any other regular inputs you happen to use later).
Then, you can use the <button type="submit" name="sort" value="FirstName" />
for your column sorting - a standard button that will submit the enclosing when clicked. I tend to prefix sort column names with a -
when it should do descending order, to avoid needing a separate order
parameter. So in this case, your server response will instead use -FirstName
as the value, so when clicked it will go the other way. On the server side you would check if the first character is a -
to decide on applying your asc/desc option, and if yes, trim it off the start.little-electrician-91324
02/02/2023, 9:16 AMthousands-teacher-15959
02/02/2023, 12:21 PMjs
htmx.onLoad(() => {
$("#collection-select").select2({
theme: "bootstrap-5",
closeOnSelect: true
});
});
window.addEventListener("DOMContentLoaded", (e) => {
$('select').on('select2:select', function (e) {
$(this).closest('select').get(0).dispatchEvent(new Event('change'));
});
});
Required both onLoad and the solution from SO https://stackoverflow.com/questions/65658432/select2-change-event-does-not-trigger-htmxmysterious-toddler-20573
02/02/2023, 12:32 PMhx-trigger='select2:select'
to avoid neeeding to add that second event handlermysterious-toddler-20573
02/02/2023, 12:32 PMmysterious-toddler-20573
02/02/2023, 12:33 PMonLoad
handler like this:
javascript
htmx.onLoad((newContent) => {
$(newContent).find("#collection-select").select2({
theme: "bootstrap-5",
closeOnSelect: true
});
});