Reputation: 1331
I have been using search container in liferay to display data from a table. Works well!! here is a snippet of code:
<%
List<testapp> pendingApprovals = ActionClass.getPendingLeaveApplications();
%>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results total="<%= pendingApprovals.size() %>"
results="<%= ListUtil.subList(pendingApprovals , searchContainer.getStart(), searchContainer.getEnd()) %>" />
<liferay-ui:search-container-row keyProperty = "empId" modelVar="search"
className="com.test.mis.portal.model.testapp">
<liferay-ui:search-container-column-text name='Leave Duration' value = '<%=String.valueOf(search.getLeaveDuration())%>' href="" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator/>
</liferay-ui:search-container>
Using the above code I display data from the testapp table based on some condition. In the same code I would like to add a row and display data. The data for this row should come from another table. In short I want to display data using search container from two different database tables. Is it possible to do? My requirement is such that data comes from two different tables
EDITED SECTION WITH REQUIREMENT I have Employee table with some fields I have another table Leave with some Fields. empId is in Leave table which maps to Employee table.
I have a search container whicg displays the data from the Leave table only if the leave is pending I want to display only those fields from Employee table which match with Leave table and satisfy the above condition.
Upvotes: 2
Views: 5715
Reputation: 3133
Your problem here has 2 faces:
a) Retrieve results per Leave table, and filter out by employee ID and pending status. Then in each row, use the employeeId to get back again the Employee instance (by EmployeeLocalServiceUtil. getEmployee(empId)), then get Employye attributes, like employee Name etc. This will need to get your hands dirty on the jsp file
b) Create a custom Class (say EmployeePendingLeaves), and use it as the searchContainer's model class. Don't include it in your database model. Just create a function that scans the Employee and Leave tables, and creates a EmployeePendingLeaves instance for each result row. it should have a variable/attribute for each of the row's columns
Upvotes: 2
Reputation: 11698
There might be many ways, here I list a few which come to my mind readily:
one way is to modify the TestAppImpl
model generated through ServiceBuilder to include your dependency something like this:
public class TestAppImpl extends TestAppBaseImpl {
private List<OtherTableData> otherTableDataList;
private OtherTableData otherTableData;
// if want to retrieve a list of rows
public List<OtherTableData> getOtherTableDataList() {
// call a custom method created in the OtherTableDataLocalService
// testAppId variable is available to TestAppImpl
List<OtherTableData> otherDataList = OtherTableDataLocalServiceUtil.getOtherDataListByTestAppId(testAppId);
return otherDataList;
}
// if want to retrieve just one row
public OtherTableData getOtherTableData() {
// call a custom method created in the OtherTableDataLocalService
// testAppId variable is available to TestAppImpl
OtherTableData otherData = OtherTableDataLocalServiceUtil.getOtherDataByTestAppId(testAppId);
return otherData;
}
}
You would have to rebuild your service after the above change.
Now in the JSP you just can use:
<liferay-ui:search-container-column-text name='Other table data name' value='<%=search.getOtherTableData().getName() %>' href="" />
Or else if you don't want to change the TestAppImpl
then in the JSP you can use:
<liferay-ui:search-container-column-text>
<%
List<OtherTableData> otherDataList = OtherTableDataLocalServiceUtil.getOtherDataListByTestAppId(search.getTestAppId());
for (OtherTableData tempData : otherDataList) {
%>
<%=tempData.getName() %>
<%=tempData.getDescription() %>
<%=tempData.getData() %>
<%
}
%>
</liferay-ui:search-container-column-text>
A variation of point-2 above:
<liferay-ui:search-container-column-jsp
name="otherDataFetch"
path="/html/portlet/testApp/other_data.jsp"
/>
And in the other_data.jsp
we can have the following code:
<%
ResultRow row = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
TestApp search = (TestApp) row.getObject();
List<OtherTableData> otherDataList = OtherTableDataLocalServiceUtil.getOtherDataListByTestAppId(search.getTestAppId());
for (OtherTableData tempData : otherDataList) {
%>
<%=tempData.getName() %>
<%=tempData.getDescription() %>
<%=tempData.getData() %>
<%
}
%>
Hope this is what you were looking for or else atleast it may give you a hint to go forward.
Upvotes: 2