温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jquery easyui datagrid 显示外键/子属性/二层属性的值的方法

发布时间:2020-08-03 13:51:03 来源:网络 阅读:1967 作者:rireach 栏目:web开发

数据库中有个学生表student,我使用SSH2框架,通过名为findForPage的action将数据库表student的内容读出,并转为json字符串,输出到页面,然后通过datagrid,将列表显示出来

首先我们有个表格

<div region="center"  border="false">
  <table id="tt"></table>
  </div>

然后在javascript里

<script type="text/javascript">
$(function(){
$("#tt").datagrid({
url: "findForPage",
title:"学生基本信息管理",
frozenColumns : [ [ {
field : "ck",
checkbox : true
} ] ],
singleSelect : true,
iconCls : "icon-ok",
width : 800,
height : 300,
fit : false,
pageSize : 5,
pageList : [ 5, 10, 15, 20 ],
columns : [ [ {
field : "id",
title : "学号",
align : "center"
}, {
field : "name",
title : "姓名",
align : "center"
}, {
field : "classes",
title : "班级",
align : "center",
formatter:function(value,row,index){
return row.classes.classname;
}
} ] ],
toolbar : [
   {text:"添加", iconCls:"icon-add",handler:function(){openadd();}},
   "-",
       {text:"删除", iconCls:"icon-no",handler:function(){del();}},
   "-",
   {text:"修改", iconCls:"icon-edit",handler:function(){openedit();}}
],
pagination : true
});
});
</script>

主要看这几行

{
field : "classes",
title : "班级",
align : "center",
formatter:function(value,row,index){
return row.classes.classname;
}

这里的formatter:function(value,row,index)

之所以在这一列用formatter,是因为此列(classes)是外键列,在持久化类student中是Classes对象,我们要取的是此对象中的classname属性的值。也就是说,在这里是二层属性,或者说是子属性。

对于datagrid来说,子属性是不能直接读出的。即,如果你写field:"classes.classname",是无效的,这时就需要formatter

看后面function括号里的三个参数:value-字段值,row-行记录数据,index-行索引

看function里面我们用的是return row.classes.classname;通过这样读出了这个子属性classname

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI