首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多到多关系的<cfoutput>

多到多关系的<cfoutput>
EN

Stack Overflow用户
提问于 2021-01-14 17:12:44
回答 2查看 62关注 0票数 2

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

代码语言:javascript
复制
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标记。我试过一些分组,但我似乎做不到。请帮帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-14 17:31:40

这会让你很接近的。首先,我们需要一个可用ID的列表,因此我们知道我们需要多少个位置子列。

代码语言:javascript
复制
<cfquery name="locationData">
  SELECT location_id FROM gl_codes_locations ORDER BY location_id
</cfquery>
<cfset allLocationIds = ValueList(locationData.location_id)>

然后,在表中,我们可以使用以下信息构建标题和正文:

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2021-01-14 19:22:40

感谢@Tomalac和他的ValueList建议,我能够根据我的代码调整它,并使它以我想要的方式工作。该子列提示是伟大的,我可能会在未来实现它,但目前我们正在处理一个固定数量的地点。

有关的完整守则如下,供参考。出于隐私原因,我编辑了位置名称。

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65723668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档