========================jsp==============================
<table id="dg" fit="true" width="100%">
<thead>
<tr>
<th field="aguid" hidden="true">主键</th>
<th field="ck" checkbox="true"></th>
<th field="aname" sortable="true">用户名</th> <!--sortable="true",这个属性是用户点击标题可以排序-->
<th field="guid_job" sortable="true" formatter="formatterJob.Formatter_job">职位</th><!--这里直接写对象名,formatterJob.Formatter_job获取职务的名称-->
<th field="guid_bm" sortable="true" formatter="formatterBm.Formatter_bm">部门</th><!--这里直接写对象名,formatterBm.Formatter_bm获取部门的名称-->
<th field="mobilephone" sortable="true">手机</th>
<th field="email" sortable="true">邮箱</th>
<th field="isworking" sortable="true" formatter="formatterStatus.Formatter_status">状态</th><!--formatterStatus.Formatter_status格式化数据-->
<th field="isoperator" formatter="formatterisIsoperator.Formatter_isoperator">是否允许登录</th><!--formatterisIsoperator.Formatter_isoperator格式化数据-->
<th field="操作" formatter="datagridformatter.operformatter">操作</th><!--datagridformatter.operformatter在表格的最后一格添加功能按钮-->
</tr>
</thead>
<script>
// 初始化表格数据
function loadinit() {
$('#dg').datagrid({
url : '/jubcrm/index/qryUser.hml',//返回后台路径
idField : 'aguid',
pagination : true,
rownumbers : true,
singleSelect : false
});
}
</script>
===============js===============
//格式化datagrid获取对象的属性名称bm
var formatterBm = {
Formatter_bm : function(val, rec, index) {
if (val == undefined) {
return "";
}
return rec.guid_bm.aname;//返回部门名称(部门是一个对象即用户的外键)
}
};
//格式化datagrid获取对象的属性名称job
var formatterJob = {
Formatter_job : function(val, rec, index) {
if (val == undefined) {
return "";
}
return rec.guid_job.aname;//返回职务名称(职务是一个对象即用户的外键)
}
};
//格式化datagrid获取对象的属性名称isworking,该值是true或者false在jsp中需要转换成中文的格式
var formatterStatus = {
Formatter_status : function(val, rec, index) {
if (val == undefined) {
return "";
}
if(val==false){
return "在职";
}else if(val==true){
return "离职";
}
}
};
//格式化datagrid获取对象的属性名称是否操作员,该值是true或者false在jsp中需要转换成中文的格式
var formatterisIsoperator = {
Formatter_isoperator : function(val, rec, index) {
if (val == undefined) {
return "";
}
if(val==true){
return "允许登录";
}else{
return "禁止登录";
}
}
};
var datagridformatter = {
operformatter : function (val, row, index) {
var e = '<a href="#" onclick="editrow(' + index + ')">编辑</a> ';
var d = '<a href="#" onclick="deleterow(' + index + ')">删除</a>';
return e + d;
}
};
================================java后台代码======================================
===========================业务层servicesImpl==================
// 用户管理查询用户信息
@SuppressWarnings("unchecked")
public String qrypage(Pubuser pub, String page, String rows, String order,String sort) {
StringBuffer jql = new StringBuffer();
jql.append(" 1=1 ");
if (szf == 65536) {
jql.append(" and status = " + szf);
} else if (szf == 0) {
jql.append(" and (status=0 or status is null) ");
}
Boolean sxz = pub.getIsxz();
if (sxz != null) {
jql.append(" and isxz = " + sxz);
}
long total = pubbasedao.getCount(Pubuser.class, jql.toString(), null);//查询总记录数
int pagenum = 1;
if (!page) {
pagenum =page;
}
int rowsnum = JToolType.defrowsnum;
if (!rows) {
rowsnum = rows;
}
if (order) {
order = "DESC";
}
if (sort) {
sort = "date_in";
}
LinkedHashMap<String, String> ordermap = new LinkedHashMap<String, String>();
if (!order) {
ordermap.put(sort, order);
}
List<Pubuser> lstrows = pubbasedao.qryPage(Pubuser.class, pagenum,
rowsnum, jql.toString(), null, ordermap);//查询数据
return formatDataGridPage(total, lstrows);
}
/**
* 格式化Controller返回DataGrid分页数据<br>
* lstrows必须为list
* fastjson-1.1.33.jar
*/
public static String formatDataGridPage(long total, Object lstrows) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("total", total);
result.put("rows", lstrows);
return parse2JSON(result);
}
=============dao,我这里用的是JPA,如果是ssh用户基本一样=======================
@Override
public List<T> qryPage(Class<T> clazz, int firstindex, int maxresult,
String whQl, Object[] params, LinkedHashMap<String, String> orderby) {
Query q = em.createQuery("SELECT u FROM " + clazz.getSimpleName()
+ " u " + (whQl ? "" : " where " + whQl)
+orderby);
setQueryParams(q, params);
if (firstindex >= 0) {
// q.setFirstResult(firstindex);
q.setFirstResult((firstindex - 1) * maxresult);
}
if (maxresult > 0) {
q.setMaxResults(maxresult);
}
return q.getResultList();
}
public void setQueryParams(Query query, Object[] params) {
try {
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i + 1, params[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
==============================控制层action,我用的是springMVC=======================================
@Controller
@RequestMapping("index")
public class PubuserAction {
@Autowired
public PubuserServices pub;
// 刷新信息
@RequestMapping("qryUser")
public void qryUser(Pubuser pubuser, HttpServletRequest request,
HttpServletResponse response) throws IOException {
Pubuser us = (Pubuser) request.getSession().getAttribute(
JToolType.AttributeUserName);
pubuser.setGuid_gs(us.getGuid_gs());
String rslt = pub.qrypage(pubuser, request.getParameter("page"),
request.getParameter("rows"), request.getParameter("order"),
request.getParameter("sort"));
response.getWriter().write(rslt);
response.getWriter().close();
}