問題:
var selected = $("#tbList").datagrid("getSelections");
selected的選中項 會包含上次已刪掉的選中項
例子:
比如list中有ArticleID的值1、2、3,
刪除1后,選中2、3繼續刪除,則selected包含了值1,導致出錯。
解決方法:在刪除數據成功后,使用datagrid("clearSelections")清空所有的已選擇項
代碼:
@using XStudio.XWebFramework.Extensions <script type="text/javascript" defer="defer"> var List = {}; List.DeleteSelected = function (e) { var selected = $("#tbList").datagrid("getSelections"); if (selected.length == 0) { alert("請選擇要刪除的文章?"); return; } var idString = ""; $.each(selected, function (index, item) { idString += item.ArticleID + ","; }); if (!confirm("確認要刪除選中文章信息?")) return; $.post($(e).attr("href"), { id: idString }, function (data) { if (data.IsOK) { $("#tbList").datagrid("clearSelections");//解決方法:在刪除數據成功后清空所有的已選擇項 $('#tbList').datagrid('reload'); } else alert(data.Description); }); } $(document).ready(function () { $('#tbList').datagrid({ pagination: true }); }); </script><div class="tt-attach"> <ul class="op"> @Html.WebPartButton("刪除選中", "Delete", null, new { onclick = "List.DeleteSelected(this); return false;", @class = "btn-remove" }) </ul> </div> <div style="padding: 5px 20px 5px 10px;"> <table id="tbList" striped="true" rownumbers="true" fix="true" fitColumns="true" title="" idfield="ArticleID" checkbox="true" url="@Url.Action("ListData")"> <thead> <tr> <th field="ArticleID" checkbox="true" width="30"> </th> </tr> </thead> </table> </div>