分类的增删改查基本完成

master
barney 2 years ago
parent a90a200bcc
commit 9f2d8fb4f6
  1. 27
      src/main/java/cc/bnblogs/controller/CategoryController.java
  2. 2
      src/main/java/cc/bnblogs/enums/ResultEnum.java
  3. 68
      src/main/resources/templates/admin/category.html

@ -4,7 +4,6 @@ import cc.bnblogs.common.Result;
import cc.bnblogs.enums.ResultEnum; import cc.bnblogs.enums.ResultEnum;
import cc.bnblogs.pojo.Category; import cc.bnblogs.pojo.Category;
import cc.bnblogs.service.CategoryService; import cc.bnblogs.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -47,6 +46,32 @@ public class CategoryController {
} }
} }
/**
* 保存修改后的分类数据
* @param category 修改的分类
* @return 修改结果
*/
@PostMapping("/")
public Result<String> save(Category category) {
categoryService.save(category);
return Result.success();
}
/**
* 删除一个分类
* @param id 删除的分类id
* @return 删除结果
*/
@DeleteMapping("/{id}")
public Result<String> delete(@PathVariable Integer id) {
categoryService.delete(id);
return Result.success();
}
} }

@ -17,7 +17,7 @@ public enum ResultEnum {
RESULT_NOT_FOUND(404,"数据不存在"), RESULT_NOT_FOUND(404,"数据不存在"),
RESULT_ERROR(500,"系统错误"); RESULT_ERROR(500,"请求错误");
private final Integer code; private final Integer code;

@ -8,7 +8,7 @@
<div class="container lw-main lw-banner"> <div class="container lw-main lw-banner">
<div class="btn-group" role="group" style="margin-bottom: 20px" aria-label="..."> <div class="btn-group" role="group" style="margin-bottom: 20px" aria-label="...">
<button type="button" class="btn btn-default"><i class="fa fa-plus"></i> 新增</button> <button type="button" id="create-category-btn" class="btn btn-default"><i class="fa fa-plus"></i> 新增</button>
</div> </div>
<table id="data-table"></table> <table id="data-table"></table>
</div> </div>
@ -17,6 +17,7 @@
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<div class="modal-content"> <div class="modal-content">
<form id="data-form"> <form id="data-form">
<input type="hidden" name="id">
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button> aria-hidden="true">&times;</span></button>
@ -26,7 +27,7 @@
<div class="form-group"> <div class="form-group">
<label>标题</label> <label>标题</label>
<input type="text" name="name" class="form-control" placeholder="请输入标题..."> <input type="text" name="name" class="form-control" required placeholder="请输入标题...">
</div> </div>
<div class="form-group"> <div class="form-group">
@ -46,8 +47,7 @@
<th:block th:include="admin/common::footer"></th:block> <th:block th:include="admin/common::footer"></th:block>
<script> <script>
$(function () { $(function () {
// $("#save-window").modal("show") // 显示表单所有分类数据
$('#data-table').bootstrapTable({ $('#data-table').bootstrapTable({
url: "/admin/category/", url: "/admin/category/",
columns: [ columns: [
@ -84,7 +84,7 @@
return res.data return res.data
} }
}) })
// 编辑对应分类
$("#data-table").on("click",".category-edit-btn",function () { $("#data-table").on("click",".category-edit-btn",function () {
let id = $(this).data("id") let id = $(this).data("id")
$.ajax({ $.ajax({
@ -103,12 +103,64 @@
} }
}) })
console.log(id);
}) })
// 新增分类
$("#create-category-btn").on("click", function () {
$("#window-title").text('新增分类')
// 清空之前表单的数据
$("#data-form").initForm({id: "",name:"",summary: ""})
$('#save-window').modal('show')
})
// 删除分类
$("#data-table").on("click",".category-delete-btn",function () { $("#data-table").on("click",".category-delete-btn",function () {
let id = $(this).data("id") let id = $(this).data('id')
console.log(id); let idx = layer.confirm('是否要删除该数据?', {
btn: ['确认', '取消'] //按钮
}, function () { // 点击确认后删除
$.ajax({
url: '/admin/category/' + id,
method: 'delete',
dataType: 'json',
success: res => {
if (res.code === 200) {
layer.msg("删除成功", {icon: 1, time: 700})
$('#data-table').bootstrapTable('refresh', {silent: true})
} else {
layer.msg(res.message, {icon: 2})
}
}
})
layer.close(idx); // 关闭提示框
})
})
// 更新分类数据
$("#data-form").on("submit",function () {
let data = $(this).serialize();
$.ajax({
url:"/admin/category/",
method: "POST",
data: data,
dataType: "json",
success: res => {
if (res.code === 200) {
// 显示成功信息
layer.msg("保存成功", {icon: 1, time: 600}, function () {
// 关闭模态框
$('#save-window').modal('hide')
// 重载表格数据
$('#data-table').bootstrapTable('refresh',{silent: true})
})
}
else {
layer.msg(res.message, {icon: 2})
}
}
})
return false; // 阻止表单的提交行为
}) })
}) })

Loading…
Cancel
Save