2014-03-24 35 views
1

条件我有填充表,并根据每个单元中值的类型的节目,我改变格式 - 像百分比,小数,分组等的if-else(嵌套),或者当JSTL

我有一个表是默认视图,我允许用户通过输入文档编号来过滤这些结果,然后表更改为添加2列 - 文档编号和相应的文档名称。

所以,如果检索到的列数是10,那么有些行为。如果是12,那么还有其他一些行为。

 
Date  | Documents Bought | Documents Started | Column 4 | Column 5 

03/24/2013 | 1000.0   | 2,000   | 1,500 | 2,500 

是默认视图的缩减版本。

 
Date  | Document ID| Document Name | Documents Bought | Documents Started | Column 4 | Column 5 

03/24/2013| 55  |  Waiver | 10    | 20    | 4  | 5 

我使用相同的Java程序来调用查询,并填充表。我在JSP中编写了以下内容。

<c:forEach var="row" items="${docFunnel}"> 
          <tr> 
           <c:forEach var="cell" items="${row}" 
        varStatus="rowIdx"> 

         <c:choose> 

         <c:when 
          test="${rowIdx.count==17}"> 

             </c:when> 
             <c:when 
          test="${rowIdx.index==2}"> 
              <td>${cell}</td> 
             </c:when> 

             <c:when 
          test="${rowIdx.index==1}"> 
              <td><fmt:formatNumber 
            type="number" maxFractionDigits="3" groupingUsed="false" 
            value="${cell}" /></td> 
             </c:when> 

             <c:otherwise> 
             <c:choose> 

             <c:when 
            test="${rowIdx.index==0}"> 
              <td>${cell}</td> 
             </c:when> 

             <c:otherwise> 

               <td> 
                <fmt:formatNumber 
              type="number" maxFractionDigits="3" groupingUsed="true" 
              value="${cell}" /> 
               </td> 


              </c:otherwise> 
              </c:choose> 
              </c:otherwise> 

            </c:choose> 

           </c:forEach> 
          </tr> 
         </c:forEach> 

我第一次在考虑第一种情况下设计的,而且我以前只是检查时,它得到指数0,以打印单元为是,否则,使用于formatNumber。随着过滤和后续两列的添加,我不得不重写JSTL并使第二个版本完美工作,但原来的版本并不全是错误的,分组没有出现,并且附加了“.0”第三列中的数字,第二列中不出现分组。在这两种情况下,其余列都很好。我知道它的一个简单的:

if(no. of columns == 12) 
    if(index == 0 || index == 2) 
     no format //print date and name of document as is 
    if(index == 1) 
     numberFormat with no grouping used //since this indicates document ID 

else if(no. of columns == 10) 
    if(index == 0) 
    no format //print date as is 

else 
    numberFormat with grouping //regardless of the number of columns 

但我没有得到正确的JSTL。我究竟做错了什么?请让我知道并提出一些改进建议。谢谢。

+0

你不是正确的吗? – Rembo

+0

第2列和第3列中的分组以及将“.0”附加到第3列中的数字这一事实并不可取。这只发生在第一种情况。过滤后,它正确渲染。 – CodingInCircles

回答

0
if(no. of columns == 12) 
     if(index == 0 || index == 2) 
      no format //print date and name of document as is 
     if(index == 1) 
      numberFormat with no grouping used //since this indicates document ID 

    else if(no. of columns == 10) 
     if(index == 0) 
     no format //print date as is 

    else 
     numberFormat with grouping //regardless of the number of columns 


========================================================= 

you have to write it as below 

    <c:choose> 
    <c:when test="${rowIdx.count==12}"> 
    <c:if test="${rowIdx.index==0} || ${rowIdx.index==2}"> 
     no format //print date and name of document as is 
    </c:if> 
    <c:if test="${rowIdx.index==1}"> 
     numberFormat with no grouping used //since this indicates document ID 
    </c:if> 
    </c:when> 
    <c:when test="${rowIdx.count==10}"> 
    <c:if test="${rowIdx.index==0}"> 
     no format //print date as is 
    </c:if> 
    </c:when>`enter code here` 
    <c:otherwise> 
    numberFormat with grouping //regardless of the number of columns 
    </c:otherwise>