生活随笔
收集整理的這篇文章主要介紹了
EasyUI-DataGrid之批量删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單介紹一下,涉及到的幾個知識點:
? ? ?1.checkbox,多選
? ? ?2.前臺到后臺傳值,我用的是字符串拼接,到了后臺在拆分的方式
? ? ?3.批量刪除的底層實現
效果圖
? ? ? ? ? ? ? ??
前臺view
[html]?view plaincopyprint?
<table?id="list_data"?class="easyui-datagrid"?style="width:1075px;height:300px"?cellpascing="0"?cellpadding="0"????></table>???
[javascript]?view plaincopyprint?
$(function?()?{?? ????????????$('#dg').datagrid({?? ????????????????title:?'查詢結果',?? ????????????????iconCls:?'icon-view',??? ????????????????width:?100,?? ????????????????height:?'auto',?? ????????????????nowrap:?false,?? ????????????????striped:?true,?? ????????????????border:?true,?? ????????????????collapsible:?true,??? ????????????????fit:?true,??? ????????????????url:?"/EvaluationTemplate/GetData",??? ????????????????remoteSort:?false,????????????? ????????????????pagination:?false,???? ????????????????rownumbers:?false,???? ????????????????singleSelect:?false,??? ????????????????selectOnCheck:?true,?? ????????????????checkOnSelect:?true,??? ????????????????columns:?[[?? ?????????????????????{?field:?'ck',?checkbox:?true,?width:?'30'?},???? ?????????????????????{?field:?'TemplateName',?title:?'模板名稱',?width:?'100'?},???? ????????????????]],?? ?? ????????????});?? ?? ????????});??
[javascript]?view plaincopyprint?
function?deletedata()?{?? ?????????? ????????var?selRow?=?$('#dg').datagrid('getSelections')?? ?????????? ????????if?(selRow.length==0)?{?? ????????????$.messager.alert("提示",?"請選擇要刪除的行!",?"info");?? ????????????return;?? ????????}else{?????? ????????????var?temID="";?? ?????????????? ????????????for?(i?=?0;?i?<?selRow.length;i++)?{?? ????????????????if?(temID?=="")?{?? ????????????????????temID?=?selRow[i].TemplateId;?? ????????????????}?else?{?? ????????????????????temID?=?selRow[i].TemplateId?+?","?+?temID;?? ????????????????}????????????????? ????????????}?? ???????????????????????? ????????????$.messager.confirm('提示',?'是否刪除選中數據?',?function?(r)?{?? ?? ????????????????if?(!r)?{?? ????????????????????return;?? ????????????????}?? ?????????????????? ????????????????$.ajax({?? ????????????????????type:?"POST",?? ????????????????????async:?false,?? ????????????????????url:?"/EvaluationTemplate/DelTem?id="?+?temID,?? ????????????????????data:?temID,?? ????????????????????success:?function?(result)?{?? ????????????????????????if?(result.indexOf("t")?<=?0)?{?? ????????????????????????????$('#dg').datagrid('clearSelections');?? ????????????????????????????$.messager.alert("提示",?"恭喜您,信息刪除成功!",?"info");?? ????????????????????????????$('#dg').datagrid('reload');?? ????????????????????????}?else?{?? ????????????????????????????$.messager.alert("提示",?"刪除失敗,請重新操作!",?"info");?? ????????????????????????????return;?? ????????????????????????}?? ????????????????????}?? ????????????????});?? ????????????});?? ?? ????????}?? ????};??
解說:根據本篇博客性質,所以,大家重點看
?“??selectOnCheck:true,//true勾選會選擇行,false勾選不選擇行, 1.3以后有此選項。重點在這里
??????????????? checkOnSelect: true,//true選擇行勾選,false選擇行不勾選,1.3以后有此選項????????”這兩行代碼。聽著挺繞,其實就是復選框和選中行的顏色一致。一旦選中,復選框和具體某一行都變。
還有循環寫入選中行的模板ID,主要利用選中行,getselections的屬性,獲取個數,循環寫入。
Controller.cs
[csharp]?view plaincopyprint?
?#region?刪除模板?? ?????????? ?????????? ?????????? ?????????? ????????public?bool?DelTem()?? ????????{?? ???????????? ?????????????? ????????????String?strTemId=?Request.QueryString["id"].ToString();?? ????????????bool?bltem?=?template.DelTemplate(strTemId);?? ????????????return?bltem;?? ?? ????????}?? #endregion??
解說:controller在這里只是作為一個數據傳輸的紐帶。
?
服務端
[csharp]?view plaincopyprint?
?? ?string[]?strTemplateId?=?strTemplateIdAll.Split(',');?? ?#region?根據條件刪除?void?DelBy(Expression<Func<T,?bool>>?delWhere)?? ?????????? ?????????? ?????????? ?????????? ????????public?void?DelBy(Expression<Func<T,?bool>>?delWhere)?? ????????{?? ?????????????? ????????????List<T>?listDeleting?=?MyBaseDbContext.Set<T>().Where(delWhere).ToList();?? ?????????????? ????????????listDeleting.ForEach(u?=>?? ????????????{?? ????????????????MyBaseDbContext.Set<T>().Attach(u);?? ????????????????MyBaseDbContext.Set<T>().Remove(u);?? ????????????});?? ????????}?? ????????#endregion??
解說:依舊是底層類庫,底層使用EF,涉及到lambda表達式,批量刪除實現原理都是一樣的,具體實現自己來做吧。
總結
以上是生活随笔為你收集整理的EasyUI-DataGrid之批量删除的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。