jquery-easyui(替代 extjs) 介紹

來源:互聯網
上載者:User

摘自:http://hi.baidu.com/zhizhesky/blog/item/1485444a2b62f32808f7ef1d.html

自從 extjs 商業化後,一些類似於 extjs的架構開始初露頭角,jquery-easyui 就是這樣一個能達到同樣效果的架構,現在已經越來越成熟了,它比extjs 簡單易用,架構也很不同,Easyui is a collection of user-interface plugin based on jQuery.

比如一個datagrid,他是這樣寫的:

$('#tt').datagrid({
url:'/demo/user/getUsers',
queryParams:{
id:'001',
state:'ok'
}
});



分頁:<div id="pp" style="background:#efefef;border:1px solid #ccc;"></div>
$('#pp').pagination(options);


他的官網:http://jquery-easyui.wikidot.com/start

例子:(http://www.javaeye.com/topic/576846)

利用easyui編寫一個使用者管理小例子,目的是示範CRUD操作。先看一下:




1、表格的定義:

<table id="user-table">
<thead>
<tr>
<th field="name" width="100">名稱</th>
<th field="phone" width="100">電話</th>
<th field="addr" width="100">地址</th>
<th field="duty" width="100">職務</th>
</tr>
</thead>
</table>

利用表格的THEAD來定義列,field屬性對應使用者資料模型的欄位名稱。

接著用jQuery建立表格,同時建立一個操作工具列:

grid = $('#user-table').datagrid({
url:'/demo1/user/getUsers',
title:'使用者資料',
width:600,
height:300,
singleSelect:true,
toolbar:[{
text:'新增',
iconCls:'icon-add',
handler:newUser
},'-',{
text:'修改',
iconCls:'icon-edit',
handler:editUser
},'-',{
text:'刪除',
iconCls:'icon-remove'
}]
});



2、定義使用者資訊視窗和表單
Html代碼

<div id="user-window" title="使用者視窗" style="width:400px;height:250px;">
<div style="padding:20px 20px 40px 80px;">
<form method="post">
<table>
<tr>
<td>名稱:</td>
<td><input name="name"></input></td>
</tr>
<tr>
<td>電話:</td>
<td><input name="phone"></input></td>
</tr>
<tr>
<td>地址:</td>
<td><input name="addr"></input></td>
</tr>
<tr>
<td>職務:</td>
<td><input name="duty"></input></td>
</tr>
</table>
</form>
</div>
<div style="text-align:center;padding:5px;">
<a href="javascript:void(0)" onclick="saveUser()" id="btn-save" icon="icon-save">儲存</a>
<a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" icon="icon-cancel">取消</a>
</div>
</div>

可以看到,視窗是一個DIV,表單是一個FORM,這種建立方式具有極大的靈活性,不需要學習成本,建立的jQuery代碼如下:

$('#btn-save,#btn-cancel').linkbutton();
win = $('#user-window').window({
closed:true
});
form = win.find('form');

其中建立了視窗的操作按鈕,並擷取表單對象。



3、進行CRUD操作的用戶端代碼:

function newUser(){
win.window('open');
form.form('clear');
form.url = '/demo1/user/save';
}
function editUser(){
var row = grid.datagrid('getSelected');
if (row){
win.window('open');
form.form('load', '/demo1/user/getUser/'+row.id);
form.url = '/demo1/user/update/'+row.id;
} else {
$.messager.show({
title:'警告',
msg:'請先選擇使用者資料。'
});
}
}
function saveUser(){
form.form('submit', {
url:form.url,
success:function(data){
eval('data='+data);
if (data.success){
grid.datagrid('reload');
win.window('close');
} else {
$.messager.alert('錯誤',data.msg,'error');
}
}
});
}
function closeWindow(){
win.window('close');
}





例子中使用etmvc架構來處理背景資料,示範例子中不使用資料庫。

定義使用者資料模型:

public class User {
public Integer id;
public String name;
public String phone;
public String addr;
public String duty;

public User clone(){
User u = new User();
u.id = id;
u.name = name;
u.phone = phone;
u.addr = addr;
u.duty = duty;
return u;
}
}

定義後台使用者的CRUD操作:

public class UserController extends ApplicationController{
/**
* 返回全部使用者資料
*/
public View getUsers() throws Exception{
Map<String,Object> result = new HashMap<String,Object>();
result.put("total", users.size());
result.put("rows", users);

return new JsonView(result);
}

/**
* 取得指定的使用者資料
*/
public View getUser(Integer id) throws Exception{
User user = users.get(id-1);
return new JsonView(user);
}

/**
* 儲存使用者資料,這裡對使用者名稱稱進行非空檢驗,僅供示範用
*/
public View save(User user) throws Exception{
Map<String,Object> result = new HashMap<String,Object>();
if (user.name.length() == 0){
result.put("failure", true);
result.put("msg", "使用者名稱稱必須填寫。");
} else {
result.put("success", true);
user.id = users.size()+1;
users.add(user);
}
View view = new JsonView(result);
view.setContentType("text/html;charset=utf-8");
return view;
}

/**
* 更新指定的使用者資料
*/
public View update(Integer id) throws Exception{
Map<String,Object> result = new HashMap<String,Object>();
User user = users.get(id-1).clone();
updateModel(user);
if (user.name.length() == 0){
result.put("failure", true);
result.put("msg", "使用者名稱稱必須填寫。");
} else {
result.put("success", true);
user.id = id;
users.set(id-1, user);
}
View view = new JsonView(result);
view.setContentType("text/html;charset=utf-8");
return view;
}

// 使用者資料測試資料
private static List<User> users = new ArrayList<User>();
static{
for(int i=1; i<10; i++){
User user = new User();
user.id = i;
user.name = "name" + i;
user.phone = "phone" + i;
user.addr = "addr" + i;
user.duty = "duty" + i;

users.add(user);
}
}
}



完整的代碼請見附件,可以看到,easyui具有極少的JS代碼。

demo1.rar (646.4 KB)


jQuery模仿ExtJS之TabPanel

jquery-easyui編寫使用者管理小例子

基於jquery-easyui的機電裝置管理系統布局
新範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.