mysql使用jtable_jtable 的简单使用
做后臺管理管理系統時,基于ajax的數據操作和富有表現力的數據綁定插件jtable絕對是一個不錯的選擇,他接收來自服務器端的json格式的數據。而且他是一款開源的基于jquery和jquery ui的插件,您可以根據自己的需要修改其表現,如css,甚至修改其源碼,讓其符合您的需求。
下面我將介紹在asp.net mvc3.0 和ssh框架下jtable的使用
2 將相應的css (jtable.css)和jquery.jtable.zh-CN.js、jquery.jtable.min.js拷到您的項目下。
3?引入插件,在view中,一般放在模板頁中,為了簡單,我放在AdministratorController下的Index Action對應的視圖中,即Index.aspx頁面中
4 編寫javascript代碼綁定數據
$("#smallTypeList").jtable({
title:"商品小類別管理列表",
paging:true,
pageSize:10,
selecting:true, //Enable selecting
multiselect: true, //Allow multiple selecting
selectingCheckboxes: true,
actions: {
listAction:"/Administrator/GoodsSmallTypeList",
createAction:"/Administrator/GoodsSmallTypeCreate",
updateAction:'/Administrator/GoodsSmallTypeUpdate',
deleteAction:'/Administrator/GoodsSmallTypeDelete'},
fields: {
sid: {
key:true,
list:false,
create:false,
edit:false},
sname: {
title:"名稱",
width:"30%",
inputClass:"validate[required]"},
cid: {
title:"類別",
width:"30%",
options:"/Administrator/CateList"},
spic: {
title:"類別圖片",
width:"30%",
options: {'暫無':'暫無'}
}});
$("#smallTypeList").jtable("load");
});
5 在controller中輸出json
為了簡單,省略Models層的代碼,筆者認為您已經具備一定的asp.net mvc的基礎知識。
綁定數據,注意參數(jtableStartIndex,jtPageSize)
第一個參數用來指定當前起始記錄,第二個用來指定一頁顯示的記錄行,用這兩個參數實現分頁。
public JsonResult GoodsSmallTypeList(int jtStartIndex, intjtPageSize)
{try{int totalCount =goodscateEntity.getAllCategorys().Count();var goodsSmallList =goodscateEntity.getAllCategorys().Skip(jtStartIndex).Take(jtPageSize);return Json(new { Result = "OK", Records = goodsSmallList, TotalRecordCount =totalCount });
}catch(Exception ex)
{return Json(new { Result = "ERROR", Message =ex.Message.ToString() });
}
}
代碼中,返回json時參數的OK表示請求狀態,Records表示數據集合,TotalRecordCount表示總記錄數。一般這些參數的約定好的,不可改成其他,除非你不愿意使用。您可以在jtable源碼中進行修改。
publicJsonResult GoodsSmallTypeCreate(tb_goodsCategory category)
{try{if (!ModelState.IsValid)
{return Json(new { Result = "ERROR", Message = "請填寫信息完整"});
}bool l =goodscateEntity.InsertGoodsCategory(category);return Json(new { Result = "OK", Record =category });
}catch(Exception ex)
{return Json(new { Result = "ERROR", Message =ex.Message.ToString() });
}
}///
///修改商品類型///
///
///
publicJsonResult GoodsSmallTypeUpdate(tb_goodsCategory category)
{try{bool l =goodscateEntity.ModifyGoodsCateGory(category);return Json(new { Result = "OK", Record =category });
}catch(Exception ex)
{return Json(new { Result = "ERROR", Message =ex.Message.ToString() });
}
}///
///刪除商品類型///
///
///
public JsonResult GoodsSmallTypeDelete(int?sid)
{try{bool l =goodscateEntity.DeleteGoodsCategory(sid);return Json(new { Result = "OK"});
}catch(Exception ex)
{return Json(new { Result = "ERROR", Message =ex.Message.ToString() });
}
}
運行結果:
這樣,jtable的使用描述就此完成,本人技術有限,文中還有許多不足,希望大家批評指正,謝謝。
使用 SSH很簡單,只要的struts.xml加入相關配置,使其返回的數據為json即可。?當然要引入json對應的jar包哦。希望對大家有用。
總結
以上是生活随笔為你收集整理的mysql使用jtable_jtable 的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UML在线画图工具ProcessOn
- 下一篇: es6 --- Thunk函数的作用