~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:Adobe JavaScript
作者:陈钰桃
撰写时间:2021年4月28日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
成果图
- 写出按钮
2.复选框进行多条选择
3.页面传参
function doDeleteMore() {
var checkStatus = layuiTable.checkStatus('tabCommodity'); //tabCommodity 即为基础参数 id 对应值
if (checkStatus.data.length > 0) { //获取选中行数量
var strIds = '';
//循环获取选中的ID
for (var i = 0; i < checkStatus.data.length; i++) {
//"1;3;5;7;"
strIds += checkStatus.data[i].commodityID + ';';
}
strIds = strIds.substring(0, strIds.length - 1);//去除最后的 ; 号
layer.confirm('您确定要删除选中的' + checkStatus.data.length + '条商品信息?', { icon: 3, title: "提示" }, function (index) {
$.post("@Url.Content("~/SystemBase/Product/DeleteCommoditys")",
{ strCommodityIDs: strIds }, function (returnJson) {
if (returnJson.State == true) {
layer.close(index);
//刷新table
tabCommoditySearch();
}
layer.alert(returnJson.Text, { icon: 6 });
});
});
} else {
layer.alert("请选择要删除的数据", { icon: 0 });
}
}
4.控制器接收参数并进行删除操作返回到页面
public ActionResult DeleteCommoditys(string strCommodityIDs)
{
ReturnJsonVo msg = new ReturnJsonVo();
//声明变量
int intSuceessCount = 0;//记录成功数
int intFailCount = 0; //记录失败数
if (!String.IsNullOrEmpty(strCommodityIDs))
{
//(1)字符串分割
string[] ptudentIDs = strCommodityIDs.Split(';');
foreach(string strId in ptudentIDs)
{
try
{
//(2)获取商品ID
int intCommodityID = Convert.ToInt32(strId);
//(3)查询库存表中是否有数据
int intCount = myModels.R_Repertory.Count(o => o.commodityID == intCommodityID);
if (intCount == 0)
{
//(4)查询要删除的数据
S_Commodity delCommodity = myModels.S_Commodity.Single(o => o.commodityID == intCommodityID);
//判断是否有图片
if (!string.IsNullOrEmpty(delCommodity.commodityPictureName))
{
//获取路径
string filePath = Server.MapPath("~/Document/commodityPicture/") + delCommodity.commodityPictureName;
//判断文件是否存在
if (System.IO.File.Exists(filePath))
{
//3、删除图片文件
System.IO.File.Delete(filePath); } }
//删除
myModels.S_Commodity.Remove(delCommodity);
myModels.SaveChanges();
intSuceessCount++;
}
else
{
msg.Text = "已发生业务的商品不能删除";
} }
catch (Exception e)
{
intFailCount++;
Debug.WriteLine(e);
msg.Text = "删除异常"; } }
msg.State = true;
msg.Text = "总共删除" + ptudentIDs.Length + "条商品信息";
if (intSuceessCount > 0)
{
msg.Text += ",成功" + intSuceessCount + "条";
}
if (intFailCount > 0)
{
msg.Text += ",失败" + intFailCount + "条";
} }
else
{
msg.Text = "请选择要删除的数据";}
return Json(msg, JsonRequestBehavior.AllowGet);
}