I'm trying to show a 2D array as a table in a page. I know this sounds as if I was reinventing the wheel but the object I have to handle is a 2D array of a custom type that must also be rendered in a particular way (always the same).
The tricky part is that this 2D array can have null values in some of its indexes. In those cases, a particular "unavailable" cell must be rendered. The table structure (yes... <table>
<tr>
and <td>
) was already defined by the Design Team and accepted by the client.
I've tried to use <c:forEach/>
but got in trouble trying to make it work, because of the order in which JSTL and JSF tags are managed. When the JSF tags are handled there are some issues that include outdated values and missing components.
The array (attribute of a @ViewScoped
bean) is always null when <c:forEach/>
is invoked, even when I force the creation of the array:
public MyObject[][] getMatrix() {
if(loadedMatrix == null)
initializeMatrix();
return loadedMatrix.getTable();
}
The initializeMatrix()
method obtains the corresponding data from the database and invokes the logic that creates the 2D array, making loadedMatrix
reference it once it's created (all of this works, no exceptions or errors). When initializeMatrix
finishes, loadedMatrix
is still null
.
I went for the nested <c:forEach/>
option because I need to manage the indexes of the table to know what to render (if the object is null, has an availability flag set to false or if it can be rendered normally), but as for now I think the safest solution is to create a custom component.
My question is: What alternatives do I have to render the content of a 2D array as a table while being aware of the indexes I'm rendering?.
Thanks in advance