简体   繁体   English

jsp中的从属下拉列表

[英]Dependent Drop Down List in jsp

I am having two dropdown in my JSP. 我的JSP中有两个下拉菜单。 One is for topcategories and the other is for subCategories. 一个用于顶级类别,另一个用于子类别。 When my JSP page initially loads it will contain only the topcategories list in first drop down box and the second drop down will be empty. 当我的JSP页面最初加载时,它的第一个下拉框中将仅包含topcategories列表,第二个下拉列表将为空。 When I select topcategory from first dropdown its corresponding subcategories should be populated in the second dropdown. 当我从第一个下拉列表中选择topcategory时,其相应的子类别应填充在第二个下拉列表中。 During my initial load of jsp itself I will get the databean which conists of both topcategory and subcategory list. 在最初加载jsp本身的过程中,我将获得由topcategory和subcategory列表组成的databean。 So when the topcategory is selected i should get the value of topcategory selected and I have to compare it with the topcategory list present in databean and need to populate the corresponding subcategories in the second drop down. 因此,当选择topcategory时,我应该获得选择的topcategory的值,并且我必须将其与databean中存在的topcategory列表进行比较,并需要在第二个下拉列表中填充相应的子类别。 How to do this plz help me. 如何做到这一点请帮助我。 Thanks in advance. 提前致谢。 I have shared my code for reference. 我已经分享了我的代码以供参考。

<div class="selectbox01">
                <select name="make" id="make" onchange="loadModel()">

                    <c:forEach var="topCategory" items="${catalog.topCategories}" varStatus="status">
                    <option selected="selected"></option>
                    <option value="${topCategory.categoryId}"><c:out value="${topCategory.description.name}"/></option>
                    </c:forEach>
                </select>
            </div>
            <c:set var="make" value="${WCParam.make}"/>
            <div class="selectbox01">
                <select name="model" id="model">
                <option selected="selected"></option>
                <c:forEach var="topCategory" items="${catalog.topCategories}" varStatus="status">
                <c:if test="${topCategory.categoryId == make}">
                <c:forEach var="subTopCategory" items="${topCategory.subCategories}" varStatus="status2"> 
                    <option value="${subTopCategory.categoryId}">
<c:out value="${subTopCategory.description.name}"/></option>
                   </c:forEach>
                   </c:if>
                     </c:forEach>

                </select>
            </div>

One way to do it is to make a javascript object representing "catalog" bean.Then write a onchange handler bound to the topcategories dropdown which uses "catalog" object in javascript to get the javascript array of subcatagories. 一种方法是制作一个表示“ catalog” bean的javascript对象。然后编写一个绑定到topcategories下拉列表的onchange处理程序,该处理程序使用javascript中的“ catalog”对象来获取子类别的javascript数组。

Breaking this down to steps: 分解为以下步骤:

Step 1: Make a javascript object representing "catalog" bean: An easy way to do this is to override toString() method of Catalog bean and return a JSON object representing the bean. 步骤1:制作一个表示“ catalog” bean的javascript对象:一种简单的方法是重写Catalog bean的toString()方法并返回一个代表该bean的JSON对象。 OR Use Jackson api to convert Catalog object to JSON String. 或使用Jackson api将Catalog对象转换为JSON字符串。 After you have done it hold the json in a javascript variable. 完成后,将json保留在javascript变量中。 eg in your jsp write this: 例如,在您的jsp中编写以下代码:

<script type="text/javascript"> var catalogJson = ${catalog};</script>

Step 2: Write loadModel function in javascript which uses "catalogJson" variable to get the subcatagories and populate them in the other dropdown. 步骤2:在javascript中编写loadModel函数,该函数使用“ catalogJson”变量来获取子目录并在另一个下拉列表中填充子目录。

I have eleminated some of the details but most of it is straightforward I guess. 我已经消除了一些细节,但是我想其中大部分都是简单明了的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM