中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

jEasyUI 創(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:顯示一些操作信息。

步驟 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í),打開一個(gè)對(duì)話框并清空表單數(shù)據(jù)。

function newUser(){
????$('#dlg').dialog('open').dialog('setTitle','New User');
????$('#fm').form('clear');
????url = 'save_user.php';
}

當(dāng)編輯用戶時(shí),打開一個(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)行代碼

開啟 MySQL,在瀏覽器運(yùn)行代碼。