datagrid行內新增,行內編輯
阿新 • • 發佈:2019-01-11
實現效果
<div data-options="region:'east',split:true,border:false" title="部門列表" style="width:13%;">
<table id="dept_datagrid" class="easyui-datagrid"
data-options="
url:'${path}/company/deptdg.do',
toolbar:'#dept_tools',
fit:true,
rownumbers:true,
fitColumns: true,
singleSelect:true,
onBeforeEdit: edit2,
onAfterEdit: edit1,
onCancelEdit:edit1,
onClickRow:function(index, row){
rowchange2(row.id);}" >
<thead>
<th data-options="field:'id'" >ID</th>
<th data-options="field:'name',width:100,editor:{type:'text'}">部門名稱</th>
</thead>
</table>
<div id="dept_tools">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="addRow()">新增</a>
<a href="#" id="editdeptbtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="editDept()">編輯</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-del'" onclick="delDept()">刪除</a>
</div>
</div>
//新增行
function addRow() {
$('#dept_datagrid').datagrid('appendRow', {}); //追加一個新行。新行將被新增到最後的位置。
var lastRowIndex=$('#dept_datagrid').datagrid('getRows').length-1;//返回當前頁的最後一行。
$('#dept_datagrid').datagrid('beginEdit', lastRowIndex); //開始編輯行。
$('#dept_datagrid').datagrid('selectRow',lastRowIndex); //選中最新行
$("#editdeptbtn").find(".l-btn-text").text("完成");
}
//編輯
function editDept(){
if(rowid==null){
$.messager.alert("提示","請選擇企業","info");
return;
}
var type = $("#editdeptbtn").find(".l-btn-text").text();
var row = $("#dept_datagrid").datagrid("getSelected");
var index = $('#dept_datagrid').datagrid('getRowIndex', row);
if(type=="完成"){
$('#dept_datagrid').datagrid('endEdit', index); //結束編輯行。
$.ajax({
type:"post",dataType:"json",
url:"/company/addDept.do",
data:{name:row.name,deptId:row.id,companyId:rowid},
success:function(){
$("#editdeptbtn").find(".l-btn-text").text("編輯");
$('#dept_datagrid').datagrid('reload');
$('#dept_datagrid').datagrid('selectRow',index);
}
});
}else{
$('#dept_datagrid').datagrid('beginEdit', index); //開始編輯行。
$("#editdeptbtn").find(".l-btn-text").text("完成");
}
}
//刪除部門
function delDept() {
var select = $("#dept_datagrid").datagrid("getSelected");
if(select == null){
$.messager.alert("提示","請選擇部門","info");
return;
} $.messager.confirm("確認","確定要刪除嗎?\n確定刪除將連同該部門下員工資訊一同刪除",function(yes){
if(yes){
commonDelete("${path}/company/delDept.do",select.id,"dept_datagrid");
}
});
}
//刪除公共呼叫方法
function commonDelete(url,id,datagrid){
$.messager.progress();
$.ajax({
url:url,
type:"post",
data:{id : id},
dataType:"json",
success:function(data){
$.messager.progress("close");
if(data.success){
$.messager.show({
title:"刪除成功",
msg:"刪除成功!",
timeout:600,
style:"left:30%;top:10%",
showType:"fade"
});
$("#"+datagrid).datagrid("reload");
$("#"+datagrid).datagrid('clearSelections');
}else{
$.messager.alert("刪除失敗", data.msg,"error");
}
},
error:function(){
$.messager.progress("close");
$.messager.alert("刪除失敗", "伺服器連線失敗!","error");
}
});
}
function edit1 (index,row)
{
row.editing = false;
$('#dept_datagrid').datagrid('refreshRow', index);
}
function edit2 (index,row)
{
row.editing = true;
$('#dept_datagrid').datagrid('refreshRow', index);
}