JSTL 迭代列表创建字段集

发布于 2025-01-05 02:24:24 字数 5298 浏览 0 评论 0 原文

大家好,我目前正在遍历一个列表,并根据字段集是否符合特定条件对字段集进行硬编码。如何编写一个循环,根据盖子中的所有组名创建一个字段集,并使用与该组名动态配合的所有相应的显示名称填充该字段集。我目前对此进行了硬编码。

<%@ 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>

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 <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);"/>
                
            <input type="button" name="clear_choice"  value="Uncheck All" onclick="checkUncheck(false);"/>

            </td>
            </tr>

        </td>
        </tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2025-01-12 02:24:24

我建议将您的数据结构从 List 更改为 Map> ,其中组名称是映射键。

下面的示例展示了如何转换它:

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);

(当然,您也可以在创建原始 detFields 列表时更改数据结构)

您可能还想维护所有标头名称的映射

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

request.setAttribute("detFieldHeaders", detFieldHeaders);

(最好在 web 应用程序启动时创建一次并将其存储在应用程序范围内,您可以为此使用 ServletContextListener

您可以使用单个嵌套Map 上的每次迭代都会给出 Map.Entry 后面又具有 getKey()getValue() 方法。键是 String 组名称,值是 List

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文