简体   繁体   English

在Liferay搜索容器中显示来自不同数据库表的数据:Liferay

[英]Display data in Liferay Search Container from different database tables : Liferay

I have been using search container in liferay to display data from a table. 我一直在liferay中使用搜索容器来显示表中的数据。 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. 使用上面的代码,我会根据某些条件显示来自testapp表的数据。 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. 有要求的已编辑部分我有带某些字段的Employee表我有另一个带某些字段的表。 empId is in Leave table which maps to Employee table. empId位于“离开”表中,该表映射到“雇员”表。

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. 我有一个搜索容器,其中只有当休假待处理时才显示请假表中的数据。我只想显示员工表中与请假表匹配并满足上述条件的那些字段。

There might be many ways, here I list a few which come to my mind readily: 可能有很多方法,在这里我列出了一些容易想到的方法:

  1. one way is to modify the TestAppImpl model generated through ServiceBuilder to include your dependency something like this: 一种方法是修改通过ServiceBuilder生成的TestAppImpl模型,以包括如下所示的依赖项:

     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: 现在,在JSP中,您可以使用:

     <liferay-ui:search-container-column-text name='Other table data name' value='<%=search.getOtherTableData().getName() %>' href="" /> 
  2. Or else if you don't want to change the TestAppImpl then in the JSP you can use: 否则,如果您不想更改TestAppImpl则可以在JSP中使用:

     <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> 
  3. 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: other_data.jsp我们可以包含以下代码:

     <% 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. 希望这是您正在寻找的东西,或者至少可以为您提供前进的提示。

Your problem here has 2 faces: 您的问题在这里有2张面孔:

  1. Being able to retrieve Data from tables Leave and Employee, by using the employeeId Foreign key. 可以通过使用employeeId外键从表Leave和Employee检索数据。 You don't need a custom query for that, and it's a pretty trivial task, I'm just pointing it out. 您不需要为此的自定义查询,这是一个非常琐碎的任务,我只是指出来。
  2. Displaying in the search container, data that cannot be retrieved from one Data/ Table. 在搜索容器中显示无法从一个数据/表检索的数据。 As you can see, the 'liferay-ui:search-container-row' attribute named 'className' which ca take the valu of one Class name. 如您所见,名为“ className”的“ liferay-ui:search-container-row”属性可以采用一个类名的值。 About this, I can see 2 approaches: 关于这一点,我可以看到两种方法:

a) Retrieve results per Leave table, and filter out by employee ID and pending status. a)根据请假表检索结果,并按员工ID和待处理状态进行筛选。 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 然后在每一行中,使用employeeId再次获取Employee实例(通过EmployeeLocalServiceUtil。getEmployee(empId)),然后获取Employye属性,例如employee Name等。这将使您的手在jsp文件上变脏

b) Create a custom Class (say EmployeePendingLeaves), and use it as the searchContainer's model class. b)创建一个自定义类(例如EmployeePendingLeaves),并将其用作searchContainer的模型类。 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. 只需创建一个扫描Employee和Leave表的函数,然后为每个结果行创建一个EmployeePendingLeaves实例即可。 it should have a variable/attribute for each of the row's columns 它应该为该行的每一列都有一个变量/属性

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

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