如何在Jquery Jtable中绑定json嵌套对象?

时间:2014-01-31 09:33:29

标签: java jquery gson jquery-jtable

表格可以显示idNodateOfBirthgender,但nameaddress无法显示。

转换java customer list后,它将是:

{
  ...
  "idNo": "MyNRC",
  ...
  "address": {
    "homeNo": "No-27",
    "street": "12th street",
    "city": "MyCity",
    "province": "MyProvince"
  },
  "name": {
    "firstName": "Jone",
    "middleName": "",
    "lastName": "Hon"
  }
}

我的字段绑定脚本中缺少什么?

CustomerManage.java< - servlet

....
List<Customer> customerList = customerService.findAllCustomer();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customer>>(){}.getType());
JsonArray jsonArray = element.getAsJsonArray();
String listData = jsonArray.toString();
listData = "{\"Result\":\"OK\",\"Records\":" + listData + "}";
response.setContentType("application/json");
response.getWriter().print(listData);

Customer.java

public class Customer {
    private String idNo;
    private Date dateOfBirth;
    private Gender gender;
    private Address address;
    private Name name;
}   

Address.java

public class Address {
    private String homeNo;
    private String street;
    private String city;
    private String province;
}   

Name.java

public class Name {
    private String firstName;
    private String middleName;
    private String lastName;
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#CustomerTableContainer').jtable({
            title: 'Manage Customer',
            actions: {
                listAction: 'CustomerManage?action=list',
                createAction:'CustomerManage?action=create',
                updateAction: 'CustomerManage?action=update',
                deleteAction: 'CustomerManage?action=delete'
            },
            fields: {
                idNo: {
                    title: 'NRC-No',
                    key: true,
                    width: '20%',
                    edit: false
                },
                dateOfBirth: {
                    title: 'Date Of Birth',
                    key: true
                },
                gender: {
                    title: 'Gender',
                    key: true
                },
                address: {
                    title: 'Address',
                    homeNo: {
                        title: 'Home-No',
                        key: true
                    },
                    street: {
                        title: 'Street',
                        key: true
                    },
                    city: {
                        title: 'City',
                        key: true
                    },
                    province: {
                        title: 'Province',
                        key: true
                    }
                },
                name: {
                    firstName: {
                        title: 'First Name',
                        key: true
                    },
                    middleName: {
                        title: 'Middle Name',
                        key: true
                    },
                    lastName: {
                        title: 'Last Name',
                        key: true
                    }
                }
            }
        });
        $('#CustomerTableContainer').jtable('load');
    });
</script>

1 个答案:

答案 0 :(得分:4)

希望现在还为时不晚。使用关键字“record”可帮助您从检索到的json数据中访问嵌套字段。请参阅下面的示例。

.....

gender: {
         title: 'Gender',
         key: true
},

homeNo: {
        title: 'Home-No',
        key: true,
        display:function(data){
                   return data.record.address.homeNo;
        }
},
street: {
        title: 'Street',
        key: true,
        display:function(data){
                   return data.record.address.street;
           }
},
city: {
      title: 'City',
      key: true,
      display:function(data){
                return data.record.address.city;
      }
},
province: {
           title: 'Province',
           key: true,
           display:function(data){
                     return data.record.address.province;
           }
},
firstName: {
        title: 'First Name',
        display:function(data){
                   return data.record.name.firstName;
        }
},
middleName: {
        title: 'Middle Name',
        display:function(data){
                  return data.record.name.middleName;
        }
},
lastName: {
        title: 'Last Name',
        display:function(data){
                  return data.record.name.lastName;
        }
},

........

来自此链接的灵感:https://github.com/hikalkan/jtable/issues/509