实现效果
<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);
}