2013-04-23 64 views
2

我一直在liferay中使用搜索容器来显示表中的数据。效果很好!! 这里是一个代码段:在不同数据库表中显示Liferay搜索容器中的数据:Liferay

<% 
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> 

使用上述代码我从基于一些条件testapp表显示数据。 在相同的代码中,我想添加一行并显示数据。这一行的数据应该来自另一个表格。简而言之,我想要使用来自两个不同数据库表的搜索容器显示数据。 可以做吗?我的要求是这样的,数据来自两个不同的表

编辑部分与要求 我有一些领域的员工表 我有另一张表离开一些字段。 empId位于映射到Employee表的离开表中。

我有一个搜索容器只显示来自离开表的数据 我只想显示Employee表中与离开表匹配并满足上述条件的那些字段。

+0

testApp的实体和'其他表'之间是否存在某种连接?像外键?是否每行只有一个“其他表”的匹配实例,或更多?你使用数据库的Service Builder吗?您的问题非常模糊,难以回答,请尝试使问题更加明确 – yannicuLar 2013-04-23 19:55:15

+0

@yannicuLar: 是的,我有外键。我很抱歉,因为我没有提到我的问题。 我有FK。 testApp具有testAppId,“otherTable”具有指向testApp表的testAppId。我已经使用了Service Builder。我只想显示FK匹配的那些数据条目。我认为这可能是不可能的,因为在搜索容器中我们提到了className中的实体。但如果有办法,请启发我。一种方法是使用我不知道的自定义查询。 – 2013-04-24 03:33:50

+0

只是一个澄清:你需要显示在每一行,从2个实体检索到的数据? – yannicuLar 2013-04-25 15:34:40

回答

2

您的问题有2张面孔:

  1. 能够从表中检索数据和离开的员工,利用雇员外键。你不需要一个自定义查询,这是一个非常简单的任务,我只是指出它。
  2. 在搜索容器中显示无法从一个数据/表中检索的数据。正如你所看到的,名为'className'的'liferay-ui:search-container-row'属性可以取得一个Class名字的值。关于这一点,我可以看到两种方法:

a)检索离开表的结果,并按员工ID和待处理状态过滤掉。然后在每一行中,使用employeeId来重新获取Employee实例(通过EmployeeLocalServiceUtil.getEmployee(empId)),然后获取Employye属性,如员工姓名等。这需要让您的手在jsp文件上变得脏兮兮

b)创建一个自定义的类(比如说EmployeePendingLeaves),并将其用作searchContainer的模型类。不要将其包含在数据库模型中。只需创建一个扫描Employee和Leave表的函数,并为每个结果行创建一个EmployeePendingLeaves实例。它应该对每一行的列有变量/属性

+0

我应用了你建议的第一种方法。谢谢yannicuLar它的工作! 但出于好奇,我想知道第二种方法。第二种方法中,您建议我使用自定义类作为searchContainder的模型类。所以从我理解的我应该在我的操作类中写入一个函数EmployeePendingLeaves并将其包含在className中。它是正确的吗? 顺便说一句我已upvoted你的答案,也接受:) – 2013-05-07 12:00:18

+0

不,在我的例子中,EmployeePendingLeaves是一个类,而不是一个函数。只是一个有3个变量的类,每个行的列都有一个变量。在你的动作类中,你只需创建这些对象并将它们返回给jsp。这可以在一个函数中完成,该函数扫描Employee和Leave条目的数据库,并为每个结果/行创建一个EmployeePendingLeaves实例。然后返回一个包含这些实例的数组 – yannicuLar 2013-05-08 02:34:37

+0

@ yannicuLar: 谢谢我将尝试这个练习。在此期间,你可以帮助我与http://stackoverflow.com/questions/16229369/get-images-from-liferay-document-library-whose-id-is-stored-in-separate-databse?noredirect=1# comment23567377_16229369 我想要你们对问题的EDITED部分的帮助.. – 2013-05-08 06:17:32

2

可能有很多方法,我在这里列出几个该来我的心很容易:

  1. 一个方法是修改通过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; 
        } 
    } 
    

    上述更改后,您将不得不重建您的服务。

    现在在JSP你只可以使用:

    <liferay-ui:search-container-column-text name='Other table data name' value='<%=search.getOtherTableData().getName() %>' href="" /> 
    
  2. 否则,如果你不想改变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. 一上述点-2的变化:

    <liferay-ui:search-container-column-jsp 
        name="otherDataFetch" 
        path="/html/portlet/testApp/other_data.jsp" 
    /> 
    

    而在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() %> 
    
    <% 
    } 
    %> 
    

希望这是你要找的人,否则ATLEAST它可能会给你一个提示前进。这里

+0

感谢您的解释。 我会尽力实施并让你知道在一个小时内:) 请关注此帖 – 2013-04-24 07:28:58

+0

这是行不通的。可能是我应该更详细地写下我的要求。检查我的问题中编辑的部分。编辑部分与要求 – 2013-04-24 09:00:15

+0

你能告诉我具体什么不工作?这个需求重新迭代你已经写过的东西,你刚刚给出了两个表'testapp = leaves'和'otherDataTable == Employee'的名字。同样在'fields'的'EDITED SECTION WITH REQUIREMENT'中,你是指这些表中的数据或列的行吗?另外我假设'Employee'和'Leave'是独立的模型对象。 – 2013-04-24 09:24:02