https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • r

    ripe-action-67367

    12/16/2022, 1:51 PM
    The canonical way is always server-size rendering. So, the second best options is rendering a part of the page, containing the select (e.g., a form, a tab etc.) and htmx then swaps it into the page content. JSON is not "native" to htmx and hypermedia, so if having a JSON api is a requirement, that you can't avoid, you need https://htmx.org/extensions/client-side-templates/ extension.
  • r

    ripe-action-67367

    12/16/2022, 1:53 PM
    This extension provides integration with three popular templating libraries, but you can always copy and paste its code to work with your engine of choice
  • a

    abundant-spring-38265

    12/16/2022, 1:53 PM
    unfortunately we are migrating from Django backend and as an interim new API will be used for what Django used to pass from within it's context :/
  • r

    ripe-action-67367

    12/16/2022, 1:54 PM
    note, that I assumed that this endpoint is external to your site (e.g, an external service or a microservice from a separate team). If you can get this data from a database directly, absolutely do so
  • a

    abundant-spring-38265

    12/16/2022, 2:00 PM
    so something like this?
    Copy code
    <div class="input-group">
                            <select class="form-select form-select-sm" hx-get="https://virtserver.swaggerhub.com/paulo882/TestRunner/1.0.0/device" hx-target="#device_list"
                                nunjucks-template="devices_dropdown">
                                <option selected disabled>Select Device</option>
                                <div id="device_list"></div>
                            </select>
                            <template id="devices_dropdown">
                                {% verbatim %}
                                {% for device in devices %}
                                <option name="device_name" value="{{ device.device_id }}">{{ device.device_id }}</option>
                                {% endfor %}
                                {% endverbatim %}
                            </template>
                        </div>
  • m

    mysterious-toddler-20573

    12/16/2022, 2:01 PM
    I would use (or repurpose) the client-side-templates extension for this: https://htmx.org/extensions/client-side-templates/
  • r

    ripe-action-67367

    12/16/2022, 2:01 PM
    yes, pretty much this
  • r

    ripe-action-67367

    12/16/2022, 2:02 PM
    don't forget to include a link to nunjucks library, as the extension does not include it
  • r

    ripe-action-67367

    12/16/2022, 2:02 PM
    also, don't forget to set hx-ext="client-side-templates" to enable the extension
  • m

    mysterious-toddler-20573

    12/16/2022, 2:02 PM
    (lol, sorry, early, realize @ripe-action-67367 has this well in hand)
  • a

    abundant-spring-38265

    12/16/2022, 2:03 PM
    yes i have it defined inside of my container (base.html)
  • r

    ripe-action-67367

    12/16/2022, 2:03 PM
    great!
  • r

    ripe-action-67367

    12/16/2022, 2:03 PM
    then it should work I think hopefully maybe it worked on my machine
  • a

    abundant-spring-38265

    12/16/2022, 2:05 PM
    think i need onselect action?
  • r

    ripe-action-67367

    12/16/2022, 2:06 PM
    that depends on your workflow
  • r

    ripe-action-67367

    12/16/2022, 2:06 PM
    wait
  • r

    ripe-action-67367

    12/16/2022, 2:06 PM
    I just realized
  • a

    abundant-spring-38265

    12/16/2022, 2:06 PM
    hx-trigger="change" ?
  • r

    ripe-action-67367

    12/16/2022, 2:06 PM
    you have a div in your select
  • r

    ripe-action-67367

    12/16/2022, 2:06 PM
    I'm 99% sure this is not valid
  • a

    abundant-spring-38265

    12/16/2022, 2:07 PM
    using my logic it would be rendered inside of select (form-select)
  • r

    ripe-action-67367

    12/16/2022, 2:08 PM
    nevermind it works
  • r

    ripe-action-67367

    12/16/2022, 2:08 PM
    when do you want to load this list?
  • a

    abundant-spring-38265

    12/16/2022, 2:09 PM
    when i click the select menu
  • a

    abundant-spring-38265

    12/16/2022, 2:09 PM
    well trigger the dropdown
  • r

    ripe-action-67367

    12/16/2022, 2:10 PM
    onclick
  • r

    ripe-action-67367

    12/16/2022, 2:10 PM
    I never heard of some separate event for opening the select
  • a

    abundant-spring-38265

    12/16/2022, 2:13 PM
    hmm doesn't seem to send a request
  • r

    ripe-action-67367

    12/16/2022, 2:13 PM
    this is unfortunate
  • r

    ripe-action-67367

    12/16/2022, 2:15 PM
    could you share your select tag?
1...951952953...1146Latest