本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于
Panel(面板)、Resizeable(调整大小)、LinkButton(按钮)、Pageination(分页)组件。
一. 后台交互
//删除记录
$.ajax({
type : ‘POST‘,
url : ‘delete.php‘,
data : {
ids : ids.join(‘,‘),
},
beforeSend : function (jqXHR, settings) {
$(‘#box‘).datagrid(‘loading‘);
},
success : function (data, textStatus, jqXHR) {
if (data) {
$(‘#box‘).datagrid(‘load‘);
$(‘#box‘).datagrid(‘loaded‘);
$(‘#box‘).datagrid(‘unselectAll‘);
$.messager.show({
title : ‘提示‘,
msg : data + ‘个用户被删除成功!‘,
});
}
},
});
//删除 PHP 代码
<?php
require ‘config.php‘;
$ids = $_POST[‘ids‘];
$query = mysql_query("DELETE FROM think_user WHERE id IN ($ids)")
or die(‘SQL 错误!‘);
echo mysql_affected_rows();
mysql_close();
?>
//新增及修改用户
onAfterEdit : function (rowIndex, rowData, changes) {
var inserted = $(‘#box‘).datagrid(‘getChanges‘, ‘inserted‘);
var updated = $(‘#box‘).datagrid(‘getChanges‘, ‘updated‘);
var url = info = ‘‘;
//新增内容
if (inserted.length > 0) {
url = ‘add.php‘;
info = ‘个用户被新增成功!‘;
}
//修改内容
if (updated.length > 0) {
url = ‘update.php‘;
info = ‘个用户被修改成功!‘;
}
$.ajax({
type : ‘POST‘,
url : url,
data : {
row : rowData,
},
beforeSend : function (jqXHR, settings) {
$(‘#box‘).datagrid(‘loading‘);
},
success : function (data, textStatus, jqXHR) {
if (data) {
$(‘#box‘).datagrid(‘load‘);
$(‘#box‘).datagrid(‘loaded‘);
$(‘#box‘).datagrid(‘unselectAll‘);
$.messager.show({
title : ‘提示‘,
msg : data + info,
});
obj.editRow = undefined;
}
},
});
},
//新增服务器代码
<?php
require ‘config.php‘;
$row = $_POST[‘row‘][0];
$user = $row[‘user‘];
$email = $row[‘email‘];
$date = $row[‘date‘];
$query = mysql_query("INSERT INTO think_user (user, email, date)
VALUES (‘$user‘, ‘$email‘ ,‘$date‘)") or die(‘SQL 错误!‘);
echo mysql_affected_rows();
mysql_close();
?>
//修改服务器代码
<?php
require ‘config.php‘;
$row = $_POST[‘row‘][0];
$id = $row[‘id‘];
$user = $row[‘user‘];
$email = $row[‘email‘];
$date = $row[‘date‘];
$query = mysql_query("UPDATE think_user SET
user=‘$user‘,email=‘$email‘,date=‘$date‘ WHERE id=‘$id‘") or die(‘SQL 错
误!‘);
echo mysql_affected_rows();
mysql_close();
?>