2016-08-09 104 views
3

我有一个查询,我想按组输出。 这里是我现在我做了一个 https://jsfiddle.net/nbqqp0cb/2/如何获得按组输出的最后一条记录?

"dataset": [ 
    <cfoutput query="all_dates" group="Month_new"> 
        { 

         "seriesname": "#all_dates.Month_new#", 

        "data": [ 
        <cfoutput> 
         <CFIF all_dates.CurrentRow EQ all_dates.RecordCount> 
          { 

          "value": "#sum_total#" 
          } 

         <br> 
        <cfelse> 
          { 

          "value": "#sum_total#" 

          }, <br> 
         </cfif> 
         </cfoutput> 

        ] 
        },<br> 


         </cfoutput> 

的问题是每个组中的最后一个逗号。我得到它为最后一个记录工作,但即使是最后一个记录最后也不应该有逗号。

回答

3

您的情况<CFIF all_dates.CurrentRow EQ all_dates.RecordCount>正在查询CurrentRow与查询中的整个RecordCount。您需要检查每个组的计数。由于您在查询结果集中没有组计数(我假设),因此您需要以不同的方式检查它。这可能不是最优雅的方法,但它应该起作用。

首先上面这段代码初始化一个新的变量:

<cfset previous_month = "" > 

然后如下修改代码(这是伪代码,并没有经过测试):

"dataset": [ 
<cfoutput query="all_dates" group="Month_new"> 
    { 
     "seriesname": "#all_dates.Month_new#", 
     "data": [ 
     <cfoutput> 
      <cfif all_dates.CurrentRow GT 1> 
       <cfif previous_month EQ all_dates.Month_new and all_dates.CurrentRow LT all_dates.RecordCount> 
        , <br> 
       <cfelse> 
        <br> 
       </cfif> 
      </cfif> 
      { 
       "value": "#sum_total#" 
      } 
      <cfset previous_month = all_dates.Month_new> 
     </cfoutput> 
     ] 
    } 
    <cfif all_dates.CurrentRow LT all_dates.RecordCount>,</cfif> 
    <br> 
</cfoutput> 
+0

是它的工作好,只有问题进入最后},因为最后}应该没有,最后 –

+1

你需要添加一个类似的条件/逻辑到外层循环。我已将代码添加到我的答案中。 –

+0

也可能[首先建立一个列表/数组字符串](http://trycf.com/gist/a6fdde2dbd28fed824eb0fd1d792da9b/acf2016?theme=monokai)并避免整个逗号问题?这就是说,@ anatp_123 - 你真的必须手动构建JSON字符串吗? – Leigh

相关问题