简体   繁体   中英

Conditional display with OGNl Expressions

In My struts project i have set of records in an array list,these recors have its name and categoryid. and in second arraylist i have records for category(category_id, categoryname).

now i want to list the first list with the category name as subheadings like

ArrayList1:(nameDetailList)
NAME:               CATEGORYID
name1                    1
name2                    2
name3                    1
name4                    5

ArrayList2:(categoryList)
CategoryID          CategoryName
1                     Category-1
2                     Category-2 
3                     Category-3 
4                     Category-4 
5                     Category-5

I need these to be displayed as

Category-1 
    --name1
    --name3
Category-2 
    --name2 
Category-5 
    --name3 

Note: Here i don't want to display the category names that don't have records associated for that. for this i coded below.

<s:iterator id="catIter"  value="categoryList">
       <s:property value="categoryName"/>
  <s:iterator value="nameDetailList.{ ?this.categoryId==#catIter.categoryId}">
 <s:property value="Name"/>
</s:iterator>
 </s:iterator>

it displaying the categories those don't have records asssociated with those also.can anyone tell how to control the display of category name. or is there any other better alternatives for this.

You can do it by counting the number of nameDetail that is related to category. If a category does not have any nameDetail related, it is not gonna print its name.

If you have a method that returns the nameDetails by categoryId, you can do it without any count. I don't know if there is a better solution for your case.

<s:iterator value="categoryList" var="category">
   <s:set name="counter" value="0"/>

   <s:iterator value="nameDetailList" var="nameDetail">
      <s:if test="#category.categoryId == #nameDetail.categoryId">
          <s:set name="counter" value ="%{#counter + 1}"/>
      </s:if>
   </s:iterator>

   <s:if test="#counter > 0">
     <s:property value="#category.categoryName"/>

     <s:iterator value="nameDetailList" var="nameDetail">
        <s:if test="#category.categoryId == #nameDetail.categoryId">
             <s:property value="#nameDetail.name"/>
        </s:if>       
     </s:iterator>

   </s:if>


</s:iterator>

i have updated my codes to

<s:iterator id="catIter"  value="categoryList" status="catstatu">      
  <s:iterator value="nameDetailList.{ ?this.categoryId==#catIter.categoryId}">
     <s:if test="#catstatus.first==true">
          <s:property value="categoryName"/>
     </s:if>
    <s:property value="Name"/>
  </s:iterator>
</s:iterator>

Now i'm getting the desired output. but is solution is the best one??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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