<cfoutput> 具有多对多关系



我正在为会计部门做一个项目。我有一个数据库(MySQL)表与分类账代码。我们公司有几个不同的办公地点,这些代码中的每一个都可以应用于一个或多个办公地点。每个办公地点可以有一个或多个适用的分类账代码。所以我有一个多对多关系和一个桥表包含code_idlocation_id。我的SQL如下:

SELECT gl.`code_id`, gl.`account_code`, gl.`account_type`, gl.`account_desc`, glloc.`location_id`
FROM `gl_codes` as gl
    LEFT JOIN `gl_codes_locations` as glloc
ON gl.`code_id` = glloc.`code_id`
ORDER BY gl.`code_id`, glloc.`location_id`

这将产生一个表,每个code_id/location_id对都有单独的行。我想在使用cfoutput的表中显示这一点。我希望每个code_id只对应一行,但是我将在每行中使用一列来标记该代码是否适用于给定的location_id,如下所示:

| CodeAccount | CodeType | CodeDescription | Code Location | | | | | 1 | 2 | 3 | 4 | |SomeAcct | SomeCode | Some Desc | X | | X | |


我知道我不能嵌套cfoutput标签与多个query属性。我试过分组,但好像总做不对。请帮助!

这应该使您非常接近。首先,我们需要一个可用id列表,这样我们就知道我们需要多少Location子列。

<cfquery name="locationData">
  SELECT location_id FROM gl_codes_locations ORDER BY location_id
</cfquery>
<cfset allLocationIds = ValueList(locationData.location_id)>

然后,在表中,我们可以使用以下信息构建header和body:

<thead>
    <tr>
      <td>Code ID</td>
      <td>Code Account</td>
      <td>Code Type</td>
      <td>Code Description</td>
      <td colspan="#ListLen(allLocationIds)#">Code Location</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <cfloop list="#allLocationIds#" index="id">
        <td>#HtmlEditFormat(id)#</td>
      </cfloop>
    </tr>
</thead>
<tbody>
  <cfoutput query="ledgerData" group="code_id">
    <cfset currLocationIds = "">
    <cfoutput>
      <cfset currLocationIds = ListAppend(currLocationIds, location_id)>
    </cfoutput>
    <tr>
      <td>#HtmlEditFormat(code_id)#</td>
      <td>#HtmlEditFormat(account_code)#</td>
      <td>#HtmlEditFormat(account_type)#</td>
      <td>#HtmlEditFormat(account_desc)#</td>
      <cfloop list="#allLocationIds#" index="id">
        <td>#ListFind(currLocationIds, id) gt 0 ? 'X' : ''#</td>
      </cfloop>
    </tr>
  </cfoutput>
</cfoutput>

感谢@Tomalac和他的ValueList建议,我能够将其适应我的代码并使其按我想要的方式工作。子列技巧很棒,我可能会在将来实现它,但现在我们正在处理固定数量的位置。

作为参考,相关的完成代码如下:出于隐私考虑,我对地名进行了编辑。

<table class="table table-striped table-bordered">
    <thead class="bg-nav text-white">
        <tr>
            <th scope="col" rowspan="2" class="align-middle">Code</th>
            <th scope="col" rowspan="2" class="align-middle">Type</th>
            <th scope="col" rowspan="2" class="align-middle">Description</th>
            <th scope="col" colspan="4" class="text-center">Applies To</th>
            <th scope="col" rowspan="2" class="text-center align-middle">Edit</th>
            <th scope="col" rowspan="2" class="text-center align-middle">Delete</th>
        </tr>
        <tr>
            <th scope="col">Chicago</th>
            <th scope="col">Detroit</th>
            <th scope="col">LA</th>
            <th scope="col">New York</th>
        </tr>
    </thead>
    <tbody>
        <cfoutput query="codes" group="code_id">
                <tr>
                    <!--- Use function in cfcomponent to grab the location(s) that pertain to the given code_id --->
                    <!--- Dump query results into ValueList --->
<cfset codeLocations = ValueList(createObject("component", "com.modules.glcodes").getCodeLocations("query", codes.code_id).location_id)>
                    <td>#account_code#</td>
                    <td>#account_type#</td>
                    <td>#account_desc#</td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "3") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "2") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "4") GT 0)>X</cfif></td>
                    <td><cfif ListLen(codeLocations) GT 0 AND (ListContains(codeLocations, "1") GT 0)>X</cfif></td>
                    <td>Edit</td>
                    <td>Delete</td>
            </tr>
        </cfoutput>
    </tbody>
</table>

最新更新