I've had a couple of projects where it's a requirement for people to
be able to show/hide and reorder columns. So, the column model is
held in the database and the table is rendered via a tag. This way I
can also add in extra columns such as a checkbox per row without any
extra effort:
TableColumns.java
public User user;
public String viewId;
public String tableId;
@OrderBy("position asc")
public List<TableColumn> tableColumns;
TableColumn.java
public String key;
public int position;
public boolean visible;
Employees.java
public static void whatever()
{
...
List<TableColumn> tableColumns =
TableColumns.findByUserAndViewIdAndTableId(user, viewId, tableId);
List<Employee> employees = ...
render(tableColumns,
employees);
}
@Util
public static String getByColumnKey(Employee employee,
String columnKey)
{
// map column key to field of employee and return
}
whatever.html
#{table cols:tableColumns}
#{list items:employees, as:'employee'}
<tr>
#{list items:tableColumns, as:'tc'}
#{if tableColumn.visible}
<td>$
{controllers.Employees.getByColumnKey(employee, tc.key}</td>
#{/if}
#{/list}
</tr>
#{/list}
#{/table}
tags/table.html
<table>
<thead>
<tr>
<th><input type="checkbox" id="rowSelect"/></th>
#{list items:_cols, as:'tableColumn'}
#{if tableColumn.visible}
<th>&{tableColumn.key}</th>
#{/if}
#{/list}
</tr>
</thead>
#{doBody /}
</table>
While this initially looks like a much more complex way of doing
things, it actually provides a quite powerful mechanism for user
customisation along with a lot of code reuse. In the
Employees#getByColumnKey I automatically map the key to the entity
field using objectify-led (will be blogging on this later) and so that
method is tiny - if you do it manually, the method length grows in
length along with the number of fields in your entity. You can,
however, combine the two approaches in order to provide a data
feedback that's dissociated from the underlying model structure.
- Steve
> >
play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go
oglegroups.com>
> > .