Java – customize crud list in playframework html
Hello
My model is like this
I have an object related to other objects, as follows:
@Entity public class MyObjectA extends Model { @required public String myObjectAName; ... @required @ManyToOne public MyObjectB myObjectB; }
Now on the list I covered In HTML, I have this
<div id="crudListTable"> #{crud.table fields:['myObjectB','myObjectAName'] /} </div>
Now, call the following URL in the browser. http://myplayapp/admin/myObjectAs When executed, this code displays something similar
myObjectB __ myObjectAName
MyObjectB [1] __ Hey, this is myobjectaname's name myobjectb [2]__ Hey, that's another name for myobjectaname
Note that the object name and object ID in [] are used in my myobjectb
So to display the myobjectbname attribute of myobjectb in the above list, I'll try this:
<div id="crudListTable"> #{crud.table fields:['myObjectB.myObjectBName','myObjectAName'] /} </div>
But then I got the mistake
Of course, myobjectb has a public attribute myobjectbname
So what did I do wrong here?
Solution
It doesn't work because the fields parameter only displays a list of field names of the class to be displayed (here myobjecta), and "myobjectb. Myobjectbname" is not the name of the field in myobjecta
There are two ways to do this:
The simple solution is to override the toString () method of myobjectb The disadvantage of this is that the same string is used every time myobjectb is displayed, which may not be what you want
Example:
public class MyObjectB extends Model { // ... @Override public String toString(() { return this.myObjectBName; } }
The list specific solution is to use crud custom tag.
Example:
#{crud.table fields:['myObjectB','myObjectAName']} #{crud.custom 'myObjectB'} ${object.myObjectB.myObjectBName} #{/crud.custom} #{/crud.table}