2013-04-23 66 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