easyUI dategrid field 用formatter顯示子屬性,easyuiformatter
dategrid:
<body class="easyui-layout" data-options="fit:true,border:false"><table id="dg" class="easyui-datagrid" data-options="fit:true,border:false"url="book/listBookForUser"toolbar="#toolbar" pagination="true"rownumbers="true" fitColumns="true" singleSelect="true"><thead><tr><th field="bookId" width="50">書ID</th><th field="bookName" width="50">書名</th><th field="bookType" width="50" formatter="formatBookType">書類型</th><th field="author" width="50">作者</th><th field="callNumber" width="50">索書號</th><th field="iSBN" width="50">ISBN</th><th field="publisher" width="50">出版社</th><th field="publishYear" width="50">出版年份</th><th field="series" width="50">系列</th><th field="language" width="50">語言</th><th field="price" width="50">價格</th><th field="page" width="50">頁數</th></tr></thead></table>
這裡,book和bookType是多對一的關係,後台返回的json中,bookType又是一個json對象,要獲得bookType的bookTypeName,不能直接用bookType.bookTypeName .所以在field的bookType欄位加上formatter 。
function formatBookType(val,row,index){//alert(row.bookType);if(val==null){return "";}elsereturn row.bookType.bookTypeName;}
val是該欄位的值,即bookType。row是行值。這裡注意,要加一個if為空白的判斷,因為我有的書籍沒有設定類型,這樣的話就會找不到類型名,造成整個date都沒有顯示資料了。