// 此代码为第一个专业设计代码,代码属实写得太难看了。
Controller
代码如下:
@RequestMapping("/empsList")
@ResponseBody
public LayuiTransform<Employee> empsList(@RequestParam("empsPage") Integer page
, @RequestParam("empsSize") Integer size
, @RequestParam("condition") String condition){
//System.out.println("-------------------------------------------condition="+condition);
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
HSMSservice serv=ac.getBean("HSMSservice",HSMSservice.class);
PageTool pt=new PageTool((page-1)*10,size);
LayuiTransform<Employee> Lemps=new LayuiTransform<Employee>();
if(condition.equals("")) {
List<Employee> emps=serv.showEmpPage(pt);
int count=serv.showEmpCount();
Lemps=new LayuiTransform<Employee>(0, "", emps,count);
//System.out.println("----------------------------------------------"+Lemps);
}else {
Condition c=new Condition(condition);
System.out.println("-------------------------------------------c="+c);
Employee emp=serv.searchEmp(c);
System.out.println("-------------------------------------------emp="+emp);
// System.out.println(emp);
int count=0;
List<Employee> newEmps=new ArrayList<>();
newEmps.add(emp);
Lemps=new LayuiTransform<Employee>(0, "", newEmps,count);
System.out.println("----------------------------------------------"+Lemps);
if(emp==null) {
List<Employee> newEmps2=new ArrayList<>();
count=1;
Employee nemp=new Employee();
newEmps2.add(nemp);
Lemps=new LayuiTransform<Employee>(2, "查无此项", newEmps2,count);
}
}
System.out.println("----------------------------------------------=="+Lemps);
return Lemps;
}
jsp
table.render({
elem: '#empsTable' //table的id
,url:'empsList' //数据接口
,page: true //开启分页
,limit:10
,limits:[10]
,cols: [[//表头
{field:'employee_id', width:150, title: '员工号'}
,{field:'employee_name', width:150, title: '员工姓名'}
,{field:'employee_gender', width:80, title: '性别'}
,{field:'employee_age', width:80, title: '年龄'}
,{field:'employee_position', width:150, title: '员工职位'}
,{fixed: 'right', width: 500, align:'center', toolbar: '#operation'}
]]
,where: {condition:''}
,id:'empsReload'
,request:{
pageName:'empsPage', //默认page
limitName:'empsSize' //默认limit
}
});
重点在于
Controller return给前台的是一个LayuiTransform<Employee>
类型的数据(这里的Lemps),就是一个将Employee实体类包装成一个Layui要求的格式,比如
LayuiTransform [code=0, msg=, data=
[Employee [employee_id=1, employee_age=21, employee_name=yky,
employee_gender=男, employee_position=总经理]], count=0]
而在实现layui根据给定关键字搜索数据库时,有可能要搜索的关键字在数据库中不存在,那么sql语句搜索返回的就是一个null。
也就是说这个时候我要传给前台的数据是这样的(控制台打印)
这样数据通过数据接口传到前台后页面就是这样的
console报错
报的什么错呢?无法读取null的length属性。
当后台传入的是一个null,(好像是因为layui的table的初始排序的原因)layui的table将无法渲染table。
页面这样转圈圈对用户很不友好,所以我们想办法让她显示成“无数据”、“查无此项”等 来给用户提示。
怎么实现呢?
if(emp==null) {
List<Employee> newEmps2=new ArrayList<>();
count=1;
Employee nemp=new Employee();
newEmps2.add(nemp);
Lemps=new LayuiTransform<Employee>(1, "查无此项", newEmps2,count);
}
new一个LayuiTransform<> 将其code设置为1,msg设为"查无此项"(layui默认code=0时table才读取数据)
再把它赋值给要返回给前台的Lemps
成功!
附上工具类LayuiTransform 代码
ackage com.yky.pojo;
import java.util.List;
import com.yky.service.HSMSservice;
public class LayuiTransform<T> {
private int code;
private String msg;
private List<T> data;
private int count;
public LayuiTransform(int code, String msg, List<T> data, int count) {
super();
this.code = code;
this.msg = msg;
this.data = data;
this.count = count;
}
public LayuiTransform() {
super();
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String toJson() {
Gson gson = new Gson();
String json = gson.toJson(this);
return json;
}
@Override
public String toString() {
return "LayuiTransform [code=" + code + ", msg=" + msg + ", data=" + data + ", count=" + count + "]";
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
欢迎讨论和提问!(如果博客对你有帮助给我点个赞吧)