2

我正在为我们的会计部门做一个项目。我有一个带有分类帐代码的数据库(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我尝试了一些分组,但我似乎无法做到正确。请帮忙!
4

2 回答 2

2

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

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

然后,在表格内部,我们可以使用以下信息构建表头和正文:

<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>
于 2021-01-14T17:31:40.550 回答
1

感谢@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>
于 2021-01-14T19:22:40.853 回答