当前位置:网站首页>新增批量删除
新增批量删除
2022-07-25 16:23:00 【迟早嘚秃】
新增删除和批量删除
前言
这次的组件前面基本都讲过,所以不在分出来一个个讲,直接完善之前项目的不足
增加和删除功能
一、新增
1、添加新增按钮(userManage.jsp)
<a id="btn-add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">新增</a>
2、给新增按钮添加点击事件
$("#btn-add").click(function() {
//清空之前表单的数据
$('#ff').form('clear');
$("#dd").dialog("open");
addFlag=1;
})
3、var一个变量判断是增加还是修改
因为和修改的窗体是一个,所以需要区分是增加还是修改,变量默认为0,点击修改则为1,点击新增则为0.
var addFlag=0;
4、表单提交的事件
function submitForm() {
/* * 点击确定按钮提交表单到后台,并且是新增/修改共用的一个方法 */
var href=null;
if(addFlag==2){
//修改
href=$("#ctx").val()+'/book.action?methodName=edit'
}else if(addFlag==1){
//增加
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、写dao方法和web
①、BookDao
// 增加
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.页面展示
二.删除
1、单个删除
①、在datagrid添加一列删除
columns:[[ {
field:'ck',checkbox:true}, {
field:'bid',title:'id',width:100}, {
field:'bname',title:'名称',width:100}, {
field:'price',title:'价格',width:100}, {
field:'操作',title:'操作',width:100,align:'right',formatter: function(value,row,index){
return '<a href="javascript:void(0);" onclick="edit();">修改</a>
< a href="javascript:void(0);" onclick="del();">删除</a>' }} ]]
②、添加删除的方法
Messager组件并不能直接向后台发送请求,可以使用ajax发送请求
function del() {
var row=$('#dg').datagrid("getSelected");
if(row){
var id=row.bid;
// messager不能向后台发送请求
$.messager.confirm('确认','您确认想要删除记录吗?',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("请选择要删除的数据");
}
}
③、dao方法
BookDao
// 删除
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;
}
点击删除时(须选中)
2、批量删除
①、添加批量删除按钮(userManage.jsp)
<a id="btn-batchDel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">批量操作</a>
②、增加复选框
$('#dg').datagrid({
url:$("#ctx").val()+'/book.action?methodName=datagrid',
pagination:true,
fitColumns:true,
// 添加复选框
checkbox:true,
toolbar: '#tb',
columns:[[
// 设置复选框的列
{
field:'ck',checkbox:true},
{
field:'bid',title:'id',width:100},
{
field:'bname',title:'名称',width:100},
{
field:'price',title:'价格',width:100},
{
field:'操作',title:'操作',width:100,align:'right',formatter: function(value,row,index){
return '<a href="javascript:void(0);" onclick="edit();">修改</a> <a href="javascript:void(0);" onclick="del();">删除</a>'
}}
]]
});
③、添加一个批量删除的点击事件
$("#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('确认','您确认想要删除记录吗?',function(r){
if (r){
$.ajax({
url:$("#ctx").val()+'/book.action?methodName=del&bids='+ids.join(","),
success:function(data){
if(data==1){
$('#dg').datagrid('reload');
}
}
});
}
});
}
});
~~```
④、修改BookAction代码
```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;
}
⑦、效果
点击确认

OK~~
边栏推荐
- 伦敦银K线图的各种有用形态
- 使用 Terraform 在 AWS 上快速部署 MQTT 集群
- [zeloengine] summary of pit filling of reflection system
- 链游开发现成版 链游系统开发详细原理 链游源码交付
- 一文理解分布式开发中的服务治理
- 测试驱动开发(TDD)在线练功房 | 9月17日开课
- 0x80131500 solution for not opening Microsoft Store
- MySQL pessimistic lock
- Implementation of recommendation system collaborative filtering in spark
- Equivalent change of resistance circuit (Ⅱ)
猜你喜欢

Crazy God redis notes 12

狂神redis笔记12

Breakthrough in core technology of the large humanoid Service Robot Walker x
![Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]](/img/b5/5c7fc70b8025cf7ef21d645a3ac22e.png)
Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]

城市燃气安全再拉警钟,如何防患于未“燃”?

吴恩达逻辑回归2
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

Talk about how to use redis to realize distributed locks?

优必选大型仿人服务机器人Walker X的核心技术突破

0x80131500打不开微软商店的解决办法
随机推荐
聊聊如何用 Redis 实现分布式锁?
02. Limit the parameter props to a list of types
什么是链游系统开发?链游系统开发如何制作
MyBaits
Gap Locks(间隙锁)
一文理解分布式开发中的服务治理
Intention lock
吴恩达逻辑回归2
Visual studio 2022 view class diagram
The annualized interest rate of treasury bonds is too low. Is there a financial product with a higher annualized interest rate than the reverse repurchase of treasury bonds?
只有1000元能买什么理财产品赚钱?
解决Win10磁盘占用100%
MySQL global lock
MySQL self incrementing lock
02. 将参数props限制在一个类型的列表中
MySQL 悲观锁
Product dynamics - Android 13 high-efficiency adaptation new upgrade
Promise期约
MySQL显式锁
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)