2015-11-18 16 views
-2

我正在寫一個函數來生成HTML列,我在查詢中使用可變列名的語法遇到了一些麻煩。在對象表示法中使用可變列名

<cffunction name="generateColumns" output=True> 
    <cfargument name="query"> 
    <cfargument name="numberOfColumns"> 
    <cfargument name="columnName"> 
    <cfargument name="linkVariable"> 
    <cfset html = ''> 
    <cfset itemsPerColumn = Ceiling(query.recordCount/3)> 
    <!--- Loop through each column ---> 
    <cfloop from="1" to="#numberOfColumns#" index="outerIndex"> 
     <cfset html = html & '<ul class="icf_nav-iblock">'> 
     <!--- Loop through the inner items ---> 
     <cfloop from="1" to="#itemsPerColumn#" index="innerIndex"> 
      <cfset totalIndex = ((outerIndex - 1) * itemsPerColumn) + innerIndex> 
      <cfset link = createRegressiveLink("#linkVariable#",query["#columnName#"][totalIndex])> 
      <cfset html = html & '<li data-id="' & query["#columnName#"][totalIndex] & '">'> 
      <cfset html = html & '<a href="' & link & '" class="icf_btn_small">'> 
      <cfset html = html & query["#columnName#"][totalIndex]> 
      <cfset html = html & '</a>'> 
      <cfset html = html & '</li>'> 
     </cfloop> 
     <cfset html = html & '</ul>'> 
    </cfloop> 
    <cfreturn html> 
</cffunction> 

,我發現了以下錯誤:

[Table (rows 10 columns VCHRMAKE): [VCHRMAKE: [email protected]] ] is not indexable by columnName 

我已經試過

query["#columnName#"][totalIndex] 
query["columnName"][totalIndex] 
query[columnName][totalIndex] 

,我也得到了同樣的錯誤。

回答

0

原來我在調用函數時犯了一個愚蠢的錯誤。我傳遞了「columnName」字符串作爲列的名稱。結果查詢[columnName] [totalIndex]是正確的語法。