layui+ssm實現(xiàn)數(shù)據(jù)批量刪除功能
更新時間:2023年12月05日 11:48:47 作者:洛洛不覺
本篇文章給大家介紹layui+ssm實現(xiàn)數(shù)據(jù)批量刪除功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
layui+ssm實現(xiàn)數(shù)據(jù)的批量刪除

//數(shù)據(jù)表格
table.render({
id: 'adminList',
elem: '#adminList',
url: ctx + "/admin/getAdminList", //數(shù)據(jù)接口
cellMinWidth: 80,
even: true,
toolbar: '#toolbarDemo',//頭部工具欄
limit: 10,//每頁條數(shù)
limits: [10, 20, 30, 40],
defaultToolbar: ['filter', 'exports', 'print'],
cols: [[ //表頭
{type: 'checkbox', fixed: 'left'},
{type: 'numbers', title: '序號', width: 80},//序號列
// {field: 'id', title: '編號', align: 'center', width: 80},
{field: 'username', title: '姓名', align: 'center'},
{field: 'phone', title: '電話', align: 'center'},
{
field: 'gender', title: '性別', align: "center", templet: function (d) {
if (d.gender == '2') {
return '<button class="layui-btn layui-bg-orange layui-btn-xs ">女</button>';
} else if (d.gender == '1') {
return '<button class="layui-btn layui-bg-cyan layui-btn-xs layui-btn-normal">男</button>';
} else if (d.gender == '') {
return '<button class="layui-btn layui-bg-red layui-btn-xs layui-btn-normal">未知</button>';
} else {
return '';
}
}
},
{field: 'roleName', title: '賬戶類型', align: 'center'},
],
page: true,
loading: true
});
/*
* 監(jiān)聽頭部工具欄
* */
table.on('toolbar(adminList)', function (obj) {
var phone = $("#phone").val(); //獲取前端頁面?zhèn)鬟^來的當(dāng)前登錄人的手機號
var id = obj.config.id;//獲取當(dāng)前操作的id
var checkStatus = table.checkStatus(id);
var checkData = checkStatus.data; // 獲取選中的數(shù)據(jù)
switch (obj.event) {
case 'deleteBatch':
if (checkData.length === 0) {
layer.msg('請選擇一行數(shù)據(jù)再進(jìn)行操作!');
} else if (checkData.some(item => item.phone === phone)) {
layer.msg("不允許刪除當(dāng)前賬戶!", {icon: 5});
} else if (checkData.some(item => item.roleName === "超級管理員")) {
layer.msg("此賬戶你沒有權(quán)限操作!");
} else {
layer.confirm('確定刪除所選賬戶嗎?', function (index) {
$.ajax({
url: ctx + "/admin/deleteBatch",
type: "POST",
data: JSON.stringify({ids: checkData}),
contentType: "application/json",
success: function (d) {
if (d.code === 0) {
layer.msg(d.msg, {icon: 1});
table.reload('adminList', {});
} else {
layer.msg("失??!", {icon: 5});
}
},
error: function (jqXHR, textStatus, errorThrown) {
layer.msg("獲取數(shù)據(jù)失??! 先檢查sql 及 Tomcat Localhost Log 的輸出");
}
});
layer.close(index);
});
}
break;
}
});controller
/*
* 批量刪除
* */
// 定義一個名為deleteBatch的方法,用于處理批量刪除請求
@RequestMapping("/deleteBatch")
@ResponseBody
public ResultUtil deleteBatch(@RequestBody Map<String, Object> params, HttpSession session) {
try {
// 從請求參數(shù)中獲取要刪除的賬戶ID列表
List<Integer> ids = (List<Integer>) params.get("ids");
System.out.println(ids);
// 調(diào)用adminService的deleteByIds方法,根據(jù)ID列表批量刪除賬戶
adminService.deleteByIds(ids);
// 返回成功結(jié)果
return ResultUtil.ok("批量刪除賬戶成功");
} catch (Exception e) {
// 如果發(fā)生異常,打印異常堆棧信息
e.printStackTrace();
// 返回錯誤結(jié)果,狀態(tài)碼為500,提示信息為"sql問題"
return new ResultUtil(500, "sql問題");
}
}service
void deleteByIds(List<Integer> ids);
serviceimpl:
@Override
public void deleteByIds(List<Integer> ids) {
adminDao.deleteByIDS(ids);
}
dao:
void deleteByIDS( List<Integer> ids);mapper.xml
<delete id="deleteByIDS" parameterType="java.util.List">
DELETE FROM tb_admin
WHERE id IN
<foreach collection="list" open="(" close=")" separator="," item="param">
#{param.id}
</foreach>
</delete>到此這篇關(guān)于layui+ssm實現(xiàn)數(shù)據(jù)批量刪除的文章就介紹到這了,更多相關(guān)layui ssm批量刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS將網(wǎng)址url轉(zhuǎn)化為JSON格式的方法
這篇文章主要介紹了JS將網(wǎng)址url轉(zhuǎn)化為JSON格式的方法,需要的朋友可以參考下2018-07-07
javascript實現(xiàn)智能手環(huán)時間顯示
這篇文章主要為大家詳細(xì)介紹了javascript實現(xiàn)智能手環(huán)時間顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09

