Fórum
Multiple Dynamic drop down list
Ranen Das, modificado 11 Anos atrás.
Multiple Dynamic drop down list
Regular Member Postagens: 137 Data de Entrada: 06/04/11 Postagens Recentes
Here is a code snippet for Multiple dynamic drop down list- main funda is to use, Liferay Json object and Json array with ResourceRequest/ResourceResponse.
In these code - once user select a country, user will get dyunamic data in state, channel(orgz) and another one drop down called Distributor list, its plain simple java code with Jquery. Thanks
In these code - once user select a country, user will get dyunamic data in state, channel(orgz) and another one drop down called Distributor list, its plain simple java code with Jquery. Thanks
Step 1 :-
<select name="state" id="selectStates">
<%
List stateList = (List)request.getAttribute("stateList");
if(stateList!=null){
for(Region stateTemp:stateList){
%>
<option id="stateName" value="<%= String.valueOf(savedState.getRegionId())%>" selected><%=savedState.getName()%></option>
<option id="stateName" value="<%= String.valueOf(stateTemp.getRegionId())%>"><%=stateTemp.getName()%></option>
<%}} %>
</select>
<select id="selectCountry" name="country">
<option value=""></option>
<%
List activeCountryList = (List)request.getAttribute("activeCountryList");
long selectedCountryId = 0;
if(addressToDisplay != null)
selectedCountryId = addressToDisplay.getCountry().getCountryId();
if(activeCountryList!=null){
for(Country activeCountry : activeCountryList){
%>
<option id="countryName" value="<%= String.valueOf(activeCountry.getCountryId())%>" selected><%=activeCountry.getName()%></option>
<option id="countryName" value="<%= String.valueOf(activeCountry.getCountryId())%>"><%=activeCountry.getName()%></option>
<% }
}%>
</select>
<select name="selectChannel" id="selectChannels">
<%
List channelList = (List)request.getAttribute("channelList");
if(channelList != null){
String companyChannel = (String)request.getAttribute("channel");
for(Organization selectChannel : channelList){
%>
<option id="selectChannel" value="<%= String.valueOf(selectChannel.getOrganizationId()) %>" selected><%= OnePRMOrganizationServiceUtil.getProgramDisplayName(selectChannel) %></option>
<option id="selectChannel" value="<%= String.valueOf(selectChannel.getOrganizationId()) %>"><%= OnePRMOrganizationServiceUtil.getProgramDisplayName(selectChannel) %></option>
<% }}%>
</select>
<select label="" name="distributors" id="selectDistributors" multiple length="5">
<%
List distributorList = (List)request.getAttribute("distributorList");
List existingDistributorList = (List)request.getAttribute("existingDistributorList");
if(distributorList!=null){
for(Distributor distributor : distributorList){
String distributorId = String.valueOf(distributor.getDistributorId());
%>
<option value="<%=distributorId%>" selected="true"><%= distributor.getName()%> </option>
<option value="<%=distributorId%>"><%= distributor.getName()%> </option>
<%}}%>
</select>
Step 2:-
jQuery("#selectCountry").change(function() {
jQuery('#selectStates').empty();
jQuery('#selectChannels').empty();
jQuery('#selectDistributors').empty();
var country = jQuery("#selectCountry").val();
var url = '<portlet:resourceurl id="getChannelAndStateList"></portlet:resourceurl>';
jQuery.post(url,{countryName : country},function(data) {
for(i=0;i<data.jsonstateslist.length;i++){ jsonstateslist="data.jsonStatesList[i].split(":");" jquery("#selectstates").append("<option value=""+ jsonStatesList[0] +"">"+jsonStatesList[1]+"");
}
for(i=0;i<data.jsonchannelslist.length;i++){ jsonchannelslist="data.jsonChannelsList[i].split(":");" jquery("#selectchannels").append("<option value="">"+""+"<option value=""+ jsonChannelsList[0] +"">"+jsonChannelsList[1]+"</option>");
}
for(i=0;i<data.jsondistributorslist.length;i++){ jsondistributorslist="data.jsonDistributorsList[i].split(":");" jquery("#selectdistributors").append("<option value=""+ jsonDistributorsList[0] +"">"+jsonDistributorsList[1]+"");
}
});
});
Step 3:-
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String jsp = null;
response.setContentType("text/html");
String resourceId = request.getResourceID();
if(Validator.isNotNull(resourceId) && resourceId.length() != 0 && resourceId.equalsIgnoreCase("getChannelAndStateList")) {
String countryId = request.getParameter("countryName");
List<region> stateList = null;
List<organization> channelList = null;
List<distributor> distributorList = null;
JSONObject jsonFeed = JSONFactoryUtil.createJSONObject();
if(countryId!=null && countryId.isEmpty() !=true){
stateList = getRegions(countryId);
distributorList = getDistributorByCountryAndActive(Long.valueOf(countryId));
try {
channelList = getChannelsForCountry(themeDisplay, countryId);
} catch (Exception e) {
e.printStackTrace();
log.error("There is some error in getChannelsForCountry = "+ e);
}
}
JSONArray jsonStatesList = JSONFactoryUtil.getJSONFactory().createJSONArray();
JSONArray jsonDistributorsList = JSONFactoryUtil.getJSONFactory().createJSONArray();
JSONArray jsonChannelsList = JSONFactoryUtil.getJSONFactory().createJSONArray();
if(stateList!=null){
for(Region region:stateList){
if(jsonStatesList!=null)
jsonStatesList.put(region.getRegionId()+ ":"+ region.getName());
}
}
if(distributorList!=null){
for(Distributor distributor:distributorList){
if(jsonDistributorsList!=null)
jsonDistributorsList.put(distributor.getDistributorId()+ ":"+ distributor.getName());
}
}
if(channelList!=null){
for(Organization channel:channelList){
if(jsonChannelsList!=null)
//jsonChannelsList.put(channel.getOrganizationId()+ ":"+ channel.getName());
jsonChannelsList.put(channel.getOrganizationId()+ ":"+ OnePRMOrganizationServiceUtil.getProgramDisplayName
(channel));
}
}
jsonFeed.put("jsonStatesList", jsonStatesList);
jsonFeed.put("jsonDistributorsList", jsonDistributorsList);
jsonFeed.put("jsonChannelsList", jsonChannelsList);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonFeed.toString());
}
</distributor></organization></region></data.jsondistributorslist.length;i++){></data.jsonchannelslist.length;i++){></data.jsonstateslist.length;i++){>