首页 > 其他分享 >js弹窗组件sweetalert2使用

js弹窗组件sweetalert2使用

时间:2024-09-25 17:25:31浏览次数:1  
标签:title gbl js alert inputValue input sweetalert2 type 弹窗

经常会用到sweetalert2这个弹出层组件,整理记录下。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link href="../plugins/sweetalert2/sweetalert2.min.css" rel="stylesheet" />
  </head>
  <body>
    <button onclick="app.msg()">简单消息</button>
    <button onclick="app.showSuccess()">成功</button>
    <button onclick="app.showError()">错误</button>
    <button onclick="app.showInfo()">信息</button>
    <button onclick="app.showWarning()">警告</button>
    <button onclick="app.confirm()">确认框</button>

    <br />
    <br />
    <button onclick="app.input_text()">文本框</button>
    <button onclick="app.input_email()">Email</button>
    <button onclick="app.input_url()">URL</button>
    <button onclick="app.input_password()">密码框</button>
    <button onclick="app.input_textarea()">文本域</button>
    <button onclick="app.input_select()">下拉菜单</button>
    <button onclick="app.input_radio()">单选框</button>
    <button onclick="app.input_checkbox()">复选框</button>
    <button onclick="app.input_date()">日期</button>
  </body>
  <script src="../plugins/sweetalert2/sweetalert2.all.min.js"></script>
  <script>
    const gbl_alert = {
      showBasicMsg: (title) => {
        Swal.fire(title);
      },

      showTypeMsg: (icon, title, timer = 0, callback_next = () => {}) => {
        const Toast = Swal.mixin({
          toast: true,
          position: "center",
          showConfirmButton: false,
          showCloseButton: true,

          timer: timer,
          timerProgressBar: true,
        });
        //type列表 warning error success info question
        Toast.fire({
          icon: icon,
          title: title,
        }).then((result) => {
          if (result.isDismissed) {
            callback_next();
          }
        });
      },

      showConfirmMsg: (title, text, callback_confirm, callback_deny = () => {}) => {
        Swal.fire({
          title: title,
          text: text,
          icon: "question",
          showCancelButton: true,
          confirmButtonColor: "#3085d6",
          cancelButtonColor: "#d33",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          backdrop: true,
          allowOutsideClick: false,
          allowEscapeKey: false,
          allowEnterKey: false,
        }).then((result) => {
          if (result.isConfirmed) {
            callback_confirm();
          } else {
            callback_deny();
          }
        });
      },

      input: async (type, title, label_text, inputPlaceholder, initialValue = "", params = {}) => {
        let input_val = "";
        if (["text", "password", "textarea", "checkbox"].indexOf(type) !== -1) {
          const { value: inputValue } = await Swal.fire({
            title: title,
            input: type,
            inputLabel: label_text,
            inputValue: type == "checkbox" ? 1 : initialValue,
            inputPlaceholder: inputPlaceholder,
            showCancelButton: true,
            inputValidator: (value) => {
              return !value && "不能为空";
            },
          });
          input_val = inputValue;
        } else if (["email", "url"].indexOf(type) !== -1) {
          const { value: inputValue } = await Swal.fire({
            title: title,
            input: type,
            inputLabel: label_text,
            inputValue: initialValue,
            inputPlaceholder: inputPlaceholder,
            validationMessage: type == "email" ? "不合法的Email格式" : "不合法的URL格式",
            showCancelButton: true,
          });
          input_val = inputValue;
        } else if (["select", "radio"].indexOf(type) !== -1) {
          const { value: inputValue } = await Swal.fire({
            title: title,
            input: type,
            inputOptions: params["options"],
            inputPlaceholder: inputPlaceholder,
            showCancelButton: true,
            inputValidator: (value) => {
              return !value && "不能为空";
            },
          });
          input_val = inputValue;
        } else if (["checkbox"].indexOf(type) !== -1) {
          const { value: inputValue } = await Swal.fire({
            title: title,
            input: type,
            inputLabel: label_text,
            inputValue: type == "checkbox" ? 1 : initialValue,
            inputPlaceholder: inputPlaceholder,
            showCancelButton: true,
            inputValidator: (value) => {
              return !value && "不能为空";
            },
          });
          input_val = inputValue;
        } else if (type == "date") {
          const { value: inputValue } = await Swal.fire({
            title: title,
            input: "date",
            didOpen: () => {
              const today = new Date().toISOString();
              Swal.getInput().min = today.split("T")[0];
            },
          });
          input_val = inputValue;
        }

        if (input_val) {
          console.log(input_val);
        }
      },
    };

    const app = {
      msg: () => gbl_alert.showBasicMsg("hello"),
      showSuccess: () =>
        gbl_alert.showTypeMsg("success", "操作成功", 3000, function () {
          console.log("alert closed");
        }),
      showError: () => gbl_alert.showTypeMsg("error", "操作失败", 0),
      showInfo: () => gbl_alert.showTypeMsg("info", "新数据", 0),
      showWarning: () => gbl_alert.showTypeMsg("warning", "显示有误", 0),
      confirm: () =>
        gbl_alert.showConfirmMsg(
          "确定要删除吗?",
          "删除后不可恢复",
          function () {
            console.log("confirmed");
          },
          function () {
            console.log("canceled");
          }
        ),
      input_text: () => gbl_alert.input("text", "请输入你的名字", "名字", "输入你的名字"),
      input_email: () => gbl_alert.input("email", "请输入你的邮箱", "邮箱", "输入你的邮箱"),
      input_url: () => gbl_alert.input("url", "请输入URL地址", "URL地址", "输入URL地址"),
      input_password: () => gbl_alert.input("password", "请输入密码", "密码", "输入密码"),
      input_textarea: () => gbl_alert.input("textarea", "请输入备注", "备注", "输入备注"),
      input_select: () =>
        gbl_alert.input("select", "请选择喜欢的食物", "喜欢的食物", "请选择喜欢的食物", "", {
          options: {
            Fruits: {
              apples: "Apples",
              oranges: "Oranges",
            },
            Vegetables: {
              broccoli: "Broccoli",
              carrot: "Carrot",
            },
            icecream: "Ice cream",
          },
        }),
      input_radio: () =>
        gbl_alert.input("radio", "请选择喜欢的颜色", "喜欢的颜色", "选择喜欢的颜色", "", {
          options: {
            "#ff0000": "Red",
            "#00ff00": "Green",
            "#0000ff": "Blue",
          },
        }),
      input_checkbox: () => gbl_alert.input("checkbox", "合同条款", "是否同意", "是否同意"),
      input_date: () => gbl_alert.input("date", "请输入日期", "日期", "输入日期"),
    };
  </script>
</html>

简单消息

带图标的消息

需确认的消息

输入框

选择框

可验证输入

单选框

复选框

 

标签:title,gbl,js,alert,inputValue,input,sweetalert2,type,弹窗
From: https://www.cnblogs.com/caroline2016/p/18431751

相关文章

  • 如何正确的在项目中接入微信JS-SDK
    微信JS-SDK的功能如果你点进来,那么我相信你应该知道微信的JS-SDK可以用来做什么了。微信的官方文档描述如下。微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照、选图、语音、位置等手机系统的......
  • node/expressjs 连接与操作 MongoDB
    MongoDB 的安装、配置、启动、常见指令等,详见上一节“mongoDB简介” 以下将讲述 node/expressjs 与 mongoDB 的交互——连接与操作数据库 mongoDB注释:以下示例是采用express官网的生成器初始化项目的。数据库 mongoDB的操作运用的是mongoose插件, mong......
  • 基于nodejs+vue校园礼品销售系统[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景在数字化时代,随着校园生活的日益丰富与多元化,学生对于个性化、创意化礼品的需求日益增长。然而,传统的校园礼品销售模式往往受限于时间、空间及信息不对称等......
  • 基于nodejs+vue校园零食商城系统[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网的飞速发展和数字化校园建设的不断推进,学生群体的消费习惯正悄然发生变化。传统校园内,学生购买零食往往依赖于实体小卖部或便利店,存在时间、空间......
  • 基于nodejs+vue校园论坛[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展,校园信息化已成为提升教育质量、促进师生交流的重要途径。传统校园生活中,信息的传递往往受限于时间、空间,学生间的交流互动也局限......
  • Vue.js中的深度监听:理解其工作原理及应用
    在Vue.js中,深度监听是指能够监测到对象内部属性变化的能力。默认情况下,Vue通过数据劫持(datahijacking)来实现响应式系统,这包括了对对象属性的访问和修改进行拦截。但是,这种监听是浅层的,即它只会监听对象本身的属性变化,而不会递归地监听对象内部的属性变化。深度监听的实现Vue.js......
  • react之jsx基础(2)高频使用场景
    在React中,JSX的使用是非常广泛和高频的。以下是一些常见的高频使用场景及其示例,帮助你更好地理解JSX的实际应用:1.组件定义JSX最常见的用途之一是定义组件的结构。组件可以是函数组件或类组件,通常会使用JSX来描述组件的UI。函数组件示例:functionGreeting(props){......
  • 像JSON一样使用ProtoBuf,空间还能缩小60%,性能提升100%
    引言在前面《释放你九成的带宽和内存:GZIP在解决Redis大Key方面的应用》一文中我使用GZIP算法可以将JSON格式数据的大小缩小88%从而节省了大量的存储和带宽资源,本文介绍另一种JAVA对象序列化神器——ProtoBuf(ProtocolBuffers(),它是由Google开发的一种用于序列化结构化数据的高效、......
  • 关于在vue2中自定义右键弹窗
            所需变量//右键点击的弹框对象rightDialogbox:null,//鼠标点击后获取的文本chooseText:'',//弹窗的偏移left:'',top:'',//右键点击的弹框显隐rightDialogShow:false,一、阻止原生事件......
  • vue 浏览器指纹-fingerprintjs
    FingerprintJS是一个用于创建用户浏览器指纹的开源库。在Vue应用中使用FingerprintJS可以帮助你追踪用户的浏览器信息,从而实现个性化的服务或者分析用户行为。首先,你需要安装FingerprintJS:npminstall@fingerprintjs/fingerprintjs然后,你可以在Vue组件中使用它来创......