• springboot整合vue2实现简单的新增删除,整合ECharts实现图表渲染


    先看效果图:

    1.后端接口

    1. // 查询所有商品信息
    2. // @CrossOrigin(origins = "*")
    3. @RequestMapping("/list1")
    4. @ResponseBody
    5. public List list1(){
    6. List list = goodsService.list();
    7. return list;
    8. }
    9. // 删除
    10. // @CrossOrigin(origins = "*")
    11. @RequestMapping("/del")
    12. @ResponseBody
    13. public String del(int id){
    14. try {
    15. goodsService.removeById(id);
    16. return "删除成功";
    17. }catch (Exception e){
    18. return "删除失败";
    19. }
    20. }
    21. // 根据条件查询
    22. @RequestMapping("/search")
    23. @ResponseBody
    24. public List search(String goodsname,String goodsType){
    25. System.out.println(goodsname+","+goodsType);
    26. QueryWrapper wrapper = new QueryWrapper<>();
    27. wrapper.like(StringUtils.isNotBlank(goodsname),"name",goodsname).like(StringUtils.isNotBlank(goodsType),"goods_type",goodsType);
    28. List list = goodsService.list(wrapper);
    29. for (Goodsinfo goodsinfo : list) {
    30. System.out.println(goodsinfo);
    31. }
    32. return list;
    33. }
    34. // 添加数据
    35. @RequestMapping("/add")
    36. @ResponseBody
    37. public String add(@RequestBody Goodsinfo goodsinfo){
    38. System.out.println(goodsinfo);
    39. goodsService.save(goodsinfo);
    40. return "添加成功";
    41. }

    2.前端页面:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>Documenttitle>
    7. <link
    8. rel="stylesheet"
    9. href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
    10. />
    11. <style>
    12. .red {
    13. color: red!important;
    14. }
    15. .search {
    16. width: 300px;
    17. margin: 20px 0;
    18. }
    19. .my-form {
    20. display: flex;
    21. margin: 20px 0;
    22. }
    23. .my-form input {
    24. flex: 1;
    25. margin-right: 20px;
    26. }
    27. .table > :not(:first-child) {
    28. border-top: none;
    29. }
    30. .contain {
    31. display: flex;
    32. padding: 10px;
    33. }
    34. .list-box {
    35. flex: 1;
    36. padding: 0 30px;
    37. }
    38. .list-box a {
    39. text-decoration: none;
    40. }
    41. .echarts-box {
    42. width: 600px;
    43. height: 400px;
    44. padding: 30px;
    45. margin: 0 auto;
    46. border: 1px solid #ccc;
    47. }
    48. tfoot {
    49. font-weight: bold;
    50. }
    51. @media screen and (max-width: 1000px) {
    52. .contain {
    53. flex-wrap: wrap;
    54. }
    55. .list-box {
    56. width: 100%;
    57. }
    58. .echarts-box {
    59. margin-top: 30px;
    60. }
    61. }
    62. style>
    63. head>
    64. <body>
    65. <div id="app">
    66. <div class="contain">
    67. <div class="list-box">
    68. <form class="my-form">
    69. <input type="text" v-model="goods.name" class="form-control" placeholder="名称" />
    70. <input type="text" v-model="goods.price" class="form-control" placeholder="价格" />
    71. <input type="text" v-model="goods.intro" class="form-control" placeholder="简介" />
    72. <input type="text" v-model="goods.amount" class="form-control" placeholder="库存" />
    73. <input type="text" v-model="goods.goodsType" class="form-control" placeholder="类别" />
    74. <input type="text" v-model="goods.remark" class="form-control" placeholder="备注" />
    75. <button type="button" class="btn btn-primary" @click="add()">添加账单button>
    76. form>
    77. <table class="table table-hover">
    78. <thead>
    79. <tr>
    80. <th>编号th>
    81. <th>名称th>
    82. <th>价格th>
    83. <th>简介th>
    84. <th>库存th>
    85. <th>类别th>
    86. <th>备注th>
    87. <th>操作th>
    88. tr>
    89. thead>
    90. <tbody>
    91. <tr v-for="(item,index) in list" :key="item.id">
    92. <td>{{index+1}}td>
    93. <<th>{{item.name}}th>
    94. <th :class="{red:item.price>1}">{{item.price}}th>
    95. <th>{{item.intro}}th>
    96. <th>{{item.amount}}th>
    97. <th>{{item.goodsType}}th>
    98. <th>{{item.remark}}th>
    99. <td><a href="javascript:;" @click="del(item.id)">删除a>td>
    100. tr>
    101. tbody>
    102. <tfoot>
    103. <tr>
    104. <td colspan="4">消费总计: {{totalPrice}}td>
    105. tr>
    106. tfoot>
    107. table>
    108. div>
    109. <div class="echarts-box" id="main">div>
    110. div>
    111. div>
    112. <script src="../echarts.min.js">script>
    113. <script src="../vue.js">script>
    114. <script src="../axios.js">script>
    115. <script>
    116. /**
    117. * 接口文档地址:
    118. * https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc-8df8-0aff8dc317a8/api-53371058
    119. *
    120. * 功能需求:
    121. * 1. 基本渲染
    122. * 2. 添加功能
    123. * 3. 删除功能
    124. * 4. 饼图渲染
    125. */
    126. const app = new Vue({
    127. el: '#app',
    128. data: {
    129. list:[],
    130. goods:{
    131. name:"",
    132. price:"",
    133. amount:"",
    134. intro:"",
    135. goodsType:"",
    136. remark:""
    137. }
    138. },
    139. computed: {
    140. totalPrice () {
    141. return this.list.reduce((sum, item) => sum + item.price, 0)
    142. }
    143. },
    144. created() {
    145. /* axios.get("http://localhost:8080/list1")
    146. .then(res=>{
    147. console.log(res.data);
    148. this.list=res.data;
    149. })
    150. .catch(err=>{
    151. console.log(err)
    152. }) */
    153. this.getlist()
    154. },
    155. mounted() {
    156. this.myChart = echarts.init(document.getElementById("main"));
    157. this.myChart.setOption(
    158. {
    159. title: {
    160. text: '价格结构图',
    161. left: 'center'
    162. },
    163. tooltip: {
    164. trigger: 'item'
    165. },
    166. legend: {
    167. orient: 'vertical',
    168. left: 'left'
    169. },
    170. series: [
    171. {
    172. name: '单价',
    173. type: 'pie',
    174. radius: '50%',
    175. data: [
    176. /* { value: 1048, name: 'Search Engine' },
    177. { value: 735, name: 'Direct' },
    178. { value: 580, name: 'Email' },
    179. { value: 484, name: 'Union Ads' },
    180. { value: 300, name: 'Video Ads' } */
    181. ],
    182. emphasis: {
    183. itemStyle: {
    184. shadowBlur: 10,
    185. shadowOffsetX: 0,
    186. shadowColor: 'rgba(0, 0, 0, 0.5)'
    187. }
    188. }
    189. }
    190. ]
    191. }
    192. )
    193. },
    194. methods:{
    195. // 查询所有
    196. getlist(){
    197. axios.get("http://localhost:8080/list1")
    198. .then(res=>{
    199. console.log(res.data);
    200. this.list=res.data;
    201. // 更新图表
    202. this.myChart.setOption({
    203. series: [
    204. {
    205. /* data: [
    206. { value: 1048, name: 'Search Engine' },
    207. { value: 735, name: 'Direct' },
    208. { value: 580, name: 'Email' },
    209. { value: 484, name: 'Union Ads' },
    210. { value: 300, name: 'Video Ads' }
    211. ] */
    212. data: this.list.map(item => ({ value: item.price, name: item.name}))
    213. }
    214. ]
    215. })
    216. })
    217. .catch(err=>{
    218. console.log(err)
    219. });
    220. },
    221. // 新增
    222. add(){
    223. axios.post("http://localhost:8080/add",this.goods)
    224. .then(res=>{
    225. console.log(this.goods);
    226. alert(res.data);
    227. this.getlist();
    228. this.goods={};
    229. /* for(var i in this.goods){
    230. this.goods[i] = ''
    231. } */
    232. })
    233. },
    234. // 删除
    235. del(id){
    236. if(confirm("确认删除数据?")){
    237. axios.get("http://localhost:8080/del?id="+id)
    238. .then(res=>{
    239. this.getlist();
    240. })
    241. }
    242. }
    243. },
    244. })
    245. script>
    246. body>
    247. html>

  • 相关阅读:
    优雅的实现EasyPoi动态导出列的两种方式
    【软考软件评测师】基于规则说明的测试技术下篇
    回溯-数组总和II
    Mysql主从复制
    JADE(自适应差分进化优化算法)在C++中的完整实现与深度解析
    使用Oracle VM VirtualBox安装Unbuntu虚拟机并安装增强功能(实现双向复制粘贴)
    C#FreeSql分库分表
    #redis 远程链接01#
    Python在字典中获取带权重的随机值
    创建HTML动态数据报告;论文投稿技巧大全(中文版);『高效Pandas指南』随书代码;oneDNN深度神经网络库;前沿论文 | ShowMeAI资讯日报
  • 原文地址:https://blog.csdn.net/adingyb/article/details/134424413