EasyUI-DataGrid之大量刪除,easyui-datagrid
簡單介紹一下,涉及到的幾個知識點:
1.checkbox,多選
2.前台到後台傳值,我用的是字串拼接,到了後台在拆分的方式
3.大量刪除的底層實現
前台view
<table id="list_data" class="easyui-datagrid" style="width:1075px;height:300px" cellpascing="0" cellpadding="0" ></table>
$(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,//true勾選會選擇行,false勾選不選擇行, 1.3以後有此選項。重點在這裡 checkOnSelect: true, //true選擇行勾選,false選擇行不勾選, 1.3以後有此選項 columns: [[ { field: 'ck', checkbox: true, width: '30' }, //複選框 { field: 'TemplateName', title: '模板名稱', width: '100' }, //課程類型名稱 ]], }); });
function deletedata() { //返回選中多行 var selRow = $('#dg').datagrid('getSelections') //判斷是否選中行 if (selRow.length==0) { $.messager.alert("提示", "請選擇要刪除的行!", "info"); return; }else{ var temID=""; //批量擷取選中行的評估模板ID 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
#region 刪除模板 /// <summary> /// 刪除模板,大量刪除,傳回值是布爾值 /// </summary> /// <returns>傳回值是布爾值</returns> public bool DelTem() { //從前台擷取字串拼接,在這裡先不轉換成數組 String strTemId= Request.QueryString["id"].ToString(); bool bltem = template.DelTemplate(strTemId); return bltem; }#endregion
解說:controller在這裡只是作為一個資料轉送的紐帶。
服務端
//拆分字串,放入數組中 string[] strTemplateId = strTemplateIdAll.Split(','); #region 根據條件刪除 void DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 根據條件刪除 /// </summary> /// <param name="delWhere">刪除條件</param> public void DelBy(Expression<Func<T, bool>> delWhere) { //1查詢要刪除的資料 List<T> listDeleting = MyBaseDbContext.Set<T>().Where(delWhere).ToList(); //2將要刪除的資料 用刪除方法添加到 EF 容器中 listDeleting.ForEach(u => { MyBaseDbContext.Set<T>().Attach(u);//先附加到 EF容器 MyBaseDbContext.Set<T>().Remove(u);//標識為 刪除 狀態 }); } #endregion
解說:依舊是底層類庫,底層使用EF,涉及到lambda運算式,大量刪除實現原理都是一樣的,具體實現自己來做吧。