Springboot快速整合bootstrap-table使用,接口对接

这个表格加持还是不错了,自带了全局搜索,分页,数据导出,卡片视图,等,本次整合添加了数据添加弹窗和编辑数据回显弹窗,附完整页面代码,只需要拿过来替换自己实际的接口即可。

效果图

image-20240325192345466

接口案例

数据列表:

{"code": 200,"msg": "ok","data": [{"categoryId": 52,"categoryName": "哈哈哈"},{"categoryId": 53,"categoryName": "哈哈哈地方"},{"categoryId": 7,"categoryName": "悬疑灵异"},{"categoryId": 4,"categoryName": "武侠仙侠"},{"categoryId": 3,"categoryName": "玄幻奇幻"}]
}

增:/novel-category/add

删:/novel-category/delete/{id}

改:/novel-category/update

查:/novel-category/list

批量删:/novel-category/deleteBatch

对应后端实现

package com.xxx.readverse.controller;import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.readverse.entity.Category;
import com.xxx.readverse.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.Arrays;
import java.util.List;@Controller
@RequestMapping("/novel-category")
@Api(tags = "小说分类")
@CrossOrigin
public class CategoryController {@Autowiredprivate CategoryService novelCategoryService;@GetMapping("/list")@ApiOperation("分类列表")@ResponseBodypublic SaResult getNovelCategories() {List<Category> categoryList = novelCategoryService.list(new QueryWrapper<Category>().last("limit 12"));return SaResult.data(categoryList);}@PostMapping("/add")@ApiOperation("新增分类")@ResponseBodypublic SaResult add(Category novelCategory) {try {novelCategoryService.save(novelCategory);} catch (DuplicateKeyException e) {return SaResult.error("操作未成功,可能是因为数据重复导致的");}return SaResult.data(novelCategory.getCategoryId());}@GetMapping("/deleteBatch")@ApiOperation("批量删除")@ResponseBodypublic SaResult deleteBatch(@RequestParam("ids") Integer[] ids) {novelCategoryService.removeByIds(Arrays.asList(ids));return SaResult.ok();}@GetMapping("/delete/{id}")@ApiOperation("删除分类")@ResponseBodypublic SaResult delete(@PathVariable Integer id) {novelCategoryService.removeById(id);return SaResult.ok();}@PostMapping("/update")@ApiOperation("修改分类")@ResponseBodypublic SaResult update(Category novelCategory) {novelCategoryService.updateById(novelCategory);return SaResult.ok();}@GetMapping("/{id}")@ApiOperation("获取某个分类信息")@ResponseBodypublic SaResult getById(@PathVariable Integer id) {Category category = novelCategoryService.getById(id);return  SaResult.data(category);}}

页面完整代码

需要注意的问题

  1. 注意依赖引进

  2. 方法异步操作,特别是弹窗组件

  3. 数据项的ID,并不是每个都是id,根据实体数据来确认。

  4. 新增,编辑使用弹窗,控制弹窗的显示和关闭,$(‘xxx’).modal(‘show’), $(‘xxx’).modal(‘hide’)

  5. 基本通用,直接换接口名称就行。

<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>表格演示</title><link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"><link href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.3/dist/bootstrap-table.min.css" rel="stylesheet"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.3/dist/bootstrap-table.min.js"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.3/dist/locale/bootstrap-table-zh-CN.min.js"></script><link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.28.0/tableExport.min.js"></script><script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script><style>.select,#locale {width: 100%;}.edit {margin-right: 10px;}</style>
</head><body><!-- 新增弹窗 --><!-- Modal Body --><!-- if you want to close by clicking outside the modal, delete the last endpoint:data-bs-backdrop and data-bs-keyboard --><div class="modal fade" id="addModal" tabindex="-1" data-bs-backdrop="static" data-bs-keyboard="false" role="dialog"aria-labelledby="modalTitleId" aria-hidden="true"><div class="modal-dialog modal-dialog-scrollable modal-dialog-centered modal-md" role="document"><form id="addForm"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="modalTitleId">新增</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><div class="form-floating mb-3"><input type="text" class="form-control" name="categoryName" id="categoryName" placeholder="" /><label for="categoryName">Name</label></div></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button><button type="reset" class="btn btn-outline-danger">清空</button><button type="submit" class="btn btn-primary">保存</button></div></div></form></div></div><!-- 编辑弹窗 --><!-- Modal Body --><!-- if you want to close by clicking outside the modal, delete the last endpoint:data-bs-backdrop and data-bs-keyboard --><div class="modal fade" id="editModal" tabindex="-1" data-bs-backdrop="static" data-bs-keyboard="false" role="dialog"aria-labelledby="modalTitleId" aria-hidden="true"><div class="modal-dialog modal-dialog-scrollable modal-dialog-centered modal-md" role="document"><form id="editForm"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="modalTitleId">编辑</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><input type="hidden" name="categoryId"><div class="form-floating mb-3"><input type="text" class="form-control" name="categoryName" id="categoryName" placeholder="" /><label for="categoryName">Name</label></div></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button><button type="reset" class="btn btn-outline-danger">清空</button><button type="submit" class="btn btn-primary">保存</button></div></div></form></div></div><div class="container"><!-- 自定义工具栏 --><div id="toolbar"><button id="add" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal"><i class="bi bi-plus-square"></i> 新增</button><button id="export" class="btn btn-success"><i class="bi bi-file-earmark-spreadsheet"></i> 导出</button><button id="remove" class="btn btn-danger" disabled><i class="bi bi-trash"></i> 批量删除</button></div><!-- 表格初始化 --><table id="table" data-toolbar="#toolbar" data-search="true" data-show-refresh="true" data-show-toggle="true"data-show-fullscreen="true" data-show-columns="true" data-show-columns-toggle-all="true" data-detail-view="true"data-show-export="true" data-click-to-select="true" data-detail-formatter="detailFormatter"data-minimum-count-columns="2" data-show-pagination-switch="true" data-pagination="true" data-id-field="id"data-page-list="[5,10, 25, 50, 100, all]" data-show-footer="true" data-side-pagination="client"data-url="/novel-category/list" data-response-handler="responseHandler"></table></div></body>
<script>// 获取组件var $table = $('#table')var $remove = $('#remove')var selections = []// 获取被选中行的IDfunction getIdSelections() {return $.map($table.bootstrapTable('getSelections'), function (row) {return row.categoryId})}// 处理远程响应的数据,可以指定要在表格显示的数据function responseHandler(res) {return res.data;}// 自定义显示样式function detailFormatter(index, row) {var html = []$.each(row, function (key, value) {html.push('<p><b>' + key + ':</b> ' + value + '</p>')})return html.join('')}function operateFormatter(value, row, index) {return ['<button type="button" class="btn btn-outline-primary edit btn-sm" title="编辑">','<i class="bi bi-pencil"></i> 编辑','</button> ','<button type="button" class="btn btn-outline-danger remove btn-sm" title="删除">','<i class="bi bi-trash"></i> 删除','</button>'].join('');}// 按钮点击事件window.operateEvents = {'click .edit': function (e, value, row, index) {// 弹窗编辑回显$.each(row, function (key, value) {$('#editForm input[name="' + key + '"]').val(value);// 如果表单字段是<input>标签之外的其他类型,也可以使用类似的方式进行赋值});$editModal.modal('show');// alert('You click edit action, row: ' + JSON.stringify(row))},// 移除'click .remove': async function (e, value, row, index) {if (await askDelete()) {// 从表格中移除选中的行remove(row.categoryId);}}}// 表格初始化function initTable() {$table.bootstrapTable('destroy').bootstrapTable({exportDataType: 'all',height: 550,locale: $('#locale').val(),columns: [[{field: 'state',checkbox: true,align: 'center',valign: 'middle'},{title: 'ID编号',field: 'categoryId',align: 'center',valign: 'middle',sortable: true,},{title: '分类名',field: 'categoryName',sortable: true,align: 'center'},{field: 'operate',title: '操作',align: 'center',clickToSelect: false,events: window.operateEvents,formatter: operateFormatter}]]})// 监听表格的选择事件,当表格中的行被选中或取消选中时触发$table.on('check.bs.table uncheck.bs.table ' +'check-all.bs.table uncheck-all.bs.table',function () {// 根据当前选中的行的数量来启用或禁用删除按钮$remove.prop('disabled', !$table.bootstrapTable('getSelections').length)// 保存你的数据,这里只保存当前页的数据selections = getIdSelections()console.log("当前选中:" + selections)// 如果你想要保存所有选中的数据,可以在这里使用 push 或 splice 方法})// 监听表格的所有事件,用于调试目的$table.on('all.bs.table', function (e, name, args) {// console.log(name, args)})// 点击删除按钮时执行的操作$remove.click(async function () {// 获取所有选中行的 IDvar ids = getIdSelections()if (await askDelete()) {// 从表格中移除选中的行console.log("要删除的ids:" + ids)removeBatch(ids);// 禁用删除按钮$remove.prop('disabled', true)}})}// 绑定导出按钮点击事件$('#export').click(function () {$table.tableExport({type: 'excel', // 导出文件类型,可选 'csv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png', 'pdf'escape: 'false' // 是否使用转义,默认为 true});});$(function () {initTable();$('#locale').change(initTable)})// 删除确认弹窗async function askDelete() {const result = await Swal.fire({title: "确定要删除它吗?",text: "删除后无法恢复!",icon: "warning",showCancelButton: true,confirmButtonColor: "#3085d6",cancelButtonColor: "#d33",confirmButtonText: "确定!"});return result.isConfirmed;}// 操作提示function mess() {Swal.fire({position: "top-end",icon: "success",title: "操作OK",showConfirmButton: false,timer: 1500});}var $addModal = $('#addModal');var $editModal = $('#editModal');// 新增$('#addForm').submit(function (event) {event.preventDefault();var data = $('#addForm').serialize();console.log(data);save(data)$addModal.modal('hide');})// 保存编辑$('#editForm').submit(function (event) {event.preventDefault();var data = $('#editForm').serialize();console.log(data);update(data);$editModal.modal('hide');})function save(data) {$.post("/novel-category/add", data, function (data) {if (data.code === 200) {mess();refresh();}})}function update(data) {$.post("/novel-category/update", data, function (data) {if (data.code === 200) {mess();refresh();}})}function remove(id) {$.get("/novel-category/delete/" + id, function (data) {if (data.code === 200) {refresh();mess();}})}function removeBatch(data) {$.get("/novel-category/deleteBatch?ids=" + data.join(','), function (data) {if (data.code === 200) {mess();refresh();}})}function refresh() {$table.bootstrapTable('refresh');}</script></html>

注意数据的对接,特别是每行数据的id项,根据实际数据调整。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/772533.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙实战开发-如何通过拖动滑块调节应用内字体大小

介绍 本篇Codelab将介绍如何使用基础组件Slider&#xff0c;通过拖动滑块调节应用内字体大小。要求完成以下功能&#xff1a; 实现两个页面的UX&#xff1a;主页面和字体大小调节页面。拖动滑块改变字体大小系数&#xff0c;列表页和调节页面字体大小同步变化。往右拖动滑块字…

ppp验证实验

实际操作图 1&#xff0c;IP划分分配 [r1]interface Serial 4/0/0 [r1-Serial4/0/0]ip add 192.168.1.1 24 [r2]interface Serial 4/0/0 [r2-Serial4/0/0]ip address 192.168.1.2 24 [r2]int Mp-group 0/0/0 [r2-Mp-group0/0/0]ip add 192.168.2.1 24 [r3]int Mp-group 0/…

Xcode Launching “XXX“ is taking longer than expected

文章目录 1.问题2.如何进入iOS DeviceSupport目录3.解决方法4.参考博客 1.问题 LLDB is likely reading from device memory to resolve symbols 2.如何进入iOS DeviceSupport目录 3.解决方法 进入iOS DeviceSupport目录&#xff0c;删除该真机对应的架构文件&#xff08;比如…

QT作业day3

1、使用手动连接&#xff0c;将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在自定义的槽函数中调用关闭函数 将登录按钮使用qt5版本的连接到自定义的槽函数中&#xff0c;在槽函数中判断ui界面上输入的账号是否为"admin"&#xff0c;密码是…

JMeter 如何并发执行 Python 脚本

要在JMeter中并发执行Python脚本&#xff0c;可以使用Jython脚本或通过调用外部Python脚本的方式实现。 使用Jython脚本并发执行Python脚本的步骤&#xff1a; 1、创建一个线程组&#xff1a;在JMeter界面中&#xff0c;右键点击测试计划&#xff0c;选择 “添加” -> “线…

c语言文件操作(下)

目录 1.文件的随机读写1.1 fseek1.2 ftell1.3 rewind 2. 文件结束的判定2.1 文本文件读取结束的判断2.2 二进制文件读取结束的判断 3. 文件缓冲区 1.文件的随机读写 1.1 fseek 根据⽂件指针的位置和偏移量来定位⽂件指针。 函数原型&#xff1a; int fseek (FILE * stream,…

Springboot+vue的企业质量管理系统(有报告)。Javaee项目,springboot vue前后端分离项目。

演示视频&#xff1a; Springbootvue的企业质量管理系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot vue前后端分离项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09…

黑马头条day5总结

1、surefire-reports for the individual test results. 借鉴&#xff1a;【已解决】surefire-reports for the individual test results.-CSDN博客 Please refer to D:\javashizhan01\heima-leadnews\heima-leadnews-service\heima-leadnews-article\target\surefire-report…

HTML(二)---【常见的标签使用】

零.前言 本文只介绍常见的标签使用&#xff0c;其中使用的一些HTML专业术语可以在作者的第一篇文章&#xff1a; HTML&#xff08;一&#xff09;---【基础】-CSDN博客中找到。 一.<b>粗体、<i>或<em>斜体 1.定义 粗体、斜体的实现可以在CSS中实现&…

【半结构化访谈法】

文章目录 什么是半结构化访谈法&#xff1f;如何进行半结构化访谈&#xff1f;1. 确定研究目的和主题2. 制定访谈指南3. 进行访谈4. 记录和分析数据5. 报告结果 半结构化访谈法的优缺点优点&#xff1a;缺点&#xff1a; 什么是半结构化访谈法&#xff1f; 半结构化访谈法是一…

Windows 和 Linux 的免费媒体播放器 - SMPlayer

Windows 和 Linux 的免费媒体播放器 - SMPlayer 1. A quick look at SMPlayer2. Downloads2.1. Packages for Ubuntu References https://www.smplayer.info/ 1. A quick look at SMPlayer SMPlayer is a free media player for Windows and Linux with built-in codecs that…

【MySQL】事务日志

事务的隔离性由锁机制实现&#xff0c;事务的原子性、一致性和持久性由redo日志和undo日志实现。 一、redo日志 1.1、为什么需要redo日志 一方面&#xff0c;由于数据从内存写回磁盘需要一定的时间&#xff0c;假如在事务提交后&#xff0c;还没有写回磁盘&#xff0c;数据库…

优雅地处理前端数据转换:自定义封装 translateDict 函数

在前端开发中&#xff0c;我们经常需要处理数据的转换。有时候&#xff0c;我们需要将某种格式的数据转换为另一种格式&#xff0c;这可能涉及到字符串、数组等不同数据类型的转换。在这篇博客中&#xff0c;我们将介绍一个名为 translateDict 的函数&#xff0c;它可以帮助我们…

Rancher(v2.6.3)——Rancher部署Nginx(单机版)

Rancher部署Nginx详细说明文档&#xff1a;https://gitee.com/WilliamWangmy/snail-knowledge/blob/master/Rancher/Rancher%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3.md#5rancher%E9%83%A8%E7%BD%B2nacos ps&#xff1a;如果觉得作者写的还行&#xff0c;能够满足您的需求&#x…

LeetCode225:用队列实现栈

题目描述 请你仅使用两个队列实现一个后入先出&#xff08;LIFO&#xff09;的栈&#xff0c;并支持普通栈的全部四种操作&#xff08;push、top、pop 和 empty&#xff09;。 实现 MyStack 类&#xff1a; void push(int x) 将元素 x 压入栈顶。 int pop() 移除并返回栈顶元…

web前端面试题---->HTML、CSS

一.居中方法 block元素如何居中 margin&#xff1a;0 auto&#xff1b;position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);flex布局&#xff1a; 对父元素操作 &#xff1a; justify-content:center; al…

商城小程序项目实现监控的可观测性最佳实践

前言 微信小程序是一种轻量级的应用程序&#xff0c;用户可以在微信内直接使用&#xff0c;无需下载安装。它具有独立的开发框架和生态系统&#xff0c;支持丰富的功能和交互&#xff0c;包括社交、购物、服务等。 观测云对微信小程序的监控能够实时收集性能指标、错误日志和…

深入理解 C++ 中的 IO 流【iostream篇】

C 中的输入输出流&#xff08;iostream&#xff09;是非常重要的一部分&#xff0c;它们提供了与用户交互以及与文件系统进行数据交换的功能。本文将深入探讨 C 中的 cin 和 cout&#xff0c;介绍它们的使用方法、缓冲区以及常用的成员函数等相关知识。 1. cin 和 cout 的基本…

[蓝桥杯 2019 省 B] 特别数的和

题目描述 小明对数位中含有 22、00、11、99 的数字很感兴趣&#xff08;不包括前导 00&#xff09;&#xff0c;在 11 到 4040 中这样的数包括 11、22、99、1010 至 3232、3939 和 4040&#xff0c;共 2828 个&#xff0c;他们的和是 574574。 请问&#xff0c;在 11 到 &…

分布式系统CAP理论

1、什么是CAP理论 C是Consistency(强一致性)、A是Availability(可用性)、P是Partition Tolerance(分区容错性)&#xff0c;一个分布式系统不可能同时很好的满足—致性、可用性和分区容错性这三个需求&#xff0c;不能同时成立&#xff0c;最多只能同时满足其中的两项&#xff…