bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.33k stars 1.31k forks source link

htmx-get parameters and htmx-push-url #653

Closed jakewaxman closed 3 years ago

jakewaxman commented 3 years ago

I'm using HTMX in Python/Django to create a "filter" effect for a dashboard app. The filter uses hx-get with the path set to a view that contains the appropriate html fragments. It works well but ...

I'd like the user to be able to bookmark their query parameters. I'd thought to do that with htmx-push-url set to true. That correctly puts the query parameters into the address bar, Unfortunately it also puts the fragment's url in the address bar rather than the url of the original page.

So ... how do I get the address bar to contain the get query parameters while leaving the original path otherwise intact?

1cg commented 3 years ago

So the search sends a request do a different url than the current page?

jakewaxman commented 3 years ago

The intent is for the user to stay on the same page at the base address. That works.

The hx-get target is to a view that responds with the part of the page that needs to be updated. That part of the page is getting updated as it should.

The browser address bar is getting updated with the ?=query parameters. That's good.

It's also showing the address for the fragment. That's not good.

..... "dashboard" is the id for the fragment to be replaced "dashboard" is also the address for the fragment when the fragment is loaded the address bar is replaced with "https://ipaddress/dashboard?=period= ..." instead of just "https://ipaddress/?=period= ..." On Wed, Nov 10, 2021 at 7:12 PM 1cg ***@***.***> wrote: > So the search sends a request do a different url than the current page? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > , > or unsubscribe > > . > Triage notifications on the go with GitHub Mobile for iOS > > or Android > . > >
1cg commented 3 years ago

OK, I think you can use the value of the URL you want as the value of hx-push-url

See the second example here:

https://htmx.org/attributes/hx-push-url/

jakewaxman commented 3 years ago

Thanks That makes sense Where do I get the get method query string (?=...) from to attach to it though?

On Wed, Nov 10, 2021, 10:19 PM 1cg @.***> wrote:

OK, I think you can use the value of the URL you want as the value of hx-push-url

See the second example here:

https://htmx.org/attributes/hx-push-url/

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bigskysoftware/htmx/issues/653#issuecomment-965953449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABZBU2NUYEKWJYU47ZHQTLULMY4TANCNFSM5HY7ISCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

1cg commented 3 years ago

The parameters should be auto-appended to the URL

JonasR commented 2 years ago

@1cg Apologies for digging out this issue, but since the original reporter never confirmed it works and I have pretty much the same problem: Auto appending parameters to the pushed URL does not work for me if I use a custom push URL, not 'true'.

Consider this example

<script 
 src="https://unpkg.com/htmx.org@1.7.0" 
 integrity="sha384-EzBXYPt0/T6gxNp0nuPtLkmRpmDBbjg6WmCUZRLXBBwYYmwAUxzlSGej0ARHX0Bo" 
 crossorigin="anonymous">
</script>
<select  id="select-number" name="number" hx-get="/debug" hx-push-url="/debug2" hx-target="#target">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<div id="target">
</div>

I browse to http://127.0.0.1:7000/debug and select 2 in the select field. This will trigger a request to /debug?number=2 as expected. However, the URL pushed to the browser is http://127.0.0.1:7000/debug2, not including the parameters.

How can I get the parameters appended to a different base URL?

gregorrr commented 2 years ago

@1cg is it possible to reopen this issue? Or to confirm that this is the expected behavior. I'm having the same problem as @JonasR whereby the URL specified in hx-push-url does not include the query string parameters used by the hx-get.

benjaminsampica commented 1 year ago

Seeing this as well. Is this intended or not?

dcr8898 commented 1 year ago

I took a look at the code and it does not appear to attach query string parameters to custom hx-push-url values.

Look here in the source code.

The value for pushUrl is extracted from the hx- attributes. If it is set to "true", then pushUrl is set to a value from the response that presumably includes the query string. If pushUrl is anything other than "true" or "false", then it is left as the original attribute value (that is, the URL you specified). No query string is appended.

If this is a problem, I would open a new issue pointing that out.

JonasR commented 1 year ago

@dcr8898 Thanks a lot for digging into it. I'll try to confirm tonight if this is where the parameters would be appended through reuqestPath and open a new issue.