欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建CRUD應(yīng)用

 更新時(shí)間:2022年05月06日 14:50:24   投稿:lijiao  
這篇文章主要幫大家輕松學(xué)習(xí)jQuery插件EasyUI,并利用EasyUI創(chuàng)建CRUD應(yīng)用,感興趣的小伙伴們可以參考一下

數(shù)據(jù)收集并妥善管理數(shù)據(jù)是網(wǎng)絡(luò)應(yīng)用共同的必要。CRUD 允許我們生成頁面列表,并編輯數(shù)據(jù)庫記錄。本教程將向你演示如何使用 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ù)庫

我們將使用 MySql 數(shù)據(jù)庫來存儲(chǔ)用戶信息。創(chuàng)建數(shù)據(jù)庫和 'users' 表。

步驟 2:創(chuàng)建 DataGrid 來顯示用戶信息

創(chuàng)建沒有 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',用來從服務(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ì)話框來創(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)建,也沒有任何的 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ù)用來驗(yàn)證表單字段值。當(dāng)表單字段值提交成功,關(guān)閉對(duì)話框并重新加載 datagrid 數(shù)據(jù)。

步驟 6:刪除一個(gè)用戶

我們使用下面的代碼來移除一個(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' 方法來刷新 datagrid 數(shù)據(jù)。

步驟 7:運(yùn)行代碼

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

二、EasyUI創(chuàng)建展開行明細(xì)編輯表單的CRUD 應(yīng)用

當(dāng)切換數(shù)據(jù)網(wǎng)格視圖(datagrid view)到 'detailview',用戶可以展開一行來顯示一些行的明細(xì)在行下面。這個(gè)功能允許您為防止在明細(xì)行面板(panel)中的編輯表單(form)提供一些合適的布局(layout)。在本教程中,我們使用數(shù)據(jù)網(wǎng)格(datagrid)組件來減小編輯表單(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 頁面頭部引入 'datagrid-detailview.js' 文件。
我們使用 'detailFormatter' 函數(shù)來生成行明細(xì)內(nèi)容。 在這種情況下,我們返回一個(gè)用于放置編輯表單(form)的空的 <div>。 當(dāng)用戶點(diǎn)擊行展開按鈕('+')時(shí),'onExpandRow' 事件將被觸發(fā),我們將通過 ajax 加載編輯表單(form)。 調(diào)用 'getRowDetail' 方法來得到行明細(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ù)來保存一個(gè)用戶或者調(diào)用 'cancelItem' 函數(shù)來取消編輯。

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' 方法來提交表單(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í),如果該行是新行而且還沒有保存,直接刪除該行,否則折疊該行。

以上就是關(guān)于EasyUI創(chuàng)建CRUD應(yīng)用的七大步驟,分享給大家,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論