12-22

102 阅读1分钟
    $(function () {
        load();


        $(".table").on("click", ".glyphicon-remove", function () {
            var id = $(this).parent().attr("data-id");
            if (confirm(`确定要删除${id}?`)) {
                $.ajax({
                    url: "http://localhost:3000/user",
                    type: "DELETE",
                    data: { _id: id },
                    success: function (res) {
                        if (res.status === 200) {
                            load();
                        }
                    }

                })
            }
        })


        $(".table").on("click", ".glyphicon-edit", function () {
            // 动态
            var id = $(this).parent().attr("data-id");

            $.ajax({
                url: `http://localhost:3000/user/${id}`,
                type: "GET",
                success: function (res) {
                    if (res.status === 200) {
                        const data = res.data;
                        // 静态
                        $(".modal-title").html("修改文章");
                        $("#_id").val(id);
                        $("#title").val(data.title)
                        $("#content").val(data.content)
                        $("#top").prop("checked", data.top)
                        // 显示
                        $("#modal-id").modal("show");
                    }
                }
            })
            $('#modal-id').on('hidden.bs-modal',function(){
                $("#_id").val("");
                $("#title").val("");
                $("#content").val("");
                $("#top").prop("checked", false);
            })
        })


        $("#modal-id form").validate({
            submitHandler: function () {
                var id = $("#_id").val();
                var title = $("#title").val();
                var content = $("#content").val();
                var top = $("#top").prop("checked");
                if (id) {
                    $.ajax({
                        url: `http://localhost:3000/user/${id}`,
                        type: "PUT",
                        data: { title, content, top },
                        success: function (res) {
                            if (res.status === 200) {
                                $("#modal-id").modal("hide");
                                load();
                            } else {
                                alert(res.msg);
                            }
                        }

                    })
                }
                else {
                    $.ajax({
                        url: "http://localhost:3000/user",
                        type: "POST",
                        data: { title, content, top },
                        success: function (res) {
                            if (res.status === 200) {
                                $("#modal-id").modal("hide");
                                load();
                            } else {
                                alert(res.msg);
                            }
                        }

                    })
                }
            },
            rules: {
                title: {
                    required: true,
                    minlength: 6,
                    maxlength: 12
                },
                content: {
                    required: true,
                    minlength: 10,
                    maxlength: 50
                }
            },
            messages: {
                title: {
                    required: "请输入标题",
                    minlength: "标题最少6个字符",
                    maxlength: "标题最多12个字符"
                }
            }

        })


        $("#modal-id").on("click", ".btn-primary", function () {
            $(this).closest(".modal-content").find("form").submit();
        })