easyui field 获取对象子属性的值

我们从服务器获取的数据格式如下:
{“total”:10,”rows”:[{“orderId”:”4”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:2,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”6”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:2,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”8”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:4,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已发货”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”9”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:7,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:null,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”12”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:3,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”未发货”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”31”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:2,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”32”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:1,”createTime”:1510029825000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”未付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”3”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:2,”createTime”:1510029784000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”2”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:2,”createTime”:1510029769000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”已付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null},{“orderId”:”1”,”payment”:”1”,”paymentType”:1,”postFee”:”1”,”status”:1,”createTime”:1509986257000,”updateTime”:1509986263000,”paymentTime”:1510072665000,”consignTime”:1510245468000,”endTime”:1510331880000,”closeTime”:1510331883000,”shippingName”:”中通快递”,”shippingCode”:”34343131231”,”userId”:7,”buyerMessage”:”尽快发货”,”buyerNick”:”zhl”,”buyerRate”:null,”formatStatus”:”未付款”,”user”:{“id”:7,”username”:”zhangsan”,”password”:”e10adc3949ba59abbe56e057f20f883e”,”phone”:”13488888888”,”email”:”aa@a”,”created”:1428311035000,”updated”:1428311035000,”isadmin”:0},”orderDetails”:null}]}

现在我们要从user中获取id,username,phone

<th field='userID1',width:100"  data-options="formatter:function(val,row){

                if (row.user.id){
               return   row.user.id;
           }}" >用户ID</th>  
            <th field="userName1" data-options="formatter:function(val,row){
                if (row.user.username){
               return   row.user.username;
           }  
              }",width:100" >用户 </th>
            <th field='userPhone1',width:150"  data-options="formatter:function(val,row){
                if (row.user.phone){
               return   row.user.phone;
           }}" >用户电话</th> 

filed可以随便起 ,不要用user,这样会覆盖后面的属性,用户和phone都会变成id

easyUI版本:1.4.2
由于easyUI在设置field的时候使用的对象的属性名,若该属性具有多个子属性且在页面需要同时展示时就会出现展示数据一样的困扰,例如:
队员 person
名称
name
电话号码
phoneNum
所属队伍
team
队伍 team
队伍名称
name
队伍类型
teamType
在查询时想展示的效果:

js代码
[javascript] view plain copy
columns: [[
{
field: ‘name’,
title: ‘姓名’,
width: 100,
},
{
field: ‘phoneNum’,
title: ‘电话号码’,
width: 100,
},
{
field: ‘team’,
title: ‘所属队伍’,
width: 100,
formatter:function(val){
if(val){
return val.teamName;
}
},
},
{
field: ‘team’,
title: ‘队伍类型’,
width: 100,
formatter:function(val){
if(val){
return val.teamType;
}
},
]],
虽然我们在队伍的不同属性返回时设置了不同的返回值,但是页面展示的效果却是

发现最后两列的内容是一样。
那么如何才能展示呢,查看了一下api,发现formatter中function的参数可以是function(value,row,index),那我们就可以从row这个参数上下手了,将代码修改为如下:
[javascript] view plain copy
{
field: ‘teamName’,
title: ‘所属队伍’,
width: 100,
formatter:function(val, row, index){
if(row.teamInfo){
returnrow.teamInfo.teamName;
}
},
},
{
field: ‘teamType’,
title: ‘队伍类型’,
width: 100,
formatter:function(val, row, index){
if(row.teamInfo){
return row.teamInfo.teamType;
}
},
},
在次查看数据就是我们需要的样子了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值