layui+ssm實(shí)現(xiàn)數(shù)據(jù)表格雙擊編輯更新數(shù)據(jù)功能
layui實(shí)現(xiàn)數(shù)據(jù)表格雙擊編輯數(shù)據(jù)更新
在使用layui加載后端數(shù)據(jù)請(qǐng)求時(shí),對(duì)數(shù)據(jù)選項(xiàng)框進(jìn)行雙擊即可實(shí)現(xiàn)數(shù)據(jù)的輸入編輯更改
代碼塊
var form = layui.form, table = layui.table, layer = parent.layer === undefined ? layui.layer : parent.layer, laypage = layui.laypage; var util = layui.util; $ = layui.jquery; //數(shù)據(jù)表格 table.render({ id: 'categoryList', elem: '#categoryList', url: ctx + "/book/getCategoryList", //數(shù)據(jù)接口 cellMinWidth: 80, toolbar: '#toolbar', editTrigger: 'dblclick',// 觸發(fā)編輯的事件類型(默認(rèn) click ) limit: 10,//每頁(yè)條數(shù) limits: [10, 20, 30, 40], layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], css: [ // 對(duì)開啟了編輯的單元格追加樣式 '.layui-table-view td[data-edit]{color: #16B777;}' ].join(''), cols: [[ //表頭 {type: 'numbers', title: '序號(hào)', width: 80},//序號(hào)列 {field: 'categoryName', title: '類別名稱', align: 'center', edit: 'textarea'},// edit: 'textarea'主要標(biāo)記當(dāng)前項(xiàng)是否啟用編輯 {field: 'categoryDesc', title: '類別說明', align: 'center', edit: 'textarea'}, { field: 'categoryDate', title: '創(chuàng)建時(shí)間', sort: true, align: 'center', templet: '<div>{{ formatTime(d.categoryDate,"yyyy-MM-dd hh:mm:ss")}}</div>' }, { fixed: 'right', title: '操作', align: 'center', templet: function (d) { return '<button class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit">編輯</i></button>' + '<button class="layui-btn layui-bg-red layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete">刪除</i></button>'; } }, ]], page: true }); /* * 單元格雙擊編輯事件 * */ table.on('edit(categoryList)', function (obj) { var field = obj.field; // 得到修改的字段 var value = obj.value // 得到修改后的值 var cateId = obj.data.cateId; // 獲取當(dāng)前修改數(shù)據(jù)的id // 值的校驗(yàn) if (value.replace(/\s/g, '') === '') { layer.msg('修改值不能為空!'); return obj.reedit(); // 重新編輯 } // 編輯后續(xù)操作,如提交更新請(qǐng)求,以完成真實(shí)的數(shù)據(jù)更新 var index = top.layer.msg('正在將新數(shù)據(jù)寫入數(shù)據(jù)庫(kù),請(qǐng)稍候...', {icon: 16, time: false, shade: 0.8}); var msg; setTimeout(function () { $.ajax({ type: "POST", url: ctx + "/book/updateCate", data: { cateId: cateId, // 獲取當(dāng)前修改數(shù)據(jù)的id field: field,// 得到修改的字段 value: value,// 得到修改后的值 }, success: function (d) { if (d.code == 0) { msg = d.msg; } else { msg = d.msg; } }, error: function (jqXHR, textStatus, errorThrown) { layer.msg("獲取數(shù)據(jù)失??! 先檢查sql 及 Tomcat Localhost Log 的輸出"); } }).done(function () { top.layer.close(index); top.layer.msg(msg); layer.closeAll("iframe"); setTimeout(function () { parent.location.reload(); }, 1000); }); }, 2000); }); });
這里只要使用layui和后端ssm框架實(shí)現(xiàn),所以后端代碼
controller代碼
/* * 數(shù)據(jù)更新操作 * */ // 更新分類信息的接口 @RequestMapping("/updateCate") @ResponseBody //根據(jù)前端提供的id,更改的字段,更改的值進(jìn)行查詢更新 public ResultUtil updateCate(Integer cateId, String field, String value) throws ParseException { // 打印傳入的分類ID、字段和值 /* System.out.println(cateId); System.out.println(value); System.out.println(field);*/ // 根據(jù)分類ID獲取分類實(shí)體 CategoryEntity categoryEntity = bookService.getCategoryById(cateId); // 打印獲取到的分類實(shí)體 System.out.println(categoryEntity); // 插入當(dāng)前時(shí)間作為修改時(shí)間 Date data = new Date(); // 創(chuàng)建一個(gè)SimpleDateFormat對(duì)象,用于格式化日期 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 使用SimpleDateFormat對(duì)象將Date對(duì)象轉(zhuǎn)換為字符串格式的日期 String nowTime = simpleDateFormat.format(data); // 使用SimpleDateFormat對(duì)象將字符串格式的日期解析為Date對(duì)象 Date date1 = simpleDateFormat.parse(nowTime); // 如果獲取到的分類實(shí)體不為空 if (categoryEntity != null) { // 根據(jù)字段名進(jìn)行相應(yīng)的操作 switch (field) { case "categoryDesc": categoryEntity.setCategoryDesc(value); break; case "categoryName": categoryEntity.setCategoryName(value); break; default: // 如果字段名不符合要求,返回錯(cuò)誤信息 return ResultUtil.error("提交的數(shù)據(jù)有問題,請(qǐng)檢查!"); } // 設(shè)置修改時(shí)間 categoryEntity.setCategoryDate(date1); // 更新分類實(shí)體 bookService.updateCategory(categoryEntity); // 返回成功信息 return ResultUtil.ok("數(shù)據(jù)信息更新成功!"); } // 如果獲取到的分類實(shí)體為空,返回錯(cuò)誤信息 return ResultUtil.error("提交的數(shù)據(jù)有問題,請(qǐng)檢查!"); }
service
/* * 數(shù)據(jù)更新 * */ void updateCategory(CategoryEntity categoryEntity); /* * 根據(jù)id機(jī)型查詢數(shù)據(jù) * * */ CategoryEntity getCategoryById(Integer cateId);
serviceImpl
/* * 數(shù)據(jù)更新 * */ @Override public void updateCategory(CategoryEntity categoryEntity) { categoryDao.updateCategory(categoryEntity); } /* * 根據(jù)id查詢數(shù)據(jù) * */ @Override public CategoryEntity getCategoryById(Integer cateId) { return categoryDao.getCategoryById(cateId); }
dao
/* * 數(shù)據(jù)更新updateCategory * */ void updateCategory(CategoryEntity categoryEntity); /* * 根據(jù)id查詢數(shù)據(jù) * */ CategoryEntity getCategoryById(Integer cateId);
mapper.xml
<!--根據(jù)id查詢數(shù)據(jù)--> <select id="getCategoryById" resultType="layui.library.manager.project.entity.CategoryEntity"> SELECT * FROM tb_book_category where cateId = #{cateId} </select> <!--數(shù)據(jù)更新--> <update id="updateCategory" parameterType="layui.library.manager.project.entity.CategoryEntity"> update tb_book_category <set> <if test="categoryName!=null"> categoryName=#{categoryName}, </if> <if test="categoryDesc!=null"> categoryDesc=#{categoryDesc}, </if> <if test="categoryDate!=null"> categoryDate=#{categoryDate} </if> </set> where cateId=#{cateId} </update>
到此這篇關(guān)于layui+ssm實(shí)現(xiàn)數(shù)據(jù)表格雙擊編輯更新數(shù)據(jù)的文章就介紹到這了,更多相關(guān)layui數(shù)據(jù)表格雙擊編輯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Layui數(shù)據(jù)表格判斷編輯輸入的值,是否為我需要的類型詳解
- Layui數(shù)據(jù)表格之單元格編輯方式
- Layui彈框中數(shù)據(jù)表格中可雙擊選擇一條數(shù)據(jù)的實(shí)現(xiàn)
- layui table表格數(shù)據(jù)的新增,修改,刪除,查詢,雙擊獲取行數(shù)據(jù)方式
- 解決Layui數(shù)據(jù)表格顯示無(wú)數(shù)據(jù)提示的問題
- Layui實(shí)現(xiàn)數(shù)據(jù)表格默認(rèn)全部顯示(不要分頁(yè))
- 在Layui中操作數(shù)據(jù)表格,給指定單元格添加事件示例
相關(guān)文章
兩款JS腳本判斷手機(jī)瀏覽器類型跳轉(zhuǎn)WAP手機(jī)網(wǎng)站
本文通過兩款js腳本判斷手機(jī)瀏覽器類型跳轉(zhuǎn)到wap手機(jī)網(wǎng)站,感興趣的小伙伴快來(lái)學(xué)習(xí)吧2015-10-10javascript 就地編輯實(shí)現(xiàn)代碼
最近正在看《javascript設(shè)計(jì)模式》,其中有一個(gè)'就地編輯'的示例,用來(lái)表現(xiàn)不同的繼承方式,看完之后想自己憑理解寫一個(gè)類似的東西。2010-05-05微信小程序?qū)崿F(xiàn)團(tuán)購(gòu)或秒殺批量倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)團(tuán)購(gòu)或秒殺批量倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07uniapp項(xiàng)目實(shí)踐自定義滑動(dòng)觸摸組件實(shí)現(xiàn)示例
這篇文章主要介紹了uniapp項(xiàng)目實(shí)踐自定義滑動(dòng)觸摸組件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09javascript發(fā)表評(píng)論或者留言時(shí)的展開效果
javascript發(fā)表評(píng)論或者留言時(shí)的展開效果...2007-07-07D3.js實(shí)現(xiàn)雷達(dá)圖的方法詳解
大家應(yīng)該都知道基本圖表一共有六種,分別是柱狀圖、折線圖、散點(diǎn)圖、氣泡圖、餅圖和雷達(dá)圖。前面五種圖形都已經(jīng)介紹過如何實(shí)現(xiàn)了,今天我們一起來(lái)看看最后的雷達(dá)圖。有需要的朋友們可以參考學(xué)習(xí)下。2016-09-09