首頁 web前端 js教程 輕鬆學習jQuery外掛EasyUI EasyUI建立CRUD應用程式_jquery

輕鬆學習jQuery外掛EasyUI EasyUI建立CRUD應用程式_jquery

May 16, 2016 pm 03:28 PM
crud easyui jquery

資料收集並妥善管理資料是網路應用共同的必要。 CRUD 允許我們產生頁面列表,並編輯資料庫記錄。本教學將向你示範如何使用 jQuery EasyUI 框架實作一個 CRUD DataGrid。
我們將使用下面的插件:
datagrid:向使用者展示清單資料。
dialog:建立或編輯一條單一的使用者資訊。
form:用於提交表單資料。
messager:顯示一些操作資訊。

一、EasyUI建立CRUD應用程式
步驟 1:準備資料庫

我們將使用 MySql 資料庫來儲存使用者資訊。建立資料庫和 'users' 表。

步驟 2:建立 DataGrid 來顯示使用者資訊

建立沒有 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',用來從伺服器檢索資料。
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:建立表單對話框

我們使用相同的對話框來建立或編輯使用者。

<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>
登入後複製

這個對話框已經創建,也沒有任何的 javascript 程式碼。

步驟 4:實作建立與編輯使用者

當建立使用者時,開啟一個對話方塊並清空表單資料。

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

當編輯使用者時,打開一個對話框並從 datagrid 選擇的行中載入表單資料。

var row = $('#dg').datagrid('getSelected');
if (row){
 $('#dlg').dialog('open').dialog('setTitle','Edit User');
 $('#fm').form('load',row);
 url = 'update_user.php&#63;id='+row.id;
}
登入後複製

'url' 儲存著當儲存使用者資料時表單回傳的 URL 位址。
步驟 5:儲存使用者資料

我們使用下面的程式碼儲存使用者資料:

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' 函數將被調用,該函數用來驗證表單欄位值。當表單欄位值提交成功,關閉對話方塊並重新載入 datagrid 資料。
步驟 6:刪除一個使用者

我們使用下面的程式碼移除一個使用者:

function destroyUser(){
 var row = $('#dg').datagrid('getSelected');
 if (row){
 $.messager.confirm('Confirm','Are you sure you want to destroy this user&#63;',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');
 }
 });
 }
}
登入後複製

移除一行之前,我們將顯示一個確認對話框讓使用者決定是否真的移除該行資料。當移除資料成功之後,呼叫 'reload' 方法來刷新 datagrid 資料。
步驟 7:執行程式碼

開啟 MySQL,在瀏覽器執行程式碼。

二、EasyUI建立展開行明細編輯表單的CRUD 應用

當切換資料網格視圖(datagrid view)到 'detailview',使用者可以展開一行來顯示一些行的明細在行下方。此功能可讓您為防止在明細行面板(panel)中的編輯表單(form)提供一些適當的佈局(layout)。在本教程中,我們使用資料網格(datagrid)元件來減少編輯表單(form)所佔據空間。

步驟 1:在 HTML 標籤中定義資料網格(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:為資料網格(DataGrid)套用明細視圖

$('#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&#63;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);
 }
});
登入後複製

为了为数据网格(DataGrid)应用明细视图,在 html 页面头部引入 'datagrid-detailview.js' 文件。
我们使用 'detailFormatter' 函数来生成行明细内容。 在这种情况下,我们返回一个用于放置编辑表单(form)的空的

。 当用户点击行展开按钮('+')时,'onExpandRow' 事件将被触发,我们将通过 ajax 加载编辑表单(form)。 调用 'getRowDetail' 方法来得到行明细容器,所以我们能查找到行明细面板(panel)。 在行明细中创建面板(panel),加载从 'show_form.php' 返回的编辑表单(form)。
步骤 3:创建编辑表单(Form)

编辑表单(form)是从服务器加载的。
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(<&#63;php echo $_REQUEST['index'];&#63;>)">Save</a>
 <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancelItem(<&#63;php echo $_REQUEST['index'];&#63;>)">Cancel</a>
 </div>
</form>
登入後複製

步骤 4:保存或取消编辑

调用 'saveItem' 函数来保存一个用户或者调用 'cancelItem' 函数来取消编辑。

function saveItem(index){
 var row = $('#dg').datagrid('getRows')[index];
 var url = row.isNewRecord &#63; 'save_user.php' : 'update_user.php&#63;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
  });
 }
 });
}
登入後複製

决定要回传哪一个 URL,然后查找表单(form)对象,并调用 'submit' 方法来提交表单(form)数据。当保存数据成功时,折叠并更新行数据。

function cancelItem(index){
 var row = $('#dg').datagrid('getRows')[index];
 if (row.isNewRecord){
 $('#dg').datagrid('deleteRow',index);
 } else {
 $('#dg').datagrid('collapseRow',index);
 }
}
登入後複製

当取消编辑动作时,如果该行是新行而且还没有保存,直接删除该行,否则折叠该行。

以上就是关于EasyUI创建CRUD应用的七大步骤,分享给大家,希望对大家的学习有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

jQuery引用方法詳解:快速上手指南 jQuery引用方法詳解:快速上手指南 Feb 27, 2024 pm 06:45 PM

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery如何移除元素的height屬性? jQuery如何移除元素的height屬性? Feb 28, 2024 am 08:39 AM

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

jQuery中如何使用PUT請求方式? jQuery中如何使用PUT請求方式? Feb 28, 2024 pm 03:12 PM

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery小技巧:快速修改頁面所有a標籤的文本 jQuery小技巧:快速修改頁面所有a標籤的文本 Feb 28, 2024 pm 09:06 PM

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:&lt

使用jQuery修改所有a標籤的文字內容 使用jQuery修改所有a標籤的文字內容 Feb 28, 2024 pm 05:42 PM

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

如何判斷jQuery元素是否具有特定屬性? 如何判斷jQuery元素是否具有特定屬性? Feb 29, 2024 am 09:03 AM

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

了解jQuery中eq的作用及應用場景 了解jQuery中eq的作用及應用場景 Feb 28, 2024 pm 01:15 PM

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s

PHP常用的檔案操作函數總結 PHP常用的檔案操作函數總結 Apr 03, 2024 pm 02:52 PM

目錄1:basename()2:copy()3:dirname()4:disk_free_space()5:disk_total_space()6:file_exists()7:file_get_contents()8:file_put_contents()9:filesize()10:filetype( )11:glob()12:is_dir()13:is_writable()14:mkdir()15:move_uploaded_file()16:parse_ini_file()17:

See all articles