更新JavaScript中Alpine.js的x-data在init函数中。

huangapple go评论58阅读模式
英文:

Updating x-data with Alpine.js in JavaScript's init function

问题

div class="mt-2" x-data="{ ShowReport: false }">
    <button class="dui-btn dui-btn-neutral" x-on:click="ShowReport = !ShowReport">Show Report</button>
    <div x-show="ShowReport">
        <div class="p-4">
            <div id="csv-response"></div>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", () => {
        const csvUploadDropzone = new common.Dropzone("#csv-upload-dropzone", {
            dictDefaultMessage: "Drag and drop your csv here",
            url: "{% url 'buyer:csv-upload' buyer.id %}",
            acceptedFiles: '.csv',
            uploadMultiple: false,
            autoProcessQueue: true,
            maxFiles: 1,
            dictMaxFilesExceeded: "You may only upload a single csv document!",
            maxFilesize: 10,
            addRemoveLinks: true,
            dictRemoveFile: "Remove",

            init: function () {
                this.on("success", function (file, response) {
                    document.getElementById('csv_filename').value = response['csv_filename'];
                    document.getElementById("csv-response").innerHTML = response;
                    this.ShowReport = true;
                });
            },

            removedfile: function(file) {
                document.getElementById('csv_filename').value = null;

                if (file.previewElement != null && file.previewElement.parentNode != null) {
                    file.previewElement.parentNode.removeChild(file.previewElement);
                }
                return this._updateMaxFilesReachedClass();
            },

            maxfilesexceeded: function(file) {
                alert("You may only upload a single csv document!");
                this.removeFile(file);
            },

        });
    });
</script>
英文:

Alpine.js: How do we update an x-data="{ ShowReport: false }" in an init function of Javascript?

I'm trying to open a &lt;div&gt; once a file already executed(which is now good) but the ShowReport didn't change to "true"

Trying to update the ShowReport to "true" Please see the attached code:

div class=&quot;mt-2&quot; x-data=&quot;{ ShowReport: false }&quot;&gt;
            &lt;button class=&quot;dui-btn dui-btn-neutral&quot; x-on:click=&quot;ShowReport = ! ShowReport&quot;&gt;Show Report&lt;/button&gt;
            &lt;div x-show=&quot;ShowReport&quot;&gt;
                &lt;div class=&quot;p-4&quot;&gt;
                    &lt;div id=&quot;csv-response&quot;&gt;&lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;

 &lt;script&gt;
        document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
            const csvUploadDropzone = new common.Dropzone(&quot;#csv-upload-dropzone&quot;, {
                dictDefaultMessage: &quot;Drag and drop your csv here&quot;,
                url: &quot;{% url &#39;buyer:csv-upload&#39; buyer.id %}&quot;,
                acceptedFiles: &#39;.csv&#39;,
                uploadMultiple: false, // POST a single document at a time (on server URL ``url``)
                autoProcessQueue: true, // true will trigger automatic uploading/processing of the dropped resume
                maxFiles: 1, // a maximum of a single CV
                dictMaxFilesExceeded: &quot;You may only upload a single csv document!&quot;,
                maxFilesize: 10, // in MB
                addRemoveLinks: true,
                dictRemoveFile: &quot;Remove&quot;,

                init: function () {
                    this.on(&quot;success&quot;, function (file, response) {
                        // Updates the &quot;hidden&quot; csv with whatever the server sent back to us...
                        document.getElementById(&#39;csv_filename&#39;).value = response[&#39;csv_filename&#39;];
                        document.getElementById(&quot;csv-response&quot;).innerHTML = response;
                        this.ShowReport = true
                    });
                },

                //success: function(file, response) {
                    // Updates the &quot;hidden&quot; csv with whatever the server sent back to us...
                    //document.getElementById(&#39;csv_filename&#39;).value = response[&#39;request_file&#39;];
                    //console.log(file)
                //},

                removedfile: function(file) {
                    // Update our hidden filename; then call the &quot;default&quot; behaviour as per docs...
                    document.getElementById(&#39;csv_filename&#39;).value = null;

                    if (file.previewElement != null &amp;&amp; file.previewElement.parentNode != null) {
                        file.previewElement.parentNode.removeChild(file.previewElement);
                    }
                    return this._updateMaxFilesReachedClass();
                },

                maxfilesexceeded: function(file) {
                    alert(&quot;You may only upload a single csv document!&quot;);
                    this.removeFile(file);
                },

            });
        });
        
    &lt;/script&gt;

答案1

得分: 1

你可以考虑使用 a store 如下:

&lt;div class=&quot;mt-2&quot; x-data&gt;
  &lt;button
    class=&quot;dui-btn dui-btn-neutral&quot;
    x-on:click=&quot;$store.myStore.toggle()&quot;
  &gt;
    Show Report
  &lt;/button&gt;
  &lt;div x-show=&quot;$store.myStore.showReport&quot;&gt;
    &lt;div class=&quot;p-4&quot;&gt;
      &lt;div id=&quot;csv-response&quot;&gt;&lt;/div&gt;
Alpine.store(&#39;myStore&#39;, {
    showReport: false,
    toggle() {
        this.showReport = !this.showReport;
    },
});

document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
  // …
  init: function () {
    this.on(&quot;success&quot;, function (file, response) {
      // …
      Alpine.store(&#39;myStore&#39;).showReport = true;
    });
  },
  // …
});

或者你可以在脚本中定义 data 如下:

&lt;div class=&quot;mt-2&quot; x-data=&quot;myData&quot;&gt;
Alpine.data(&#39;myData&#39;, {
  ShowReport: false,
  init() {
    const myData = this;
    document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
      // …
      init: function () {
        this.on(&quot;success&quot;, function (file, response) {
          // …
          myData.ShowReport = true;
        });
      },
      // …
    });
  },
});
英文:

You could consider using a store like:

&lt;div class=&quot;mt-2&quot; x-data&gt;
  &lt;button
    class=&quot;dui-btn dui-btn-neutral&quot;
    x-on:click=&quot;$store.myStore.toggle()&quot;
  &gt;
    Show Report
  &lt;/button&gt;
  &lt;div x-show=&quot;$store.myStore.showReport&quot;&gt;
    &lt;div class=&quot;p-4&quot;&gt;
      &lt;div id=&quot;csv-response&quot;&gt;&lt;/div&gt;
Alpine.store(&#39;myStore&#39;, {
    showReport: false,
    toggle() {
        this.showReport = !this.showReport;
    },
});

document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
  // …
  init: function () {
    this.on(&quot;success&quot;, function (file, response) {
      // …
      Alpine.store(&#39;myStore&#39;).showReport = true;
    });
  },
  // …
});

Or you could define the data in the script:

&lt;div class=&quot;mt-2&quot; x-data=&quot;myData&quot;&gt;
Alpine.data(&#39;myData&#39;, {
  ShowReport: false,
  init() {
    const myData = this;
    document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
      // …
      init: function () {
        this.on(&quot;success&quot;, function (file, response) {
          // …
          myData.ShowReport = true;
        });
      },
      // …
    });
  },
});

</details>



huangapple
  • 本文由 发表于 2023年5月21日 08:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定