Fórum

Dynamic Select within liferay

xun ren, modificado 15 Anos atrás.

Dynamic Select within liferay

Junior Member Postagens: 81 Data de Entrada: 01/04/08 Postagens Recentes
Hi everyone, I want to create a portlet which contains a function to select the city and county dynamicly. In fact, liferay has created one which is used to choose the country and region information. What I want is more or less the same. It's javascript code looks like this:



<script type="text/javascript">
document.<portlet:namespace />fm.<portlet:namespace />street1.focus();

function <portlet:namespace />selectCountryPost() {
setSelectedValue(document.<portlet:namespace />fm.<portlet:namespace />regionId, "<%= regionId %>");
}

DynamicSelect.create(
"<%= themeDisplay.getPathMain() %>/portal/json_regions",
document.<portlet:namespace />fm.<portlet:namespace />countryId,
document.<portlet:namespace />fm.<portlet:namespace />regionId,
<portlet:namespace />selectCountryPost
)
</script>



However, I do not know what does "/portal/json_regions" directs to. So I can not create my own function. Can anyone help me find it out?
The code above is located at: portal-web/docroot/html/portlet/enterprise_admin/edit_address.jsp
xun ren, modificado 15 Anos atrás.

RE: Dynamic Select within liferay

Junior Member Postagens: 81 Data de Entrada: 01/04/08 Postagens Recentes
Nobody can help me?

Come on, please! Just give me a tip!
Ed F., modificado 15 Anos atrás.

RE: Dynamic Select within liferay

Expert Postagens: 280 Data de Entrada: 27/06/06 Postagens Recentes
sorry man, I tried to look into it, but it seems you're on an older version than mine. If you can't figure it out, you don't have to use Liferay's method for doing dynamic selections. There are plenty of javascript methods for doing what I think you're trying to do.

I recommend a google search for javascript dynamic select. There's so many ways you can do it, and you can still do anything with javascript inside any portlet.
xun ren, modificado 15 Anos atrás.

RE: Dynamic Select within liferay

Junior Member Postagens: 81 Data de Entrada: 01/04/08 Postagens Recentes
Hi,
Thanks. I did it with my own ajax functions. It is very easy! I just need a xmlHttpRequest object. Then I can do anything. It's great!
And I am using v4.4.2 Yes, it's a little old.
thumbnail
Helmi Mahara, modificado 15 Anos atrás.

RE: Dynamic Select within liferay

New Member Postagens: 13 Data de Entrada: 08/08/08 Postagens Recentes
Hi xun ren,
Don't you mind to show the code?

Thanks
xun ren, modificado 15 Anos atrás.

RE: Dynamic Select within liferay

Junior Member Postagens: 81 Data de Entrada: 01/04/08 Postagens Recentes
Hi,
Sorry for replying so late. Because I was busing on Beijing Olympic Games. Of course I can show you the code.
There are not well organized. It is better to separate them in different files.


This is the js codes:



function changeCity(provinceId)
{
var url="/html/portlet/ext/ajaxServer/dynamicSelect_City.jsp";
url=url+"?provinceId="+provinceId;
var controlId = document.<portlet:namespace />fm.<portlet:namespace />cityId;
init(url, controlId);
clearList(document.<portlet:namespace />fm.<portlet:namespace />areaId);
}

function changeArea(cityId)
{
var url="/html/portlet/ext/ajaxServer/dynamicSelect_Area.jsp";
url=url+"?cityId="+cityId;
var controlId = document.<portlet:namespace />fm.<portlet:namespace />areaId;
init(url, controlId);
}

function init(url, controlId)
{
xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)
{
alert ("Your browser doesn't supoort AJAX!");
return;
}

xmlHttp.onreadystatechange=function()
{
stateChanged(controlId);
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function GetXmlHttpObject()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

function stateChanged(controlId)
{
if (xmlHttp.readyState==4)
{
updateList(controlId);
}
}

function clearList(listId)
{
var i;
for(i=listId.options.length-1;i>=0;i--)
{
listId.remove(i);
}
}

function updateList(listId)
{
clearList(listId); //clear the list first
var str = xmlHttp.responseText.split("#");
var subStr;
var option = null;
for(i=0;i<str.length-1;i++)
{
subStr = str.split("*");
var option = document.createElement("OPTION");
option.text = subStr[1];
option.value = subStr[0];
listId.options.add(option);
}
}




And these methods will be triggered once the drop-down list is changed. The code will be like this:


<select id="<portlet:namespace />provinceId" name="<portlet:namespace />provinceId" onchange="changeCity(this.value);">

<table>
<tr>
<td>
<select id="<portlet:namespace />provinceId" name="<portlet:namespace />provinceId" onchange="changeCity(this.value);">
<option value=""><%=LanguageUtil.get(pageContext, "choose-province")%></option>

<%
List provinces = ProvinceLocalServiceUtil.getProvinces();

for (int i = 0; i < provinces.size(); i++) {
Province province = (Province)provinces.get(i);
%>

<option value="<%= province.getProvinceId() %>"><%= LanguageUtil.get(pageContext, province.getProvinceName()) %></option>

<%
}
%>
</select>
</td>
<td style="padding-left: 30px;"></td>
<td>
<select id="<portlet:namespace />cityId" name="<portlet:namespace />cityId" onchange="changeArea(this.value);">
<option value=""><%=LanguageUtil.get(pageContext, "choose-city")%></option>
</select>
</td>
<td style="padding-left: 30px;"></td>
<td>
<select id="<portlet:namespace />areaId" name="<portlet:namespace />areaId">
<option value=""><%=LanguageUtil.get(pageContext, "choose-county")%></option>
</select>
</td>
</tr>
</table>






and in /html/portlet/ext/ajaxServer/dynamicSelect_City.jsp, you can do what you want, and do not forget to return the corrected text. I can show you below:



<%
String cityName = null;
String cityId;
City city = null;
String message = " " + "*" + " " + "#";
String provinceId = request.getParameter("provinceId");
if(!("".equals(provinceId) || provinceId == null)){
List cities = CityLocalServiceUtil.getCities(provinceId);
for(int i=0; i < cities.size(); i++){
city = (City)cities.get(i);
cityName = city.getCityName();
cityId = city.getCityId();
message += cityId + "*" + cityName + "#";
}
}
out.println(message);
%>

Prathibha h m, modificado 14 Anos atrás.

RE: Dynamic Select within liferay

Junior Member Postagens: 74 Data de Entrada: 17/09/09 Postagens Recentes
Hi,

For Dynamic Select. For ex : if we have country and state. If the there is no state for that country how can we check the condition so that the state combo(select box) doesnt appear.
Please help me I out. I am trying but failed.
thumbnail
Anil singh Gurjar, modificado 12 Anos atrás.

RE: Dynamic Select within liferay

New Member Postagens: 18 Data de Entrada: 31/10/11 Postagens Recentes
Hello Sir

I am anil I am new in liferay .I have used your code for dynamic selection of country and city . but it is not working .
can u help about this or can send that code or portlet plz


my mail id is anilgurjar86@gmail.com


thanks in advance
thumbnail
Anil singh Gurjar, modificado 12 Anos atrás.

RE: Dynamic Select within liferay

New Member Postagens: 18 Data de Entrada: 31/10/11 Postagens Recentes
Hi

xun ren, sir i am new in liferay i have urgent need to this code can u help plz share your code.

thanks
thumbnail
chris chris chris, modificado 15 Anos atrás.

RE: Dynamic Select within liferay

Regular Member Postagens: 179 Data de Entrada: 25/09/07 Postagens Recentes
Hi there,

you can use AjaxUtil from Liferay to select cities that belong to a country without refreshing the page.



xun ren:
Hi everyone, I want to create a portlet which contains a function to select the city and county dynamicly. In fact, liferay has created one which is used to choose the country and region information. What I want is more or less the same. It's javascript code looks like this:



<script type="text/javascript">
document.<portlet:namespace />fm.<portlet:namespace />street1.focus();

function <portlet:namespace />selectCountryPost() {
setSelectedValue(document.<portlet:namespace />fm.<portlet:namespace />regionId, "<%= regionId %>");
}

DynamicSelect.create(
"<%= themeDisplay.getPathMain() %>/portal/json_regions",
document.<portlet:namespace />fm.<portlet:namespace />countryId,
document.<portlet:namespace />fm.<portlet:namespace />regionId,
<portlet:namespace />selectCountryPost
)
</script>



However, I do not know what does "/portal/json_regions" directs to. So I can not create my own function. Can anyone help me find it out?
The code above is located at: portal-web/docroot/html/portlet/enterprise_admin/edit_address.jsp