1. 程式人生 > >freemaker:同一個td中將字串資料動態換行

freemaker:同一個td中將字串資料動態換行

1、需求

     常見的報表均採用了freemaker的格式,那對於報表中的某個欄位資料是由字串拼接而成,而在報表中的某一行中需要將這些資料串分隔為多行資料進行分行顯示,對於這種問題,我們應該如何解決?

 2、方法

     這個問題可大致分為兩種情況。即根據字串的拼接樣式,可以有不同的解決方法以及前臺展示。

 a、拼接字串為:

       String str1 ="蘋果    12399.00;香蕉   17000.00;火龍果    23000.00";

     這種格式的字串,用於報表在前臺展示比較簡單,直接在Control層使用分隔方法將字串進行分隔,然後放入到字串陣列中即可,顯示的欄位名為fruit

           String[]  strList =str1.split(";");

 

//首先從後臺獲取到所有資料(包括fruit)

             ResultVO<Map>rs= costnoticemgservice.queryTargSetByParams(params);

             

   //專案資訊表

           List TargSetListData=  (List)rs.getModel().get("o_ExaInfo_Cur");

                  if(TargSetListData != null &&TargSetListData.size() > 0){

                            String[] tempStr =null;

                         for(inti=0;i<TargSetListData.size();i++){

                             Map<Object,String> tempList =(Map<Object, String>) TargSetListData.get(i);

                                if(tempList!=null && tempList.size()>0){

                                       String tempData =tempList.get("fruit");

                                               //將獲取的字串資料用分隔符進行拆分

                                       tempStr =tempData.split(";");

                                       map.put("prolistdoc",tempStr);//表示專業部門目標框的換行資料

                                }

                         }

                        map.put("targsetlistdoc", TargSetListData);

                  }else{

                         map.put("targsetlistdoc",null);

                  }

在freemaker模板中如下修改:

        

<tdcolspan="3" style="text-align:left;color:#000080;">

   <#list prolistdoc! as subinfo>

   <#assign sqlno = subinfo_index+1>

${sqlno}、${subinfo}<br/>

</#list>

</td>        

    b、拼接字串為:

          String str1 ="蘋果:12399.00;香蕉:17000.00;火龍果:23000.00";

  這種格式的字串,相較於上述情況稍微複雜一些,直接在Control層使用分隔方法將字串進行兩次分隔,放入到字串陣列中即可,顯示的欄位名為fruit

           String[] strList = str1.split(";");//第一次分隔

           String[] strList = str1.split(";");//第二次分隔

//首先從後臺獲取到所有資料(包括fruit)

 

              ResultVO<Map>rs= costnoticemgservice.queryTargSetByParams(params);

                    List DesBomListData=  (List)rs.getModel().get("o_ExaInfo_Cur");

 

          if(DesBomListData!= null && DesBomListData.size() > 0){

                map.put("desbomlistdoc",DesBomListData);

              //並從DesBomListData 獲取fruit欄位 

                       Map<Object,String> tempList =(Map<Object, String>) TargSetListData.get(i);

                if(tempList !=null &&tempList.size()>0){

                    String tempData = tempList.get("fruit");

                     String[] strList = str.split(";");

         String[] recordStr = null;

for(inti=0;i<srcList.length;i++){//行資料

  String tempStr=srcList[i];

  recordStr =tempStr.split(":");

 // String deptName=recordStr[0];

 // String price=recordStr[1];

                                                    map.put("prolistdoc", recordStr);//表示專業部門目標框的換行資料

                                }

                         }

                        map.put("targsetlistdoc", TargSetListData);

                  }else{

                         map.put("targsetlistdoc",null);

                  }

 

在freemaker模板中如下修改:

        

<tdcolspan="3" style="text-align:left;color:#000080;">

  <#list prolistdoc! as deptinfo>

${deptinfo[0]} ${deptinfo[1]}<br/>

</#list>

</td>        

 

在這種需要分隔兩次的情況下,其實還存在另外一種方法解決,那就是當第二次分隔時,直接使用replace()方法之後,後面的步驟同情況a的後續步驟一樣。