輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建CRUD應(yīng)用
數(shù)據(jù)收集并妥善管理數(shù)據(jù)是網(wǎng)絡(luò)應(yīng)用共同的必要。CRUD 允許我們生成頁(yè)面列表,并編輯數(shù)據(jù)庫(kù)記錄。本教程將向你演示如何使用 jQuery EasyUI 框架實(shí)現(xiàn)一個(gè) CRUD DataGrid。
我們將使用下面的插件:
datagrid:向用戶展示列表數(shù)據(jù)。
dialog:創(chuàng)建或編輯一條單一的用戶信息。
form:用于提交表單數(shù)據(jù)。
messager:顯示一些操作信息。
一、EasyUI創(chuàng)建CRUD應(yīng)用
步驟 1:準(zhǔn)備數(shù)據(jù)庫(kù)
我們將使用 MySql 數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)用戶信息。創(chuàng)建數(shù)據(jù)庫(kù)和 'users' 表。

步驟 2:創(chuàng)建 DataGrid 來(lái)顯示用戶信息
創(chuàng)建沒(méi)有 javascript 代碼的 DataGrid。
<table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px" url="get_users.php" toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="firstname" width="50">First Name</th> <th field="lastname" width="50">Last Name</th> <th field="phone" width="50">Phone</th> <th field="email" width="50">Email</th> </tr> </thead> </table> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a> </div>
我們不需要寫任何的 javascript 代碼,就能向用戶顯示列表,如下圖所示:

DataGrid 使用 'url' 屬性,并賦值為 'get_users.php',用來(lái)從服務(wù)器檢索數(shù)據(jù)。
get_users.php 文件的代碼
$rs = mysql_query('select * from users');
$result = array();
while($row = mysql_fetch_object($rs)){
array_push($result, $row);
}
echo json_encode($result);步驟 3:創(chuàng)建表單對(duì)話框
我們使用相同的對(duì)話框來(lái)創(chuàng)建或編輯用戶。
<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">User Information</div>
<form id="fm" method="post">
<div class="fitem">
<label>First Name:</label>
<input name="firstname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Last Name:</label>
<input name="lastname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Phone:</label>
<input name="phone">
</div>
<div class="fitem">
<label>Email:</label>
<input name="email" class="easyui-validatebox" validType="email">
</div>
</form>
</div>
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>
這個(gè)對(duì)話框已經(jīng)創(chuàng)建,也沒(méi)有任何的 javascript 代碼。

步驟 4:實(shí)現(xiàn)創(chuàng)建和編輯用戶
當(dāng)創(chuàng)建用戶時(shí),打開(kāi)一個(gè)對(duì)話框并清空表單數(shù)據(jù)。
function newUser(){
$('#dlg').dialog('open').dialog('setTitle','New User');
$('#fm').form('clear');
url = 'save_user.php';
}
當(dāng)編輯用戶時(shí),打開(kāi)一個(gè)對(duì)話框并從 datagrid 選擇的行中加載表單數(shù)據(jù)。
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('setTitle','Edit User');
$('#fm').form('load',row);
url = 'update_user.php?id='+row.id;
}
'url' 存儲(chǔ)著當(dāng)保存用戶數(shù)據(jù)時(shí)表單回傳的 URL 地址。
步驟 5:保存用戶數(shù)據(jù)
我們使用下面的代碼保存用戶數(shù)據(jù):
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'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
}
});
}
提交表單之前,'onSubmit' 函數(shù)將被調(diào)用,該函數(shù)用來(lái)驗(yàn)證表單字段值。當(dāng)表單字段值提交成功,關(guān)閉對(duì)話框并重新加載 datagrid 數(shù)據(jù)。
步驟 6:刪除一個(gè)用戶
我們使用下面的代碼來(lái)移除一個(gè)用戶:
function destroyUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
if (r){
$.post('destroy_user.php',{id:row.id},function(result){
if (result.success){
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({ // show error message
title: 'Error',
msg: result.errorMsg
});
}
},'json');
}
});
}
}

移除一行之前,我們將顯示一個(gè)確認(rèn)對(duì)話框讓用戶決定是否真的移除該行數(shù)據(jù)。當(dāng)移除數(shù)據(jù)成功之后,調(diào)用 'reload' 方法來(lái)刷新 datagrid 數(shù)據(jù)。
步驟 7:運(yùn)行代碼
開(kāi)啟 MySQL,在瀏覽器運(yùn)行代碼。
二、EasyUI創(chuàng)建展開(kāi)行明細(xì)編輯表單的CRUD 應(yīng)用
當(dāng)切換數(shù)據(jù)網(wǎng)格視圖(datagrid view)到 'detailview',用戶可以展開(kāi)一行來(lái)顯示一些行的明細(xì)在行下面。這個(gè)功能允許您為防止在明細(xì)行面板(panel)中的編輯表單(form)提供一些合適的布局(layout)。在本教程中,我們使用數(shù)據(jù)網(wǎng)格(datagrid)組件來(lái)減小編輯表單(form)所占據(jù)空間。

步驟 1:在 HTML 標(biāo)簽中定義數(shù)據(jù)網(wǎng)格(DataGrid)
<table id="dg" title="My Users" style="width:550px;height:250px" url="get_users.php" toolbar="#toolbar" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="firstname" width="50">First Name</th> <th field="lastname" width="50">Last Name</th> <th field="phone" width="50">Phone</th> <th field="email" width="50">Email</th> </tr> </thead> </table> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newItem()">New</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyItem()">Destroy</a> </div>
步驟 2:為數(shù)據(jù)網(wǎng)格(DataGrid)應(yīng)用明細(xì)視圖
$('#dg').datagrid({
view: detailview,
detailFormatter:function(index,row){
return '<div class="ddv"></div>';
},
onExpandRow: function(index,row){
var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
ddv.panel({
border:false,
cache:true,
href:'show_form.php?index='+index,
onLoad:function(){
$('#dg').datagrid('fixDetailRowHeight',index);
$('#dg').datagrid('selectRow',index);
$('#dg').datagrid('getRowDetail',index).find('form').form('load',row);
}
});
$('#dg').datagrid('fixDetailRowHeight',index);
}
});
為了為數(shù)據(jù)網(wǎng)格(DataGrid)應(yīng)用明細(xì)視圖,在 html 頁(yè)面頭部引入 'datagrid-detailview.js' 文件。
我們使用 'detailFormatter' 函數(shù)來(lái)生成行明細(xì)內(nèi)容。 在這種情況下,我們返回一個(gè)用于放置編輯表單(form)的空的 <div>。 當(dāng)用戶點(diǎn)擊行展開(kāi)按鈕('+')時(shí),'onExpandRow' 事件將被觸發(fā),我們將通過(guò) ajax 加載編輯表單(form)。 調(diào)用 'getRowDetail' 方法來(lái)得到行明細(xì)容器,所以我們能查找到行明細(xì)面板(panel)。 在行明細(xì)中創(chuàng)建面板(panel),加載從 'show_form.php' 返回的編輯表單(form)。
步驟 3:創(chuàng)建編輯表單(Form)
編輯表單(form)是從服務(wù)器加載的。
show_form.php
<form method="post"> <table class="dv-table" style="width:100%;background:#fafafa;padding:5px;margin-top:5px;"> <tr> <td>First Name</td> <td><input name="firstname" class="easyui-validatebox" required="true"></input></td> <td>Last Name</td> <td><input name="lastname" class="easyui-validatebox" required="true"></input></td> </tr> <tr> <td>Phone</td> <td><input name="phone"></input></td> <td>Email</td> <td><input name="email" class="easyui-validatebox" validType="email"></input></td> </tr> </table> <div style="padding:5px 0;text-align:right;padding-right:30px"> <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="saveItem(<?php echo $_REQUEST['index'];?>)">Save</a> <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancelItem(<?php echo $_REQUEST['index'];?>)">Cancel</a> </div> </form>
步驟 4:保存或取消編輯
調(diào)用 'saveItem' 函數(shù)來(lái)保存一個(gè)用戶或者調(diào)用 'cancelItem' 函數(shù)來(lái)取消編輯。
function saveItem(index){
var row = $('#dg').datagrid('getRows')[index];
var url = row.isNewRecord ? 'save_user.php' : 'update_user.php?id='+row.id;
$('#dg').datagrid('getRowDetail',index).find('form').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(data){
data = eval('('+data+')');
data.isNewRecord = false;
$('#dg').datagrid('collapseRow',index);
$('#dg').datagrid('updateRow',{
index: index,
row: data
});
}
});
}
決定要回傳哪一個(gè) URL,然后查找表單(form)對(duì)象,并調(diào)用 'submit' 方法來(lái)提交表單(form)數(shù)據(jù)。當(dāng)保存數(shù)據(jù)成功時(shí),折疊并更新行數(shù)據(jù)。
function cancelItem(index){
var row = $('#dg').datagrid('getRows')[index];
if (row.isNewRecord){
$('#dg').datagrid('deleteRow',index);
} else {
$('#dg').datagrid('collapseRow',index);
}
}
當(dāng)取消編輯動(dòng)作時(shí),如果該行是新行而且還沒(méi)有保存,直接刪除該行,否則折疊該行。
以上就是關(guān)于EasyUI創(chuàng)建CRUD應(yīng)用的七大步驟,分享給大家,希望對(duì)大家的學(xué)習(xí)有所幫助。
- jQuery EasyUI API 中文文檔 - ValidateBox驗(yàn)證框
- jQuery EasyUI API 中文文檔 - DateBox日期框
- 淺談jQuery.easyui的datebox格式化時(shí)間
- jQuery插件EasyUI校驗(yàn)規(guī)則 validatebox驗(yàn)證框
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)拖放商品放置購(gòu)物車
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹(shù)形網(wǎng)絡(luò)(1)
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)樹(shù)形網(wǎng)絡(luò)基本操作(2)
- jQuery easyui的validatebox校驗(yàn)規(guī)則擴(kuò)展及easyui校驗(yàn)框validatebox用法
- easyui validatebox驗(yàn)證
- 淺談如何實(shí)現(xiàn)easyui的datebox格式化
相關(guān)文章
用jQuery簡(jiǎn)化JavaScript開(kāi)發(fā)分析
我總是樂(lè)于嘗試新工具或利用節(jié)省開(kāi)發(fā)時(shí)間的資源。由于市面上存在有許多Web開(kāi)發(fā)工具,你可能很難縮小搜索范圍。2009-02-02
jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
jQuery EasyUI框架中的Datagrid數(shù)據(jù)表格組件結(jié)構(gòu)詳解
jQuery EasyUI是一個(gè)旨在輔助HTML5打造更好的Web界面的框架,而其中的Datagrid組件也是非常強(qiáng)大,這里我們就來(lái)看一下jQuery EasyUI框架中的Datagrid數(shù)據(jù)表格組件結(jié)構(gòu)詳解2016-06-06
基于jQuery實(shí)現(xiàn)的文字按鈕表單特效整理
這里給大家整理了10個(gè)熱門的基于jQuery實(shí)現(xiàn)的文字、按鈕、表單等特效的代碼,集合起來(lái)方便大家對(duì)比使用2014-12-12
使用jQuery將多條數(shù)據(jù)插入模態(tài)框的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用jQuery將多條數(shù)據(jù)插入模態(tài)框的方法,很簡(jiǎn)單,很實(shí)用,需要的朋友可以參考下2014-10-10
jQuery的實(shí)現(xiàn)原理的模擬代碼 -1 核心部分
最近又看了一下 jQuery 1.4.2, 為了便于理解,將 jQuery 的核心使用比較簡(jiǎn)單的代碼模擬一下。方便學(xué)習(xí)。2010-08-08

