數據庫中有個學生表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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。