• 在Layui中实现开关按钮的效果实例


    今天看过几篇博客,看的有点乱,下面分享一个layui的开关按钮,不知道你们看这个会不会乱呢,hhh;

    按钮代码如下:

    {field: 'state', title: '状态',  85, templet: function (data) {
                        if (data.state == 0) {
                            return '<div> <input type="checkbox" checked="" name="codeSwitch" lay-skin="switch" id="open" lay-filter="switchTest" switchId=' + data.userId + '' +
                                ' lay-text="启用|已禁用"  value=' + data.state + '></div>';
                        }
                        return '<div> <input type="checkbox" lay-skin="switch" name="codeSwitch"  switchId=' + data.userId + ' lay-filter="switchTest"' +
                            'lay-text="启用|已禁用" value=' + data.state + '></div>';
    
                    }
                }

     

    实现操作js代码如下:

     /**
         * 监听开关 状态 操作
         */
        form.on('switch(switchTest)', function (data) {
            /**
             * 禁用标签
             * 状态 赋值为 1
             */
            var layerIndex = layer.load(3);
    
            if ((this.checked ? 'true' : 'false') == 'false') {
                $.ajax({
                    url: '/users/delete',
                    data: {
                        state: 1,
                        userId: data.elem.getAttribute("switchId")
                    },
                    type: 'PUT', //HTTP请求类型
                    success: function (data) {
                        console.log(data);
    
                        $.message({
                            message: "禁用用户",
                            type: 'success',
                            showClose: true
                        });
    
                    }, error: function () {
                        $.message({
                            message: "boom..",
                            type: 'error',
                            showClose: true
                        });
                    }
    
                })
            } else {
                /**
                 * 启动标签
                 * 状态 赋值为 0
                 */
                $.ajax({
                    url: '/users/delete',
                    data: {
                        state: 0,
                        userId: data.elem.getAttribute("switchId")
                    },
                    type: 'PUT',
                    success: function (data) {
                        console.log(data);
                        $.message({
                            message: "启动用户",
                            type: 'success',
                            showClose: true
                        });
    
                    }, error: function () {
                        $.message({
                            message: "boom..",
                            type: 'error',
                            showClose: true
                        });
                    }
    
                })
            }
            layer.close(layerIndex);
    
        });

    直接复制粘贴就可以用了(记得改下id与状态哦)

     

    效果图如下:

    提示框用的是bootstrap框架的

     

     

     

    下面两行代码可以实现页面的加载效果,第一行放在操作的开头,第二行放在操作的结束

    var layerIndex = layer.load(3);  
    layer.close(layerIndex);

      

    码云地址:https://gitee.com/ckfeng/springboot_mysql_redis.git

    感谢来访!

     

  • 相关阅读:
    Linux环境下入侵工具Knark的分析及防范 java程序员
    六个步骤即可防范ARP地址欺骗类病毒 java程序员
    巧用命令行 揪出ARP欺骗病毒母机方法 java程序员
    poj3264Balanced Lineup(线段树RMQ)
    有了1A的把握再去提交
    poj2828Buy Tickets(线段树 单点更新+区间求和+区间第K值)
    poj2513Colored Sticks(无向图判欧拉路、回路+trie树)
    ACM数学(转)
    sdut2381Broken Keyboard
    sdut2383Decode the Strings(循环节)
  • 原文地址:https://www.cnblogs.com/ckfeng/p/13067029.html
Copyright © 2020-2023  润新知