2015-04-20 66 views
0

我想要显示数据库中的数据,但不能重复相同的配置文件,只需将位置添加到现有的配置文件。通过上面的例子就很明显显示嵌套数据表而不重复原始数据

+-------------------+---------------+---------------+ 
| PROFILE   | LOCATION | STATUS  | 
+-------------------+---------------+---------------+ 
+-------------------+---------------+---------------+ 
| Admin   | Chief  | OK   | 
+-------------------+---------------+---------------+ 
| Director  | Supervision | OK   | 
+-------------------+---------------+---------------+ 
| Secretary  | Supervision | OK   | 
+-------------------+---------------+---------------+ 
|     | Admin  | OK   | 
|     | Chief  | OK   | 
| Chief-accessor | Director | OK   | 
|     | Secretary | OK   | 
|     | Supervision | OK   | 
+-------------------+---------------+---------------+ 

随着这段代码是我的数据表,我只能重复他们,但我不能将它们分组:

<rich:panel header="User location info" style="margin: 20px 0 !important;"> 
    <h:dataTable id="userLocationList" 
     value="#{userLocation.list()}" var="row" 
     styleClass="rich-table" 
     headerClass="rich-table-subheader rich-table-subheadercell rich-table-thead" 
     rowClasses="rich-table-row" 
     columnClasses="rich-table-cell,rich-table-cell,rich-table-cell"> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Profile" /> 
      </f:facet> 
      <center> 
       <h:outputText value="#{row.profile}" /> 
      </center> 
     </h:column> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Location" /> 
      </f:facet> 
      <h:outputText value="#{row.location}" /> 
     </h:column> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Status" /> 
      </f:facet> 
      <center> 
       <h:outputText value="#{row.profile.status ? 'OK' : 'Inactive'}" /> 
      </center> 
     </h:column> 
    </h:dataTable> 
</rich:panel> 

像这样:

+-------------------+---------------+---------------+ 
| PROFILE   | LOCATION | STATUS  | 
+-------------------+---------------+---------------+ 
+-------------------+---------------+---------------+ 
| Admin   | Chief  | OK   | 
+-------------------+---------------+---------------+ 
| Director  | Supervision | OK   | 
+-------------------+---------------+---------------+ 
| Secretary  | Supervision | OK   | 
+-------------------+---------------+---------------+ 
| Chief-accessor | Admin  | OK   | 
+-------------------+---------------+---------------+ 
| Chief-accessor | Chief  | OK   | 
+-------------------+---------------+---------------+ 
| Chief-accessor | Director | OK   | 
+-------------------+---------------+---------------+ 
| Chief-accessor | Secretary | OK   | 
+-------------------+---------------+---------------+ 
| Chief-accessor | Supervision | OK   | 
+-------------------+---------------+---------------+ 

方法list()UserLocation只是selecttable_user_location

select userLoc from UserLocation userLoc where userLoc.user.id = :userId

我试图按照这里StackOverflow的一些答案是说把一个数据表内的另一个数据表,但因为我可能是重复的所有位置,而组配置文件,例如:

+-------------------+---------------+---------------+ 
| PROFILE   | LOCATION | STATUS  | 
+-------------------+---------------+---------------+ 
+-------------------+---------------+---------------+ 
|     | Chief  |    | 
|     | Supervision |    | 
|     | Supervision |    | 
|     | Admin  |    | 
| Admin   | Chief  | OK   | 
|     | Director |    | 
|     | Secretary |    | 
|     | Supervision |    | 
+-------------------+---------------+---------------+ 
|     | Chief  |    | 
|     | Supervision |    | 
|     | Supervision |    | 
|     | Admin  |    | 
| Director  | Chief  | OK   | 
|     | Director |    | 
|     | Secretary |    | 
|     | Supervision |    | 
+-------------------+---------------+---------------+ 
|     | Chief  |    | 
|     | Supervision |    | 
|     | Supervision |    | 
|     | Admin  |    | 
| Secretary  | Chief  | OK   | 
|     | Director |    | 
|     | Secretary |    | 
|     | Supervision |    | 
+-------------------+---------------+---------------+ 
|     | Chief  |    | 
|     | Supervision |    | 
|     | Supervision |    | 
|     | Admin  |    | 
| Chief-accessor | Chief  | OK   | 
|     | Director |    | 
|     | Secretary |    | 
|     | Supervision |    | 
+-------------------+---------------+---------------+ 
|     | Chief  |    | 
|     | Supervision |    | 
|     | Supervision |    | 
|     | Admin  |    | 
| Chief-accessor | Chief  | OK   | 
|     | Director |    | 
|     | Secretary |    | 
|     | Supervision |    | 
+-------------------+---------------+---------------+ 
And so it goes... 

我的疑问是我正在做的通用查询,所以表中的重复内容。有没有人有这个操作的想法?

相关的问题:

回答

0

至于我是不是能够做到这一点,他们返回相同的配置文件的所有位置的方式,只用一个查询,我决定做不同的事情,我想用两个查询,一个没有重复配置文件(select distinct ...)和一个根据配置文件为每个位置。只有意识到(纠正我,如果我错了)属性h:datatable按列而不是行呈现。因此,部分原因是,第一个配置文件列全部返回而没有重复配置文件,但是每个单元格中的位置都是相同的,如果该位置显示的单元格是给定配置文件,则不显示所有用户位置。那时,我意识到我非常专注于将重点放在用户配置文件上的位置。

运行select distinct配置文件,并创建一个方法来返回创建表时配置文件的位置。正如所报告的问题一样,datatable在另一个datatable内。

终于拿到我的代码:

<rich:panel header="Informations" style="margin: 20px 0 !important;"> 
    <h:dataTable id="userLocationList" 
     value="#{userLocation.list()}" var="row" 
     styleClass="rich-table" 
     headerClass="rich-table-subheader rich-table-subheadercell rich-table-thead" 
     rowClasses="rich-table-row" 
     columnClasses="rich-table-cell,rich-table-cell,rich-table-cell"> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Profile" /> 
      </f:facet> 
      <center> 
       <h:outputText value="#{row.name}" /> 
      </center> 
     </h:column> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Localizations" /> 
      </f:facet> 
       <h:dataTable id="uLoc" var="c" 
       value="#{home.getLocByProfile(row.idProfile)}"> 
        <h:column> 
         <h:outputText value="#{c}" /> 
        </h:column> 
       </h:dataTable> 
     </h:column> 
     <h:column> 
      <f:facet name="header"> 
       <h:outputText value="Status" /> 
      </f:facet> 
      <center> 
       <h:outputText value="#{row.active ? 'OK' : 'Inactive'}" /> 
      </center> 
     </h:column> 
    </h:dataTable> 
</rich:panel>