动态路由:
router.put("/content/:id",async ctx=>{
try {
const data = await contentModel.updateOne({_id:ctx.params.id},ctx.request.body);
return success(ctx,data);
} catch (error) {
return fail(ctx, error)
}
})
修改:
$(".table").on("click", ".glyphicon-edit", function(){
//动态
var id = $(this).parent().attr("data-id");
$.ajax({
url:`http://localhost:3007/content/${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 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:3007/content/${id}`,
type:"PUT",
data:{title,content,top},
success:function(res){
if(res.status===200){
// console.log("添加成功");
$("#modal-id").modal("show");
load();
}else{
alert(res.msg); // 错误信息
}
}
})
}else{
$.ajax({
url:"http://localhost:3007/content",
type:"POST",
data:{title,content,top},
success:function(res){
if(res.status===200){
// console.log("添加成功");
$("#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个字符"
},
content:{
required:"请输入正文",
minlength:"标题最少10个字符",
maxlength:"标题最多50个字符"
}
}
})
pagination: true,//启用分页 // pageSize: 1, //分页大小 pageList:[1,2,3],// 分页大小 自定义 showToggle:true,//列表视图/表格视图 切换 showPaginationSwitch:true,//分页开关 是否启用分页 showFullscreen:true,//全屏显示 // showRefresh:true,//刷新 仅支持url数据源 search:true,//启用查询 showSearchButton:true,//显示查询按钮 输入框+按钮 clickToSelect:true,//点击这一行选中 showPrint:true,//打印 showExport:true,//导出