当json中object嵌套,datagrid中如何显示数据

这里写图片描述

后台

model数据

java
@Entity
public class Person {
    private int id; //自增长
    private String identy; //身份证
    private String name; //姓名
    private Date birthday; //出生日期
    private Person daddy; //父亲
    private Person mother; //母亲

Action

用Hibernate将数据读取出来,List对象;然后转为Json对象,传回前台

java
@Resource HibernateTemplate hibernateTemplate;

    @RequestMapping("/showPerson")
    public void showPerson(HttpServletResponse response){
        List<Person> personList = new ArrayList<Person>();
        personList = hibernateTemplate.find("from Person");
        String json = JSONUtils.writeValueAsString(personList);  //转为json对象  
        HTMLUtils.writeJosn(response, json); //json转到前台
    }

前台

在th的data-options字段要注意外,还要注意formatter字段,对应在js的方法 进行数据处理

java
<table class="easyui-datagrid" title="个人信息表" style="width:1200px;height:500px"
            data-options="rownumbers:true,singleSelect:true,url:'<%=request.getContextPath()%>/showPerson.do',method:'get'">
        <thead>
            <tr>
                <th data-options="field:'identy',width:220,align:'center'" rowspan="2">身份证号</th>
                <th data-options="field:'name',width:200,align:'center'" rowspan="2">姓名</th>
                <th data-options="field:'birthday',width:200,align:'center'" rowspan="2">出生日期</th>
                <th colspan="2">家庭关系</th>
            </tr>
            <tr>
                <th data-options="field:'daddy',width:260,align:'center'" formatter="deal_daddy">父上大人</th>
                <th data-options="field:'mother',width:260,align:'center'" formatter="deal_mother">母上大人</th>
            </tr>
        </thead>
    </table>

JS

java
<script>
function deal_daddy(value,row,rowIndex){
    if(row.daddy!=undefined){
        return row.daddy.name;
    }
}
function deal_mother(value,row,rowIndex){
    if(row.mother!=undefined){
        return row.mother.name;
    }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值