这个是,后台得到json格式的数据,类型是双层嵌套的,
现在前端的需求是,页面显示本表ideas和user表的数据(多表联查);sql是left join on
然后,前端需要鼠标上移动,看到user表里的某些字段的详情。
easyui默认的表格渲染只能渲染到第一层的数据,即本表本字段,联表的数据无法显示,所以需要手动再次封装一下数据
做法:利用easyui封装的 formatter属性,在field.inc里手动写一个,然后在js里写函数,
下面是easyui封装的表格列:
<thead>
<tr>
<th data-options="field:'selector',checkbox:true"></th>
<th data-options="field:'id',width:40,align:'center',hidden:false">编号</th>
<th formatter="formatterTooltip" data-options="field:'user.name',width:100,align:'center'" >用户姓名</th>
<th data-options="field:'type',width:100,align:'center'">点子类型</th>
<th data-options="field:'title',width:200,align:'center'">点子标题</th>
<th data-options="field:'supcount',width:100,align:'center'">点赞数量</th>
<th data-options="field:'context',width:500,align:'center'">具体内容</th>
<th data-options="field:'imgurl',width:400,align:'center'">图片地址</th>
<th data-options="field:'time',width:150,align:'center'">创建时间</th>
<th data-options="field:'userId',width:80,align:'center',hidden:true" >用户编号</th>
</tr>
</thead>
下面是 ideas.js的内容,主要是手动写一个function,目的是解析rows数组,然后拼一个新的数据包,返回给表格渲染。
/**
* <summary>初始化name列</summary>
* <return>鼠标移动到单元格上方显示个人详情</return>
*/
function formatterTooltip(value,row){
//把null替换为空串
var rowToStr = JSON.stringify(row);
row = JSON.parse(rowToStr.replace(/null/g,"\"\""));
console.log(row);
//添加tooltip内容
value = '<a href="javascript:void(0)" title="';
value += "电话 : "+row.user.phone+"\n";
value += "QQ : "+row.user.qq+"\n";
value += "微信 : "+row.user.wechat+"\n";
//手动转换,使得数据0-1-2的类型 变为 汉子显示到前端页面
var userType = row.user.usertype;
if(userType == 0){
userType = "个人";
}else if(userType == 1){
userType = "社团";
}else if(userType == 2){
userType = "管理员";
}
value += "用户类型 : "+userType+"\n";
value +='" class="easyui-tooltip">';
value += '<span style="color:#104E8B">'+row.user.name+'</span>';
value +='</a>';
console.log(value);
return value;
}