这里以Roles角色表修改功能做一个例子
RolesController
/**
* 角色管理控制类
* @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);
}
}