layui使用者新增實現

ζMonologue°發表於2020-11-23
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="submit" onclick="sub();return false;" class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
<script type="text/javascript">
    <!--載入form模組-->
    layui.use("form",function () {
        let  form = layui.form;
        //渲染所有元素
        form.render();
    });
    layui.use("laydate",function () {
        let laydate = layui.laydate;
        laydate.render({
            elem: '#date1',
            type: 'datetime'
        });
    });

   function sub() {
        $.post({
            url: "/admin/user-add",
            dataType: 'json',
            data: {
                username: $('#username').val(),
                password: $('#password').val(),
                nickname: $('#nickname').val(),
                email: $('#email').val(),
                phone: $('#phone').val(),
                gender: $('.gender').val()
            },
            success: function (data) {

                layer.alert(data.success);
                window.parent.location.href="/admin/user-list";
            },
            error: function (error) {
                layer.msg(error);
            }
        });
    };

</script>
        //監聽頭部工具欄的點選
        table.on('toolbar(user_table)', function (obj) {
            let event = obj.event;
            let data = obj.data;
            if (event === 'addUser') {
                //新增操作
                let index = layer.open({
                    title: '新增使用者',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '80%'],
                    content: '/admin/user-add',
                });
                //視窗自適應
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
    @PostMapping("/user-add")
    @ResponseBody
    public String addUser(User user) {

        JSONObject object = new JSONObject();
        int addUser = userService.addUser(user);
        if (addUser == 1) {
            object.put("success", "新增成功");
            return object.toJSONString();
        }else {
            object.put("default", "新增失敗");
            return object.toJSONString();
        }
    }

 

相關文章