背景介紹:php
原先項目採用普通的jsp頁面來作爲前端顯示,用戶體驗差,而且爲了實現某一種效果須要編寫大量的js代碼。所以尋找能夠逐步替代前端顯示的框架,逐漸轉變爲富客戶端開發。經過上網查閱資料,並結合業務須要,發現extjs過於龐大,而easyui小巧而且功能也很強大。因而採用EasyUI的方式嘗試在一個功能上使用。功能以下:css
用戶點擊提交時,彈出模態窗口,該模態窗口內容支持異步獲取表格內容,同時支持某個表格的單元格進行編輯。而且支持複選,最後將選擇的內容提交到後臺。html
1、引入EasyUI框架前端
Easyui的引入很是簡單。只須要在頁面中加入以下js即可以工做。java
- <span style="font-family:SimHei;font-size:18px;"><script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.8.3.js"></script>jquery包
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/jquery.easyui.min.js"></script> easyui開發包
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>解決中文亂碼包,不一樣的語言只要加入local下對應的js
- <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/default/easyui.css"> 全局easyui css樣式包
- <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/icon.css">全局easyui 圖標樣式包</span>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.8.3.js"></script>jquery包
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/jquery.easyui.min.js"></script> easyui開發包
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>解決中文亂碼包,不一樣的語言只要加入local下對應的js
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/default/easyui.css"> 全局easyui css樣式包
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/icon.css">全局easyui 圖標樣式包
2、使用dialog實現模態彈出框jquery
代碼以下:json
- <div id="dlg" <span style="color:#ff0000;">class="easyui-dialog"</span> title="詳細信息" style="width:730px;height:340px;padding:10px"
- data-options="
- buttons: [{
- text:'提交',
- iconCls:'icon-ok',
- handler:function(){
- alert('ok');
- }
- },{
- text:'取消',
- handler:function(){
- $('#dlg').dialog('close');
- }
- }]
- ">
- </div>
<div id="dlg" class="easyui-dialog" title="詳細信息" style="width:730px;height:340px;padding:10px"
data-options="
buttons: [{
text:'提交',
iconCls:'icon-ok',
handler:function(){
alert('ok');
}
},{
text:'取消',
handler:function(){
$('#dlg').dialog('close');
}
}]
">
</div>
請你們注意上述代碼紅色部分,必定要寫成easyui-dialog,easyui會根據該標識初始化一個dialog對象 。 api
效果以下:微信
3、採用Datagrid實現表格數據綁定
代碼以下:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.8.3.js"></script>
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
- <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/default/easyui.css">
- <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/icon.css">
- </head>
- <body>
-
- <div id="dlg" <span style="color:#ff0000;">class="easyui-dialog"</span> title="詳細信息" style="width:740px;height:350px;padding:10px"
- data-options="
- buttons: [{
- text:'提交',
- iconCls:'icon-ok',
- handler:function(){
- alert('ok');
- }
- },{
- text:'取消',
- handler:function(){
- $('#dlg').dialog('close');
- }
- }]
- ">
- <table id="dg" class="easyui-datagrid" title="數據" style="width:700px;height:250px"
- data-options="
- rownumbers:true,
- singleSelect:false,
- url:'<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/demo/datagrid/datagrid_data1.json'
- ">
- <thead>
- <tr>
- <th data-options="field:'ck',checkbox:true"></th>
- <th data-options="field:'itemid',width:80">Item ID</th>
- <th data-options="field:'productid',width:100">Product</th>
- <th data-options="field:'listprice',width:80,align:'right'">List Price</th>
- <th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
- <th data-options="field:'attr1',width:220">Attribute</th>
- <th data-options="field:'status',width:60,align:'center'">Status</th>
- </tr>
- </thead>
- </table>
- </div>
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/themes/icon.css">
</head>
<body>
<div id="dlg" class="easyui-dialog" title="詳細信息" style="width:740px;height:350px;padding:10px"
data-options="
buttons: [{
text:'提交',
iconCls:'icon-ok',
handler:function(){
alert('ok');
}
},{
text:'取消',
handler:function(){
$('#dlg').dialog('close');
}
}]
">
<table id="dg" class="easyui-datagrid" title="數據" style="width:700px;height:250px"
data-options="
rownumbers:true,
singleSelect:false,
url:'<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/demo/datagrid/datagrid_data1.json'
">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true"></th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'attr1',width:220">Attribute</th>
<th data-options="field:'status',width:60,align:'center'">Status</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
請你們注意上面代碼紅色部分,告訴easyui初始化一個datagrid對象。
效果以下:
4、實現編輯Product列
使用easyui內置formatter函數,在表示datagrid表格對應product列頭增長以下代碼:
- <span style="font-size:18px;"><th data-options="field:'productid',width:100, formatter:formatProduct">Product</th></span>
<th data-options="field:'productid',width:100, formatter:formatProduct">Product</th>
編寫formatProduct回調函數,代碼以下:
- <span style="font-size:18px;">function formatProduct(val,row,index){
- return "<input type='text' id='ch'"+row.itemid+" value='0' ></input>"</span>
function formatProduct(val,row,index){
return "<input type='text' id='ch'"+row.itemid+" value='0' ></input>"
- <span style="font-size:18px;">}</span>
}
效果以下:
5、爲DataGrid添加footer
該footer至關於表格底部信息,通常作統計信息,須要給datagrid的data-options增長一個顯示footer的屬性,這裏須要注意:因爲datagrid綁定的是json數據,所以json格式須要包含footer信息。代碼以下,注意紅色部分和json數據(後面會補充上):
- <span style="font-size:18px;"><table id="dg" class="easyui-datagrid" title="數據" style="width:700px;height:250px"
- data-options="
- rownumbers:true,
- singleSelect:false,
- <span style="color:#ff0000;">showFooter: true,</span>
- url:'<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/demo/datagrid/datagrid_data2.json'
- ">
- <thead>
- <tr>
- <th data-options="field:'ck',checkbox:true"></th>
- <th data-options="field:'itemid',width:80">Item ID</th>
- <th data-options="field:'productid',width:150, formatter:formatProduct">Product</th>
- <th data-options="field:'listprice',width:80,align:'right'">List Price</th>
- <th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
- <th data-options="field:'attr1',width:170">Attribute</th>
- <th data-options="field:'status',width:60,align:'center'">Status</th>
- </tr>
- </thead>
- </table></span>
<table id="dg" class="easyui-datagrid" title="數據" style="width:700px;height:250px"
data-options="
rownumbers:true,
singleSelect:false,
showFooter: true,
url:'<%=request.getContextPath() %>/js/jquery-easyui-1.3.3/demo/datagrid/datagrid_data2.json'
">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true"></th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:150, formatter:formatProduct">Product</th>
<th data-options="field:'listprice',width:80,align:'right'">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
<th data-options="field:'attr1',width:170">Attribute</th>
<th data-options="field:'status',width:60,align:'center'">Status</th>
</tr>
</thead>
</table>
效果以下:
你們能夠看到多出來了footer可是好像不是很美觀,咱們稍微做下處理,將formatProduct函數內容改寫下,代碼以下:
- <span style="font-size:18px;">function formatProduct(val,row,index){
- if(<span style="color:#ff0000;">"undefined" != typeof(row.productid)</span>){
- return "<input type='text' id='ch'"+row.itemid+" value='0' ></input>";
- }
- }</span>
function formatProduct(val,row,index){
if("undefined" != typeof(row.productid)){
return "<input type='text' id='ch'"+row.itemid+" value='0' ></input>";
}
}
請注意紅色部分,因爲footer中沒有productid,因此判斷當數據定義了productid格式化爲編輯框,不然不格式化爲編輯框。
效果以下:
總結
本實例中省略了一些內容,包括提交,取消,選擇checkbox時,進行product的求和,同後臺的交互(徹底jQuery異步請求)。作爲EasyUI入門的因子,但願對初學的人能有所幫助。
有footer的json數據以下,這個也很重要,若是json數據格式不正確將沒法出現上述效果:
- {"total":28,"rows":[
- {"productid":"FI-SW-01","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
- {"productid":"K9-DL-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
- {"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":28.50,"attr1":"Venomless","itemid":"EST-11"},
- {"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
- {"productid":"RP-LI-02","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
- {"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
- {"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
- {"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":63.50,"attr1":"Adult Female","itemid":"EST-16"},
- {"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
- {"productid":"AV-CB-01","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
- ],"footer":[
- {"unitcost":19.80,"listprice":60.40,"itemid":"Average:"},
- {"unitcost":198.00,"listprice":604.00,"itemid":"Total:"}
- ]}
{"total":28,"rows":[
{"productid":"FI-SW-01","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":28.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":63.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
],"footer":[
{"unitcost":19.80,"listprice":60.40,"itemid":"Average:"},
{"unitcost":198.00,"listprice":604.00,"itemid":"Total:"}
]}
另外你們能夠去EasyUI的官網學習:http://www.jeasyui.com/,最新的api提供了更簡便的操做。
偶爾會出現官網上不去的狀況,請參看國內的網址,缺點是api不是最新:http://www.phptogether.com/juidoc/
後續會補充上datagrid的分頁,datagrid掌握後並有jquery的基礎能夠說easyui就會用了,期待學習EasyUI的朋友可以發掘更多實用的技術。