简体   繁体   English

如何使用Vue-SweetAlert2在单个弹出窗口中传递多个用户输入并更新数据

[英]How to pass multiple user inputs and update the data in a single popup using Vue-SweetAlert2

I know how to ask a user for his or her user name by a popup with Vue-SweetAlert2. 我知道如何通过Vue-SweetAlert2弹出窗口向用户询问其用户名。

<template>
      <v-btn class="create-button" color="yellow" @click="alertDisplay">Create</v-btn>

    <br/>

    <p>Test result of createCustomer: {{ createdCustomer }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        createdCustomer: null
      }
    },
    methods: {
      alertDisplay() {
        var customer = await this.$swal({
                          title: 'What is your Name?',
                          input: 'text',
                          inputPlaceholder: 'Enter your name here',
                          showCloseButton: true,       
                        });
        console.log(customer);
        this.createdCustomer = customer; 
      }
    }
  }
</script>

With code like the one above, you can store whatever the user typed into createdCustomer, and it should be displayed on the screen after the user gives the input. 使用上面的代码,您可以存储用户在createdCustomer中键入的任何内容,并且在用户提供输入后,它应该显示在屏幕上。

But what if I wanted to ask the user for multiple pieces of information? 但是,如果我想询问用户多条信息怎么办? For example, how do I ask for info like 例如,我如何要求提供类似信息

  • "customerNumber" (also want to make sure that alphabets and numbers are combined) “ customerNumber”(还希望确保字母和数字结合在一起)
  • "locale" (also want to make sure that the input is a collection of choices that the user chooses from, like drop down menu, rather than a text field where you can type in whatever you like) “语言环境”(还希望确保输入是用户从中选择的选项的集合,例如下拉菜单,而不是可以在其中键入所需内容的文本字段)
  • "firstName" (also want to make sure that the name doesn't exceed 255 characters) “名字”(还希望确保名称不超过255个字符)

etc. 等等

in a single popup? 在一个弹出窗口中?

I tried to set multiple input fields like below, but I got a warning "Unknown parameter", and this doesn't seem to be a valid way. 我试图像下面那样设置多个输入字段,但是收到警告“未知参数”,这似乎不是有效的方法。

var customer = await this.$swal({
                          title: 'Fill in your personal data',
                          input1: 'text',
                          input2: 'text',
                          input3: 'text',
                          inputPlaceholder: 'Enter your name here',
                          showCloseButton: true,       
                        });

And how do I check if the user has given a valid input (like the name is within 255 characters, both of alphabets and numbers are used etc)? 以及如何检查用户是否提供了有效的输入(例如名称在255个字符以内,同时使用了字母和数字等)? If I were using C or Java, I could imagine using if-statements like 如果我使用的是C或Java,我可以想象使用if语句,例如

if(length <= 255){
    // proceed
} else {
    // warn the user that the input is too long
}

somewhere in the code, but in this case I don't know how I can do a similar if-statement like thing within the popup... 代码中的某处,但是在这种情况下,我不知道如何在弹出窗口中执行类似的if语句...

[ADDITIONAL QUESTION] [其他问题]

Is it also possible to pass an object that consists of multiple smaller elements, like "address"? 是否还可以传递包含多个较小元素(例如“地址”)的对象?

"address": {
    "street": "string",
    "city": "string",
    "country": "USA",
    "region": "string",
    "zipCode": "string"
}

As per the documentation : 根据文档:

Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters. 不支持多个输入,您可以使用html和preConfirm参数来实现它们。 Inside the preConfirm() function you can return (or, if async, resolve with) the custom result: 在preConfirm()函数内部,您可以返回(或使用异步方法解析)自定义结果:

const {value: formValues} = await Swal.fire({
    title: 'Multiple inputs',
    html: '<input id="swal-input1" class="swal2-input">' +
        '<input id="swal-input2" class="swal2-input">',
    focusConfirm: false,
    preConfirm: () => {
        return [
            document.getElementById('swal-input1').value,
            document.getElementById('swal-input2').value
        ]
    }
})
if (formValues) {
    Swal.fire(JSON.stringify(formValues))
}

https://sweetalert2.github.io/ https://sweetalert2.github.io/

For validation you have to use the inputValidor prop like this : 为了进行验证,您必须使用inputValidor如下所示:

const {value: ipAddress} = await Swal.fire({
    title: 'Enter your IP address',
    input: 'text',
    inputValue: inputValue,
    showCancelButton: true,
    inputValidator: (value) => {
        if (!value) {
            return 'You need to write something!'
        }
    }
})

if (ipAddress) {
  Swal.fire(`Your IP address is ${ipAddress}`)
}

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

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