With a couple of minor adjustments to the code shown in a previous post, I now have a script that will fix the display of comboboxes, radio button groups, checkbox groups (single or multi-valued), and listboxes (single or multi-valued) all in one fell swoop!
The output of a single-value for any of these field types looks like this:
<table id="id"> <tr> <td>value </td> </tr> </table>
However, if you have a field with multiple values, it separates each value out into its own row, so the previous code needed to be adjusted to read the values out of all cells in the nested table. This code will concatenate multiple values to be separated by a comma and a space, but you can easily change that by modifying the code below.
<table id="id"> <tr> <td>value 1</td> </tr> <tr> <td>value 2</td> </tr> <tr> <td>value 3</td> </tr> ...and so on... </table>
This code, when run onClientLoad of the page, will fix all of these fields types, by replacing the table with just the value of the field, concatenating multiple values as needed.
if("#{javascript:document1.isEditable()}" == "false"){ dojo.query('table td table[id]').forEach(function(node) { var value = ''; // Get the value out of all cells in the nested table and concatenate them dojo.query('td', node).forEach(function(innerNode) { value += innerNode.innerHTML + ', '; }); // Replace the table with only the value(s) (or a blank), but cut off the trailing comma node.outerHTML = value.substring(0, value.length-2); }); }
Line 1 tells it to only run in read mode. (Update the name of the document data source variable if you need to.)
Line 3 gets a handle on tables with ids that are nested inside of table cells.
Lines 7-9 search for all table cells within the table and retrieve each cell’s innerHTML, which contains a value. If there is no value, then there won’t be a table cell and it will replace the table with an empty string.
Line 12 replaces the nested with just the value that needs to be displayed.
And now the output from the example shown above looks like this, as it should:
If you run into conflicts with this affecting other tables in your page, you may need to refine the selector in line 3.
If you need to preserve the of the element and/or wrap it in a span tag like other field values, you can tweak the code accordingly.
XSnippet
The latest version of the code is posted as an XSnippet on OpenNTF
