简体   繁体   English

使用 vue.js 和 axios 提交表单数组

[英]Submit form array with vue.js and axios

I'm new to Vue and building a search form that the user can add rows to as the search gets more complex.我是 Vue 的新手,正在构建一个搜索表单,当搜索变得更加复杂时,用户可以添加行。 I can use Vue and Axios to submit a single form input fine, but am lost when moving the previous PHP and HTML form array POST process to Vue.我可以使用 Vue 和 Axios 提交单个表单输入,但是在将之前的 PHP 和 HTML 表单数组 POST 过程移动到 Vue 时我会丢失。 The form fields will all be submitted to an API as JSON in the request.表单字段将在请求中作为 JSON 全部提交给 API。

While I have found plenty of info on how to submit a form field with Vue, I haven't found anything that speaks to form element array submission.虽然我找到了大量关于如何使用 Vue 提交表单字段的信息,但我还没有找到任何与表单元素数组提交有关的信息。

The form values look like the below and there can be one or more sets of these based on the user's input.表单值如下所示,根据用户的输入,可以有一组或多组。 I have tried naming with and without the basic "[]" to indicate multiple array items.我尝试过使用和不使用基本“[]”来表示多个数组项的命名。 These are in the Vue app as defined by "myapp-pages".这些在“myapp-pages”定义的 Vue 应用程序中。

<input type='text' v-model='advancedSearchForm.field' name='field' />
<input type='text' v-model='advancedSearchForm.operator' name='operator' />
<input type='text' v-model='advancedSearchForm.value' name='value' />

The Vue JS code is the following with the form items defined and the Axios POST function defined. Vue JS 代码如下,定义了表单项并定义了 Axios POST function。

var app = new Vue({
    el: '#myapp-pages',
    data: {
        pages: [],
        pagecount: 0,
        advancedSearchForm: {
            field: "",
            operator: "",
            value: ""
        }
    },
    methods: {
        advancedSearch: function() {
            axios({
                url: '/api/pages/search/advanced',
                method: 'post',
                data: {
                    search: this.advancedSearchForm
                }
            })
            .then(function (response) {
                app.pages = response.data.data.pages;
                app.pagecount = response.data.data.pages.length;
            })
            .catch(function (error) {
                console.log(error);
            });
        }
    }
})

My questions are, does the use of v-model make sense here to try and register those fields with the Vue app?我的问题是,在这里使用 v-model 是否有意义来尝试使用 Vue 应用程序注册这些字段? If so, how can I register an unknown number of the same DOM element as the user adds more rows?如果是这样,当用户添加更多行时,如何注册未知数量的相同 DOM 元素? If not, how can Vue+Axios use these values to submit the POST request?如果没有,Vue+Axios如何使用这些值来提交POST请求?

Thanks for any help.谢谢你的帮助。

You can accomplish this as you are with v-models and then saving values as an object to an array.您可以像使用 v-models 一样完成此操作,然后将值作为 object 保存到数组中。 This will let you save the input history.这将让您保存输入历史记录。 For dynamically adding rows (or anything) to the DOM, look up the v-for directive.要向 DOM 动态添加行(或任何内容),请查找 v-for 指令。 This lets you dynamically add things to the template so that as the array increases, more rows will be generated.这使您可以动态地将内容添加到模板中,以便随着数组的增加,将生成更多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM