這裏以Roles角色表修改功能作一個例子html
RolesControllersql
/**
* 角色管理控制類
* @author 御手洗紅豆
*/
public class RolesController extends Controller {
/**
* 貨品管理初始頁面方法,查詢第一頁數據
* @author 御手洗紅豆
*/
public void index() {
// 轉入客戶grid管理頁面
render("/businessPage/rolesPage/gridPage.html");
}
/**
* 獲取貨品信息列表的json,帶分頁信息,轉入gridPage
* @author 御手洗紅豆
*/
public void getdatetojson() {
// 得到jqGrid傳入參數
JqGridBaseBean searchBean = getBean(JqGridBaseBean.class, "", true);
// 總記錄得到語句
String countSql = "select count(*) totalRow from roles where isdel=0 ";
// 編輯查詢語句
String sqlstr = "select * from roles where isdel=0 ";
// 判斷是否有檢索條件,若是沒有,前臺不傳檢索參數,獲取會報錯。
if(searchBean.is_search()){
// 網上說有單字段查詢的時候,可是在ACE框架給的頁面中沒發現有,暫時保留這種寫法
if(searchBean.getJqGridSearch().getSearchField() != null && searchBean.getJqGridSearch().getSearchOper() != null){
// 單字段查詢的時候
countSql +=" and (" + JqGridOpSwitch.opSwitchMySql(searchBean.getJqGridSearch().getSearchField(), searchBean.getJqGridSearch().getSearchOper(), searchBean.getJqGridSearch().getSearchString(), 2)+")";
sqlstr +=" and (" + JqGridOpSwitch.opSwitchMySql(searchBean.getJqGridSearch().getSearchField(), searchBean.getJqGridSearch().getSearchOper(), searchBean.getJqGridSearch().getSearchString(), 2)+")";
}else if(searchBean.getJqGridSearch().getGroupOp() != null && searchBean.getJqGridSearch().getRules()!= null && searchBean.getJqGridSearch().getRules().size() != 0){
// 多字段查詢的時候
countSql +=" and (";
sqlstr +=" and (";
for(int i = 0; i < searchBean.getJqGridSearch().getRulesList().size(); i++){
if(i > 0){
countSql += searchBean.getJqGridSearch().getGroupOp();
sqlstr += searchBean.getJqGridSearch().getGroupOp();
}
JqGridSearchDetailBean tempBean = searchBean.getJqGridSearch().getRulesList().get(i);
countSql += JqGridOpSwitch.opSwitchMySql(tempBean.getField(),tempBean.getOp(),tempBean.getData(),2);
sqlstr += JqGridOpSwitch.opSwitchMySql(tempBean.getField(),tempBean.getOp(),tempBean.getData(),2);
}
countSql +=") ";
sqlstr +=") ";
}
}
sqlstr += " limit " + ((searchBean.getPage()-1) * searchBean.getRows()) + "," + searchBean.getRows();
// 取得總記錄數
long totalRow = RolesModel.dao.findFirst(countSql).getLong("totalRow");
// 計算總頁數,不能整除的進1
int totalPage = BigDecimal.valueOf(totalRow).divide(BigDecimal.valueOf(searchBean.getRows()), BigDecimal.ROUND_UP).intValue();
// 取得查詢內容
List<RolesModel> roleslist = RolesModel.dao.find(sqlstr);
Map<String, Object> map = new HashMap<String, Object>();
map.put("page", searchBean.getPage());
map.put("totalPage", totalPage);
map.put("totalRecords", totalRow);
map.put("list", roleslist);
renderJson(map);
}
/**
* jqGrid的數據操做
* @author 御手洗紅豆
*/
public void editfromgrid(){
// 得到操做類別
String oper = getPara("oper");
// 得到操做對象
RolesModel roles = getModel(RolesModel.class,"",true);
if(oper.equals("add")){
// 添加
roles.save();
}else if(oper.equals("edit")){
// 修改
roles.update();
}else if(oper.equals("del")){
// 刪除
roles.set("isdel", 1);
roles.update();
}
renderText("succeed");
}
/**
* 獲取用於下拉列表填充的數據
* @author 御手洗紅豆
*/
public void getalldatetotext() {
// 編輯查詢語句
String sqlstr = "select * from roles where isdel=0";
// 取得查詢內容
List<RolesModel> roleslist = RolesModel.dao.find(sqlstr);
String str = "";
for(int i = 0; i < roleslist.size(); i++){
RolesModel tempModel = roleslist.get(i);
if(i>0){
str += ";";
}
str += tempModel.get("id").toString() + ": " + tempModel.getStr("name");
}
renderText(str);
}
}
json