输出创建不需要的空格

The*_*Man 2 coldfusion whitespace

我正在输出下面列出的子类别的类别.如果它不是循环中的第一个项,则每个子类都会在其前面加一个逗号.

另外,我只显示四个结果,所以如果记录计数超过四个,我需要追加...到第四个循环结果的末尾.

问题是,在...已应用的情况下,每个子类别后面都有一个额外的空间.见下文: 在此输入图像描述

看看逗号前有空格?

码:

<ul class="defaultUL" style="float:right;">
            <cfloop query="getParent" startrow="7" endrow="12">
              <cfquery name="getSubCategory" datasource="dss">
                SELECT Name, ID FROM Category WHERE ParentID = #getParent.ID#
                </cfquery>
          <cfset SubNumb = getSubCategory.recordcount>

              <li><h3><a href="?Page=#Application.Utility.qsEncode(getParent.Name)#">#getParent.Name#</a></h3>
                  <cfloop query="getSubCategory" startrow="1" endrow="#SubNumb#">
                    <cfif SubNumb gt 4>
                      <cfif getSubCategory.currentRow lt 4 AND getSubCategory.currentRow gt 1>
                          , #getSubCategory.Name#
              <cfelseif getSubCategory.currentRow eq 1>
                            #getSubCategory.Name#
                            <cfelseif getSubCategory.currentRow eq 4>
                            #getSubCategory.Name#...
                        </cfif>
                      <cfelse>
                        #getSubCategory.Name#,
                    </cfif>

                  </cfloop>
                  </li>
            </cfloop>
            </ul>
Run Code Online (Sandbox Code Playgroud)

我确保数据库中的数据最后没有空格.

Ant*_*ony 7

使用该listAppend函数构造您的字符串:

<cfset subCatList = "" /> <!--- define a variable to hold the list of subcats; variable gets reset for each iteration of outer loop --->
<cfloop query="getSubCategory" startrow="1" endrow="4">
    <!--- listAppend uses , as a the default delimiter. --->
    <cfset subCatList = listAppend(subCatList, getSubCategory.Name) />
</cfloop>
<cfif getSubCategory.RecordCount gt 4>
    <cfset subCatList = listAppend(subCatList,"...") />
</cfif>
<!---- value of subCatList at this point: subcat1,subcat2,subcat3,subcat4... --->
<!--- output subcatlist and fix spacing --->
#replace(subCatList, ",", ", ","all"#
<!--- output is subcat1, subcat2, subcat3, subcat4... --->
Run Code Online (Sandbox Code Playgroud)