easyui 增删改查实现js以及html代码
时间: 2024-01-05 11:04:38 浏览: 120
我可以回答这个问题。以下是一个简单的 easyui 增删改查实现的示例代码:
HTML 代码:
```
<div class="easyui-layout" style="width:100%;height:100%;">
<div region="west" split="true" title="菜单" style="width:200px;">
<ul id="menu"></ul>
</div>
<div region="center" title="内容">
<table id="dg"></table>
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新增</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a>
</div>
</div>
</div>
```
JS 代码:
```
$(function(){
$('#menu').tree({
url:'menu.json',
onClick:function(node){
$('#dg').datagrid({
url:'get_users.php?menu='+node.id,
fit:true,
fitColumns:true,
singleSelect:true,
rownumbers:true,
pagination:true,
toolbar:'#toolbar',
columns:[[
{field:'id',title:'编号',width:50},
{field:'name',title:'姓名',width:100},
{field:'sex',title:'性别',width:50},
{field:'age',title:'年龄',width:50},
{field:'phone',title:'电话',width:100},
{field:'email',title:'邮箱',width:150},
{field:'address',title:'地址',width:200},
{field:'remark',title:'备注',width:200}
]]
});
}
});
});
function newUser(){
$('#dlg').dialog('open').dialog('setTitle','新增');
$('#fm').form('clear');
url = 'save_user.php';
}
function editUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('setTitle','编辑');
$('#fm').form('load',row);
url = 'update_user.php?id='+row.id;
}
}
function saveUser(){
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.errorMsg){
$.messager.show({
title: 'Error',
msg: result.errorMsg
});
} else {
$('#dlg').dialog('close');
$('#dg').datagrid('reload');
}
}
});
}
function destroyUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$.messager.confirm('Confirm','Are you sure you want to delete this user?',function(r){
if (r){
$.post('destroy_user.php',{id:row.id},function(result){
if (result.success){
$('#dg').datagrid('reload');
} else {
$.messager.show({
title: 'Error',
msg: result.errorMsg
});
}
},'json');
}
});
}
}
```
阅读全文