EasyUI DataGrid 实用例子(2015-05-22)
代码经过精心改良,可以直接使用,具体看注释吧.
1,前台,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LineInfoManagement.aspx.cs" Inherits="LMX.XiAnPeiHua.Web.Admin.UnitManagement.LineInfoManagement" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>线路信息设置</title>
<script src="/js/jquery-1.7.1.min.js"></script>
<script src="/js/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
<script src="/js/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
<link href="/js/jquery-easyui-1.4.2/themes/metro-blue/easyui.css" rel="stylesheet" />
<link href="/js/jquery-easyui-1.4.2/themes/icon.css" rel="stylesheet" />
<link href="/css/style.css" rel="stylesheet" />
<link href="/css/admin.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function () {
fLoadTable();//加载参数
$('#tt').datagrid('reload', {
Method: 'FirstLoad'
});
$('#unit_id').combobox({
onSelect: function (rec) {
//alert(rec.ID);
//$('#site_id').combobox('reload', location.href + '?type=LoadSite&UnitID=' + rec.ID);
var vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadSite&UnitID=' + rec.ID;
$('#site_id').combobox({
url: vUrl,
onLoadSuccess: function () {
var data = $('#site_id').combobox('getData');
if (data.length > 0) {
$('#site_id').combobox('select', data[0].ID);
}
else {
$('#site_id').combobox('clear');
}
}
})
}
});
})
function fLoadTable() {
$('#tt').datagrid({
title: '',
url: location.href,
pagination: true,
pageSize: 20,
pagePosition: 'top',
striped: true,
//singleSelect: true,
ctrlSelect: true,//在启用多行选择的时候允许使用Ctrl键+鼠标点击的方式进行多选操作。
rownumbers: true,
columns: [[
{ field: 'unit_id', title: 'unit_id', align: 'center', hidden: true },
{ field: 'site_id', title: 'site_id', align: 'center', hidden: true },
{ field: 'line_id', title: 'line_id', align: 'center', hidden: true },
{ field: 'ckey', title: '线路编号', align: 'center' },
{ field: 'name', title: '线路名称', align: 'center' },
{ field: 'remark', title: '备注', align: 'center' }
]],
toolbar: [{
text: '增加',
iconCls: 'icon-add',
handler: function () {
$('#dlg').dialog('open').dialog('setTitle', '新增线路信息');
$('#dlg_form').form('clear');
$('#unit_id').combo('readonly', false); // 禁用只读模式
$('#site_id').combo('readonly', false); // 禁用只读模式
$('#unit_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadUnit');
vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=add';
}
}, {
text: '修改',
iconCls: 'icon-edit',
handler: function () {
var row = $('#tt').datagrid('getSelected');
if (row) {
$('#dlg').dialog('open').dialog('setTitle', '修改线路信息');
$('#unit_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx??type=LoadUnit');
$('#site_id').combobox('reload', '/Admin/UnitManagement/LineInfoManagement.aspx?type=LoadSite');
$('#dlg_form').form('load', row);
$('#unit_id').val(row.unit_id);
$('#site_id').val(row.line_id);
$('#ckeyPre').val(row.site_id);
$('#unit_id').combo('readonly', true); // 启用只读模式
$('#site_id').combo('readonly', true); // 启用只读模式
vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=modify&line_id=' + row.line_id;
} else {
$.messager.alert('请至少选中一项作为修改项.')
}
}
}, '-', {
text: '删除',
iconCls: 'icon-remove',
handler: function () {
var row = $('#tt').datagrid('getSelected');
if (row) {
$.messager.confirm('提示', '您确定要删除这个线路信息吗?', function (r) {
if (r) {
vUrl = '/Admin/UnitManagement/LineInfoManagement.aspx?type=del&site_id=' + row.site_id + '&unit_id=' + row.unit_id + '&line_id=' + row.line_id;
$.ajax({
type: "POST",
url: vUrl,
success: function (json) {
var json = eval('(' + json + ')');
if (json.result == true) {
$.messager.show({
title: 'Success',
msg: '删除成功!'
});
$('#dlg').dialog('close');
// close the dialog
$('#tt').datagrid('reload'); // reload the user data
} else {
$.messager.show({
title: 'Error',
msg: '删除失败,请稍后再试!'
});
$('#dlg').dialog('close');
// close the dialog
$('#tt').datagrid('reload'); // reload the user data
}
}
});
}
});
}
else {
$.messager.alert("请至少选中一项作为删除项");
}
}
}]
});
};
function fSaveData() {
$('#dlg_form').form('submit', {
url: vUrl,
onSubmit: function () {
return $(this).form('validate');
},
success: function (sjson) {
var json = eval('(' + sjson + ')');
if (json.result == true) {
$.messager.show({
title: 'Success',
msg: json.msg ? json.msg : '操作成功!'
});
$('#dlg').dialog('close'); // close the dialog
$('#tt').datagrid('reload'); // reload the user data
} else {
$.messager.show({
title: 'Error',
msg: json.msg ? json.msg : '操作失败,请稍后再试!'
});
$('#dlg').dialog('close'); // close the dialog
}
},
error: function () {
$.messager.show({
title: 'Error',
msg: json.msg ? json.msg : '操作失败,请稍后再试!'
});
$('#dlg').dialog('close'); // close the dialog
}
});
}
</script>
</head>
<body>
<div class="divForTable">
<table id="tt" style="width: 100%; height: 600px; $(this).width() * 0.2;">
</table>
</div>
<div id="dlg" class="easyui-dialog" style="width: 300px; height: 350px; padding: 5px 10px"
data-options="closed:true,buttons:'#dlg-buttons',modal:true">
<form id="dlg_form" method="post">
<div class="fitem">
<label>所属电网:</label>
<input id="unit_id" name="unit_id" class="easyui-combobox" required="true" data-options="valueField:'ID',textField:'Name',editable:false" />
</div>
<div class="fitem">
<label>所属变电所:</label>
<input id="site_id" name="site_id" class="easyui-combobox" required="true" data-options="valueField:'ID',textField:'Name',editable:false" />
</div>
<%-- <div class="fitem">
<label>线路编号:</label>
<input name="ckeyPre" id="ckeyPre" class="easyui-validatebox" readonly="true" style="width: 30px;" />
</div>--%>
<div class="fitem">
<label>名称:</label>
<input name="name" id="name" class="easyui-validatebox" required="true" />
</div>
<div class="fitem">
<label>备注:</label>
<input name="remark" id="remark" class="easyui-validatebox" />
</div>
<div class="line" style="margin-top: 15px;">
说明:线路编号由8位数字组成,前5位是所属变电所编号,最后3位代表线路内部编号.(编号在前端不显示,后台自动添加)
</div>
</form>
</div>
<div id="dlg-buttons">
<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="fSaveData()">保存
</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel"
onclick="javascript:$('#dlg').dialog('close')">取消
</a>
</div>
</body>
</html>
2,后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["from"] == "LoadF_ParentItemCode")
{
using (item_energyEntities ent = new item_energyEntities())
{
List<t_dt_energyitemdict> list = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemType == "A" select c).ToList();
if (list != null && list.Count != 0)
{
var vData = from m in list select new { ID = m.F_EnergyItemCode, Name = m.F_EnergyItemName };
Response.Write(JsonHelper.Serialize(vData));
}
else
{
Response.Write(null);
}
Response.End();
}
}
if (Request.Form["Method"] == "FirstLoad")
{
List<t_dt_energyitemdict> list = DataLoader.GetEnergyItemList();
int page = string.IsNullOrWhiteSpace(Request["page"]) ? 1 : Int32.Parse(Request["page"]);
int rows = string.IsNullOrWhiteSpace(Request["rows"]) ? 1 : Int32.Parse(Request["rows"]);
string order = string.IsNullOrWhiteSpace(Request["order"]) ? "ASC" : Request["order"].ToUpper();
string sort = string.IsNullOrWhiteSpace(Request["sort"]) ? "ID" : Request["sort"];
IQueryable<t_dt_energyitemdict> query = CommonTools.DataSorting<t_dt_energyitemdict>(list.AsQueryable(), sort, order).Skip((page - 1) * rows).Take(rows);
Response.Write(JsonHelper.Serialize(new { total = list.Count, rows = query }));
Response.End();
}
if (Request.QueryString["type"] == "add")
{
string strMsg;
if (AddObject(out strMsg))
{
Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
}
else
{
Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
}
Response.End();
}
if (Request.QueryString["type"] == "modify")
{
string strMsg;
if (ModifyObject(out strMsg))
{
Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
}
else
{
Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
}
Response.End();
}
if (Request.QueryString["type"] == "del")
{
string strMsg;
if (DelObject(out strMsg))
{
Response.Write(JsonHelper.Serialize(new { result = true, msg = strMsg }));
}
else
{
Response.Write(JsonHelper.Serialize(new { result = false, msg = strMsg }));
}
Response.End();
}
}
}
/// <summary>
/// 增加一个对一个对象.
/// </summary>
/// <returns></returns>
private bool AddObject(out string strMsg)
{
string F_EnergyItemCode = Request.Form["F_EnergyItemCode"];
string F_EnergyItemName = Request.Form["F_EnergyItemName"];
string F_ParentItemCode = Request.Form["F_ParentItemCode"];
string F_EnergyItemType = Request.Form["F_EnergyItemType"];
string F_EnergyItemUnit = Request.Form["F_EnergyItemUnit"];
string F_EnergyItemFml = Request.Form["F_EnergyItemFml"];
string F_EnergyItemState = Request.Form["F_EnergyItemState"];
short sF_EnergyItemState = 0;
if (short.TryParse(F_EnergyItemState, out sF_EnergyItemState))
{
sF_EnergyItemState = short.Parse(F_EnergyItemState);
}
bool bPass = false;
try
{
using (item_energyEntities ent = new item_energyEntities())
{
t_dt_energyitemdict aNewModel = new t_dt_energyitemdict()
{
F_EnergyItemCode = F_EnergyItemCode,
F_EnergyItemState = sF_EnergyItemState,
F_EnergyItemType = F_EnergyItemType,
F_EnergyItemUnit = F_EnergyItemUnit,
F_EnergyItemName = F_EnergyItemName,
F_EnergyItemFml = F_EnergyItemFml,
F_ParentItemCode = F_ParentItemCode
};
ent.t_dt_energyitemdict.Add(aNewModel);
if (ent.SaveChanges() > 0)
{
bPass = true;
strMsg = "增加成功。";
}
else
{
bPass = false;
strMsg = "增加失败,请稍后再试。";
}
}
}
catch (Exception ex)
{
strMsg = ex.Message;
bPass = false;
}
return bPass;
}
/// <summary>
/// 更新一个对象.
/// </summary>
/// <returns></returns>
private bool ModifyObject(out string strMsg)
{
string F_EnergyItemCode = Request.Form["F_EnergyItemCode"];
string F_EnergyItemName = Request.Form["F_EnergyItemName"];
string F_ParentItemCode = Request.Form["F_ParentItemCode"];
string F_EnergyItemType = Request.Form["F_EnergyItemType"];
string F_EnergyItemUnit = Request.Form["F_EnergyItemUnit"];
string F_EnergyItemFml = Request.Form["F_EnergyItemFml"];
string F_EnergyItemState = Request.Form["F_EnergyItemState"];
short sF_EnergyItemState = 0;
if (short.TryParse(F_EnergyItemState, out sF_EnergyItemState))
{
sF_EnergyItemState = short.Parse(F_EnergyItemState);
}
bool bPass = false;
try
{
using (item_energyEntities ent = new item_energyEntities())
{
t_dt_energyitemdict aModel = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemCode == F_EnergyItemCode select c).FirstOrDefault();
if (aModel != null)
{
aModel.F_EnergyItemCode = F_EnergyItemCode;
aModel.F_EnergyItemName = F_EnergyItemName;
aModel.F_ParentItemCode = F_ParentItemCode;
aModel.F_EnergyItemType = F_EnergyItemType;
aModel.F_EnergyItemUnit = F_EnergyItemUnit;
aModel.F_EnergyItemFml = F_EnergyItemFml;
aModel.F_EnergyItemState = sF_EnergyItemState;
if (ent.SaveChanges() > 0)
{
bPass = true;
strMsg = "修改成功。";
}
else
{
bPass = false;
strMsg = "修改失败,请稍后再试。";
}
}
else
{
bPass = false;
strMsg = "不存在此记录,请稍后再试。";
}
}
}
catch (Exception ex)
{
strMsg = ex.Message;
bPass = false;
//throw;
}
return bPass;
}
/// <summary>
/// 删除一个对象.
/// </summary>
/// <returns></returns>
private bool DelObject(out string strMsg)
{
string F_EnergyItemCode = Request.QueryString ["F_EnergyItemCode"];
bool bPass = false;
using (item_energyEntities ent = new item_energyEntities())
{
t_dt_energyitemdict aModel = (from c in ent.t_dt_energyitemdict where c.F_EnergyItemCode == F_EnergyItemCode select c).FirstOrDefault();
try
{
if(aModel!=null)
{
ent.t_dt_energyitemdict.Remove(aModel);
if(ent.SaveChanges()>0)
{
bPass = true;
strMsg = "删除成功。";
}
else
{
bPass = false;
strMsg = "删除失败,请稍后再试。";
}
}
else
{
bPass = false;
strMsg = "不存在此记录,请稍后再试。";
}
}
catch (Exception ex)
{
bPass = false;
strMsg = ex.Message;
}
}
return bPass;
}