Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Seeya S Kudtarker
Drop down list with values from database shows empty
April 1, 2013 3:48 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

I want to display values in the dropdown list which come from the database. The code is as follows in the jsp:

<aui:select id="empName" name="empName">
<%
Employee employee;
Employee newEmployee = new EmployeeImpl();
int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
for(int i=0; i<totalEmployees;i++) {
%>
<aui:option name = "opt" value ='<%=String.valueOf(newEmployee.getEmpFname())%>'/>
System.out.println("newEmployee.getFname string value in loop: " +newEmployee.getEmpFname());
<% } %>
</aui:select>


It shows an a big empty list.

What should I do to get the values in the drop down list from the database?
mohammad azaruddin
RE: Drop down list with values from database shows empty
April 1, 2013 4:31 AM
Answer

mohammad azaruddin

Rank: Expert

Posts: 434

Join Date: September 17, 2012

Recent Posts

Hi
Replace your code with this

<aui:select id="empName" name="empName">
<%
int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
List<Employee> employeeList=EmployeeLocalServiceUtil.getEmployees(0,totalEmployees );
for(Employee emp:employeeList) {
%>
<aui:option value ="<%=emp.getEmpFname()%>"><%=emp.getEmpFname()%></aui:option>
System.out.println("newEmployee.getFname string value in loop: " +newEmployee.getEmpFname());
<% } %>
</aui:select>
Atin Agarwal
RE: Drop down list with values from database shows empty
April 1, 2013 4:31 AM
Answer

Atin Agarwal

Rank: Junior Member

Posts: 74

Join Date: February 19, 2012

Recent Posts

Hi Seeya,

First of all get a list of all employees from your table then iterate the loop over that list.

Something like---

<aui:select id="empName" name="empName">
<%
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
for(Employee emp : EmpList ) {
%>
<aui:option value ="<%= emp.getEmpFname() %>" >
<liferay-ui:message key="<%= emp.getEmpFname() %>" />
</aui:option>

<% } %>
</aui:select>

Hope it would help.

Regards,
Atin Agarwal
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 7:01 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

mohammad azaruddin:
Thanks a lot for your timely help you give whenever my code stops running. emoticon

Regards

Seeya
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 7:05 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Hey Atin Agarwal,

Thanks for your help!

By the way, if you implement the line which you gave me:
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
with
List<Employee> employeeList=EmployeeLocalServiceUtil.getEmployees(0,totalEmployees );
It works!! emoticon Try it out..

Regards

Seeya
David H Nebinger
RE: Drop down list with values from database shows empty
April 1, 2013 7:12 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6290

Join Date: September 1, 2006

Recent Posts

Atin's suggested implementation is better since you don't have to evaluate a database query to get the number of employees before retrieving the list of employees.
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 7:20 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

mohammad azaruddin

There is one more query I have
When I try to use the concatenate two names, I get an error at the line:

<aui:option value ='<%=emp.getEmpFname()%> + <%=emp.getEmpLname()%>'><%=emp.getEmpFname()%><%=emp.getEmpLname()%></aui:option>
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 7:23 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Hey David,

Thanks!! I didn't notice it.
Thanks for the prompt reply.

In that case, i will go with Atin's suggestion.

Regards

Seeya
David H Nebinger
RE: Drop down list with values from database shows empty
April 1, 2013 7:30 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6290

Join Date: September 1, 2006

Recent Posts

Seeya S Kudtarker:
<aui:option value ='<%=emp.getEmpFname()%> + <%=emp.getEmpLname()%>'><%=emp.getEmpFname()%><%=emp.getEmpLname()%></aui:option>


This is not proper syntax. Try:

<aui:option value='<%=emp.getEmpFname() +" " + emp.getEmpLname()%>'><%=emp.getEmpFname()%><%=emp.getEmpLname()%></aui:option>
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 7:50 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

David,
Thanks a lot!!
I had created a total mess of the syntax.

Thanks for the help!! It is working properly now!

Regards

Seeya
Seeya S Kudtarker
RE: Drop down list with values from database shows empty
April 1, 2013 8:01 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Atin,
I am using your suggestion. thanks for the help!!
Like David told me your code is better since I dont have to take the count of number of entries in the database:
I am referring to the line u suggested:
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
It actually makes everything simple.

Regards

Seeya
mohammad azaruddin
RE: Drop down list with values from database shows empty
April 1, 2013 9:33 AM
Answer

mohammad azaruddin

Rank: Expert

Posts: 434

Join Date: September 17, 2012

Recent Posts

Hi david

List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
i was using this method too.but sometime this may return null value(I'm not sure about it ,correct me if i'm wrong) or that problem might occured due to some other errror in code..
David H Nebinger
RE: Drop down list with values from database shows empty
April 1, 2013 10:06 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6290

Join Date: September 1, 2006

Recent Posts

mohammad azaruddin:
Hi david

List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);

i was using this method too.but sometime this may return null value(I'm not sure about it ,correct me if i'm wrong) or that problem might occured due to some other errror in code..


You will get null back if you have no entities. You should be aware of what the database may contain as to whether you need to check for null or not.

Personally I would have coded it like:

 1<aui:select id="empName" name="empName">
 2<%
 3List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
 4
 5if ((EmpList != null) && (! EmpList.isEmpty())) {
 6    for(Employee emp : EmpList ) {
 7%>
 8         <aui:option  value ="<%= emp.getEmpFname() %>" >
 9                  <liferay-ui:message key="<%= emp.getEmpFname() %>" />
10          </aui:option>
11
12<% }
13}
14%>
15</aui:select>
mohammad azaruddin
RE: Drop down list with values from database shows empty
April 1, 2013 10:33 PM
Answer

mohammad azaruddin

Rank: Expert

Posts: 434

Join Date: September 17, 2012

Recent Posts

Thank you Davidemoticon