EasyUI dategrid field displays sub-attributes with 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"> book ID </th> <th field = "bookName" width = "50"> title </th> <th field = "bookType" width = "50" formatter = "formatBookType"> book type </th> <th field = "author" width = "50"> author </th> <th field = "callNumber" width = "50"> cable number </th> <th field = "iSBN" width = "50"> ISBN </th> <th field = "publisher" width = "50"> publisher </th> <th field = "publishYear" width = "50"> publication year </th> <th field =" series "width =" 50 "> series </th> <th field =" language "width =" 50 "> language </th> <th field =" price "width = "50"> price </th> <th field = "page" width = "50"> page number </th> </tr> </thead> </table>
Here, the relationship between book and bookType is many-to-one. In the json returned by the background, bookType is a json object. To obtain the bookTypeName of bookType, bookType cannot be used directly. bookTypeName. therefore, add formatter to the bookType field of the field.
function formatBookType(val,row,index){//alert(row.bookType);if(val==null){return "";}elsereturn row.bookType.bookTypeName;}
Val is the value of this field, that is, bookType. Row is the row value. Note that you need to add a null if judgment, because some books do not set the type, so the type name will not be found, resulting in no data display for the entire date.