麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

輕松學習jQuery插件EasyUI EasyUI創建CRUD應用

2019-11-20 11:07:19
字體:
來源:轉載
供稿:網友

數據收集并妥善管理數據是網絡應用共同的必要。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?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?',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?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)的空的 <div>。 當用戶點擊行展開按鈕('+')時,'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(<?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:保存或取消編輯

調用 'saveItem' 函數來保存一個用戶或者調用 'cancelItem' 函數來取消編輯。

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  }); } });}

決定要回傳哪一個 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應用的七大步驟,分享給大家,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品国产成人国产三级 | 欧美特一级片 | 日本教室三级在线看 | 久久国产精品系列 | 欧美伦交 | 日本欧美一区二区三区在线播 | 激情大乳女做爰办公室韩国 | 国产 日韩 一区 | 日韩欧美中文字幕视频 | 欧美日韩国产成人在线 | 久久国产免费 | 污在线观看网站 | 羞羞电影在线观看 | 圆产精品久久久久久久久久久 | 欧美人人干 | 超碰97人 | 久久精品一区二区三 | 国产精品久久久久永久免费 | 欧洲成人一区二区 | 国产精品爱久久久久久久 | 欧美成人se01短视频在线看 | 日美av在线 | 国产精品一二区 | 羞羞视频.www在线观看 | 999精品久久久 | 中文在线国产 | 久久精品亚洲欧美日韩精品中文字幕 | 污污黄 | 青草av.久久免费一区 | 一道本不卡一区 | 亚洲精品一区二区三区大胸 | 日韩视频―中文字幕 | 成熟女人特级毛片www免费 | 亚洲va久久久噜噜噜久久男同 | 黄色网址免费在线播放 | 一区二区视频在线看 | 一本精品999爽爽久久久 | 久久草在线观看视频 | 久久国产精 | 欧美黄一级 | 久久99精品久久久久久青青日本 |