English 中文(简体)
如何通过与htmx的等值提交X-数据
原标题:How to submit x-data via hx-vals with htmx

任何人都知道如何这样做? 我的母体为<代码>x-data=“check BoxHandler”。

<条码>x-data本身正在发挥作用,但我无法按要求提交。

《联合材料》的错误是,任何类型都没有长度。

我知道,在Alpine仓库中,你可以做像我下面尝试的那样做些事情,但它没有数据。

任何帮助都是令人吃惊的。

<input type="button"
           value="Submit"
           hx-get= /example
           hx-target="#hx_modal_container"
           hx-vals= js:{"total": Alpine.$data( checkboxHandler ).checkedValues.length}  # This is the line that is not working
/>

×数据

document.addEventListener( alpine:init , () => {
            Alpine.data( checkboxHandler , () => ({
                checkedValues:[],
                selectAll: false,
                total(){this.checkedValues.length},
                addAll() {
                    this.selectAll=true
                    const allPageValues=JSON.parse(document.getElementById( all_rows_ids ).textContent)
                    this.checkedValues=[]
                    this.checkedValues.push(...allPageValues.map(value => value.toString()))
                    this.checkedValues.push( all )
                 },
                 removeAll() {
                    this.selectAll=false
                    const allPageValues=JSON.parse(document.getElementById( all_rows_ids ).textContent)
                    this.checkedValues = this.checkedValues.filter(value => !allPageValues.map(value=>value.toString()).includes(value))
                    const index= this.checkedValues.indexOf( all )
                    this.checkedValues.splice(index, 1);
                 }
     
                
            }))
           
        })

问题回答

类似情况:

<input type="button"
           value="Submit"
           hx-get="/example"
           hx-target="#hx_modal_container"
           x-bind:hx-vals="JSON.stringify({"total": Alpine.$data( checkboxHandler ).checkedValues.length})"
/>

您必须使用<<>AlpineJS 贮藏<>m>与联合材料共享数据:

<div>
    <input type="button"
           value="Submit"
           hx-get="/example"
           hx-target="#hx_modal_container"
           hx-vals="js:{total: Alpine.store( checkedValues ).length}"
    />

</div>

<script>

    document.addEventListener( alpine:init , () => {

        Alpine.store("checkedValues", []);

        Alpine.data("checkboxHandler", () => ({

            selectAll: false,
            total(){this.$store.checkedValues.length},

            addAll() {
                this.selectAll=true
                const allPageValues=JSON.parse(document.getElementById( all_rows_ids ).textContent)
                this.$store.checkedValues=[]
                this.$store.checkedValues.push(...allPageValues.map(value => value.toString()))
                this.$store.checkedValues.push( all )
            },

            removeAll() {
                this.selectAll=false
                const allPageValues=JSON.parse(document.getElementById( all_rows_ids ).textContent)
                this.$store.checkedValues = this.$store.checkedValues.filter(value => !allPageValues.map(value=>value.toString()).includes(value))
                const index= this.$store.checkedValues.indexOf( all )
                this.$store.checkedValues.splice(index, 1);
            }
        }))

    })

</script>

The checkedValues variable is now placed into the store.
Inside the Alpine object it is accessed via the magic property $store, while in the JS (htmx here) is accessed using the Alpine.store() function





相关问题
htmx - focus element (non javascript)

How are elements focused in htmx? I can t seem to find an answer anywhere. The goal is to focus a specific element after a POST request i.e. focus the input with id #field-description. I d like this ...

Hide HTMX search results after option is selected

I have a search bar I created using HTMX and django forms that displays results correctly. I have a javascript selectOption() function filling the search bar with whatever the selected option is, but ...

Hx-push-url don't work on Back buttonpoint

我正试图将Htmx与django混为一谈,并实现单一页的应用行为。 我向Django项目投了票。 当我点击短页链接时,内容负荷量为htmx。

热门标签