当前位置:网站首页>Add batch delete
Add batch delete
2022-07-25 17:00:00 【Sooner or later, I will go bald】
Add delete and batch delete
Preface
The components of this time have been basically mentioned above , So don't tell them one by one , Directly improve the deficiencies of previous projects
Add and delete functions
One 、 newly added
1、 Add new button (userManage.jsp)
<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'"> newly added </a>
2、 Add a click event to the new button
$("#btn-add").click(function() {
// Clear the data of the previous form
$('#ff').form('clear');
$("#dd").dialog("open");
addFlag=1;
})
3、var A variable determines whether to add or modify
Because the form with modification is a , Therefore, it is necessary to distinguish whether to add or modify , The variable defaults to 0, Click Modify and it will be 1, Click add, then 0.
var addFlag=0;
4、 Event of form submission
function submitForm() {
/* * Click OK to submit the form to the background , And it's new / Modify a common method */
var href=null;
if(addFlag==2){
// modify
href=$("#ctx").val()+'/book.action?methodName=edit'
}else if(addFlag==1){
// increase
href=$("#ctx").val()+'/book.action?methodName=add'
}
$('#ff').form('submit', {
url:href,
success: function(data){
if(data==1){
$("#dd").dialog("close");
$('#dg').datagrid('reload');
}
}
});
}
5、 Write dao Methods and web
①、BookDao
// increase
public void add(Book book) throws Exception {
book.setBid((int)new Date().getTime());
super.executeUpdate("insert into t_mvc_book values(?,?,?)", book, new String[] {
"bid","bname","price"});
}
②、BookAction
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
6. The page display 
Two . Delete
1、 Single delete
①、 stay datagrid Add a column and delete
columns:[[ {
field:'ck',checkbox:true}, {
field:'bid',title:'id',width:100}, {
field:'bname',title:' name ',width:100}, {
field:'price',title:' Price ',width:100}, {
field:' operation ',title:' operation ',width:100,align:'right',formatter: function(value,row,index){
return '<a href="javascript:void(0);" onclick="edit();"> modify </a>
< a href="javascript:void(0);" onclick="del();"> Delete </a>' }} ]]
②、 Methods of adding and deleting
Messager Components cannot send requests directly to the background , have access to ajax Send a request
function del() {
var row=$('#dg').datagrid("getSelected");
if(row){
var id=row.bid;
// messager Cannot send request to background
$.messager.confirm(' confirm ',' Are you sure you want to delete the record ?',function(r){
if (r){
$.ajax({
url:$("#ctx").val()+'/book.action?methodName=del&bid='+id,
success:function(data){
if(data==1){
$('#dg').datagrid('reload');
}
}
});
}
});
}else{
alert(" Please select the data to delete ");
}
}
③、dao Method
BookDao
// Delete
public void del(Book book) throws Exception {
super.executeUpdate(“delete from t_mvc_book where bid=?”, book, new String[] {“bid”});
}
④、BookAction
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.del(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
Click delete ( Must be selected )
2、 Batch deletion
①、 Add batch delete button (userManage.jsp)
<a id="btn-batchDel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'"> The batch operation </a>
②、 Add check box
$('#dg').datagrid({
url:$("#ctx").val()+'/book.action?methodName=datagrid',
pagination:true,
fitColumns:true,
// Add checkboxes
checkbox:true,
toolbar: '#tb',
columns:[[
// Set the column of the check box
{
field:'ck',checkbox:true},
{
field:'bid',title:'id',width:100},
{
field:'bname',title:' name ',width:100},
{
field:'price',title:' Price ',width:100},
{
field:' operation ',title:' operation ',width:100,align:'right',formatter: function(value,row,index){
return '<a href="javascript:void(0);" onclick="edit();"> modify </a> <a href="javascript:void(0);" onclick="del();"> Delete </a>'
}}
]]
});
③、 Add a batch delete click event
$("#btn-batchDel").click(function() {
var rows=$('#dg').datagrid("getSelections");
var ids=new Array();
if(rows !=null&& rows.length >0){
for (var i in rows) {
ids.push(rows[i].bid)
}
}
if(ids.length>0){
$.messager.confirm(' confirm ',' Are you sure you want to delete the record ?',function(r){
if (r){
$.ajax({
url:$("#ctx").val()+'/book.action?methodName=del&bids='+ids.join(","),
success:function(data){
if(data==1){
$('#dg').datagrid('reload');
}
}
});
}
});
}
});
~~```
④、 modify BookAction Code
```csharp
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
String ids = req.getParameter("bids");
String[] split = ids.split(",");
for (String s : split) {
book.setBid(Integer.parseInt(s));
bookDao.del(book);
}
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
⑦、 effect
Click on the confirmation 

OK~~
边栏推荐
猜你喜欢

152. 乘积最大子数组

虚拟内存管理

一百个用户眼中,就有一百个QQ

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类

Rainbond插件扩展:基于Mysql-Exporter监控Mysql

激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就

Chapter VI succession
![[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart](/img/73/2b6fe0cf69fa013894abce331e1386.png)
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart

【数学建模绘图系列教程】二、折线图的绘制与优化

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
随机推荐
自定义mvc项目登录注册和树形菜单
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
Data analysis and privacy security become the key factors for the success or failure of Web3.0. How do enterprises layout?
Replicate swin on Huawei ascend910_ transformer
Don't believe these "rumors" in the process of preparing for the exam!
IaaS基础架构云 —— 云网络
Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
多租户软件开发架构
Baidu rich text editor ueeditor single image upload cross domain
[MySQL] takes you to the database
基于redis6.2.4的redis cluster部署
Birui data joins Alibaba cloud polardb open source database community
What is chain game system development? How to make chain game system development
【读书会第13期】+FFmpeg视频采集功能
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification
失意的互联网人拼命叩开Web3大门
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
Box selection screenshot shortcut key of win10
Mindoc makes mind map
备考过程中,这些“谣言”千万不要信!