0

Hello guys Im currently itearing over a list and im hard-coding fieldsets based on if they match a certain criteria. How can I write a loop that creates a fieldset based off all the groupnames in the lidst and populates that fieldset with all the corresponding displayNames that go along with that groupName dynamically. Im currently hardcoding this.

<%@ include file="../include/pre-header.html" %>

    <tr>
        <th>
            <span onclick="toggleDiv('displayFields', 'displayImg')" style="cursor: hand;">Data Fields&nbsp;<img name="displayImg" src="../images/minus.gif" /></span>
        </th>

    </tr>

    <tr>
        <td>

            <div id="displayFields" style="display:block;">
            <fieldset class="det">
                <legend>Header Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'HEADER_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                   </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Materiel Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'MATERIEL_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Planned Unit Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'PLANNED_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Actual Unit Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'ACTUAL_DATA'}">
                <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Planned Cost Data</legend>
                <c:forEach var="detBean" items="${detFields}">
                    <c:if test="${detBean.groupName == 'COST_DATA'}">
                        <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                    </c:if>
                </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Carry Over Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'CARRYOVER_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Schedule Exceptions</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'SCHEDULE_EXCEPTIONS'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>DIFMS data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'DIFMS_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>
            </div>
            <tr>
            <td style="text-align: center;">
            <input type="button" name="clear_choice"  value="Check All" onclick="checkUncheck(true);"/>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" name="clear_choice"  value="Uncheck All" onclick="checkUncheck(false);"/>

            </td>
            </tr>

        </td>
        </tr>
skaffman
  • 398,947
  • 96
  • 818
  • 769
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

1 Answers1

2

I'd suggest to change your datastructure from List<DetBean> to Map<String, List<DetBean>> where the group name is the map key.

The below example shows how to convert it:

Map<String, List<DetBean>> detFieldMap = new LinkedHashMap<String, List<DetBean>>();

for (DetBean detBean : detFields) {
    String groupName = detBean.getGroupName();
    List<DetBean> detBeans = detFieldMap.get(groupName);

    if (detBeans == null) {
        detBeans = new ArrayList<DetBean>();
        detFieldMap.put(groupName, detBeans);
    }

    detBeans.add(detBean);
}

request.setAttribute("detFieldMap", detFieldMap);

(you can of course also change the datastructure at the point where you're creating the original detFields list)

You probably also want to maintain a mapping of all header names

Map<String, String> detFieldHeaders = new HashMap<String, String>();
detFieldHeaderMap.put("HEADER_DATA", "Header Data");
detFieldHeaderMap.put("MATERIEL_DATA", "Materiel Data");
// ...

request.setAttribute("detFieldHeaders", detFieldHeaders);

(it would probably be better to create it once on webapp's startup and store in application scope, you can use a ServletContextListener for this)

This way you can use a single nested <c:forEach>. Every iteration over a Map gives Map.Entry back which in turn has getKey() and getValue() methods. The key is then the String group name and the value is then the List<DetBean>.

<c:forEach items="${detFieldMap}" var="detFieldEntry">
    <fieldset class="det">
        <legend>${detFieldHeaders[detFieldEntry.key]}</legend>
        <c:forEach items="${detFieldEntry.value}" var="detBean">
            <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
            <br/>
        </c:forEach>
    </fieldset>
</c:forEach>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That looks kinda slick BalusC.. Im about to give it a try – Doc Holiday Feb 13 '12 at 16:33
  • Hel BalusC. since you pretty much understand what Im doing. What if I wanted to Iterate over and display in order of the larger group fieldSets to the smallest. Meaning each group has x number of displayname attached to it. What if I wanted to put the groups with the most amount of fieldName at the beginning of the list when iterating over? – Doc Holiday Feb 13 '12 at 16:51
  • It's possible, but that get tricky because sortable maps are by default sorted by keys, not by values. You'd need to create a `HashMap` instead and when it's finished, then create another `TreeMap` with a `Comparator>>` which you construct around the already-created `HashMap`. – BalusC Feb 13 '12 at 17:04
  • Thats what I was thinking. I created a hashmap for the initial one you had made...and didnt worry about ` Map detFieldHeaders ' ...just changed the groupNames in the database – Doc Holiday Feb 13 '12 at 17:05
  • I remember ever having posted a similar answer in `[java]`, but couldn't find it right now. Let me look... Edit: ah, found a similar answer, check the `createSortedMap()` method here http://stackoverflow.com/questions/8855849/sorting-a-hashmap-while-keeping-duplicates/8856082#8856082 This does it a bit differently, using a `Comparable`, but it should give some insights. – BalusC Feb 13 '12 at 17:06