bigskysoftware / htmx

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

Scoping Parameters #766

Closed tola closed 2 years ago

tola commented 2 years ago

This is my table template. If I have 3 data items then I have three rows. `

               <span id="b<%#Eval("Code")%>">

            </span>

                <input type="hidden" id="booktoken" name="token" value="<%#Eval("Token")%>">
                  <a href="#" hx-post="/GetBookInfo"  
                  hx-target="#b<%#Eval("Code")%>" 
                  hx-params="token"  
                  hx-trigger="click"> <%#Eval("Code")%></a>

            </td> `

htmx will work as expect if there is only one row. However, if there are more rows, htmx will send all the params (3 param in this case) when only a single link is clicked. image

My work-around is to wrap a TD tag between the FORM tag like this:

            <form>
            <td>

               <span id="b<%#Eval("Code")%>">

            </span>

                <input type="hidden" id="booktoken" name="token" value="<%#Eval("Token")%>">
                  <a href="#" hx-post="/GetBookInfo"  
                  hx-target="#b<%#Eval("Code")%>" 
                  hx-params="token"  
                  hx-trigger="click"> <%#Eval("Code")%></a>

            </td>  
            </form> 

I have read about hx-params here https://htmx.org/docs/#parameters and here https://htmx.org/attributes/hx-params/ to try to find how to make hx-params limit to get only parameter that's belong to current TD. In other words, how do I limit the hx-param scope to current elelemnt or parent element only?

Is there any scoping parameter feature similar to scoping variable by @hype as mentioned here https://hyperscript.org/docs/#variables_and_scope

Is there away to make hx-params target the element by ID or read value assignment directly? Something like this:

1cg commented 2 years ago

You can use hx-vals to accomplish this:

https://htmx.org/attributes/hx-vals/