背景介绍
技术背景
后端写回数据代码
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class AppAction extends ActionSupport {
public HttpServletResponse getResponse(){
HttpServletResponse resp = ServletActionContext.getResponse();
return resp;
}
public void writeString(String msg){
msg = msg==null?"":msg;
getResponse().setCharacterEncoding("utf-8");
getResponse().setContentType("text/html; charset=utf-8");
PrintWriter pw=null;
try {
pw = getResponse().getWriter();
pw.write(msg);
} catch (IOException e) {
log.error("输出异常",e);
}finally{pw.close();}
}
}
按照lyaui table组件说明的封装数据
public JSONObject listJson(){
JSONObject json = new JSONObject();
json.put("code", 0);
json.put("count", 0);//数据总数
json.put("data", "");//sql查询的数据集 List<Object> list
//调用前面封装好的servlet response 输出流
writeJson(json);
}
最重要的一步
layui.use('table',function(){
table = layui.table;
table.render({
elem:'#info',
autoSort:false,
contentType:'application/json;charset=utf-8',
method:'post',
url:url,
cols:[[
{type:'checkbox',width:'3%'}
]]
,done:function(res,curr,count){
}
});