jquery - 创建动态 html 表,其中 td 值与 ms sql db 中的 th 值关联

标签 jquery sql-server coldfusion

使用 ColdFusion 9 服务器和 MS-SQL 数据库,我尝试创建 html 表,其中 td 值与同一 MS-Sql 数据库表中的 th 值关联。

查询 1 - 获取部门:

<cfquery datasource="datasrc" name="qGetDep">
    SELECT DISTINCT Dept
    FROM someTable
    WHERE isnull(Dept, '') != ''
    ORDER BY DEPT ASC
</cfquery>

查询 2 - 获取名称

<cfquery datasource="datasrc" name="qGetNam">
    SELECT a.Dept, a.names
    FROM someTable as a
    INNER JOIN
    (
    SELECT DISTINCT Dept, MIN(ID) as ID, names
    FROM someTable
    GROUP BY Dept, names
    ) as b
    ON a.Dept = b.Dept
    AND a.ID = b.ID
    WHERE isnull(a.Dept, '') != ''
    ORDER BY a.Dept ASC
</cfquery>

index.cfm

<table border="1" id="table1">
    <thead>
        <tr>
            <cfoutput query="qGetDep">
                <th><b>#DEPT#</b></th>
            </cfoutput>
        </tr>
    </thead>
    <tbody>
        <cfoutput query="qGetNam">
            <tr>
                <cfloop query="qGetDep">
                    <cfset cls= qGetDep.Dept>
                    <cfif qGetNam.Dept EQ cls >
                        <td class="#cls#">#qGetNam.names#</td>
                    <cfelse>
                        <td class="#cls#"></td>
                    </cfif>

                </cfloop>
            </tr>
        </cfoutput>
    </tbody>
</table>

使用数据库中的当前数据,这将输出与此类似的表: enter image description here

我需要的是这样的。 enter image description here
jQuery 解决方案是可以接受的。
任何帮助将不胜感激。
谢谢。

最佳答案

我是根据我对您“实际私有(private)数据”的假设来回答这个问题的。

希望即使我的假设是错误的,这也能让你朝着正确的方向前进。

如果这是你的 -

Department Table

Department Table

这是你的

People table

People Table

然后使用此查询 -

SELECT *
  FROM (
        SELECT a.id AS pid
              ,a.NAME
              ,b.dept
          FROM deptPeople a
              ,deptTable b
         WHERE a.dept = b.dept
       ) AS A
  PIVOT(
        MIN(NAME) FOR dept IN ( dept1, dept2, dept3, dept4, dept5)
       ) AS B

会给你

Final Result

像这样组合 ColdFusion 中的所有内容 -

<cfquery datasource="que_tempdb_int" name="qGetDep">
    SELECT dept
    FROM deptTable
    ORDER BY DEPT ASC
</cfquery>

<cfset allDept = valuelist(qGetDep.dept,',') />

<cfquery name="qGetNam" datasource="que_tempdb_int">
    SELECT *
      FROM (
            SELECT a.id AS pid
                  ,a.NAME
                  ,b.dept
                  FROM deptPeople a
                  ,deptTable b
             WHERE a.dept = b.dept
           ) AS A
      PIVOT(
            MIN(NAME) FOR dept IN (#allDept#)
           ) AS B
</cfquery>

<table border="1" id="table1">
    <thead>
        <tr>
            <cfoutput query="qGetDep">
                <th><b>#DEPT#</b></th>
            </cfoutput>
        </tr>
    </thead>
    <tbody>
    <cfoutput query="qGetNam">
        <tr>
            <cfloop list="#allDept#" index="dept" delimiters=",">
            <td>#qGetNam[dept][qGetNam.currentRow]#</td>
            </cfloop>
        </tr>
    </cfoutput>
    </tbody>
</table>

最终结果 -

HTML Pivot Table

关于jquery - 创建动态 html 表,其中 td 值与 ms sql db 中的 th 值关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884712/

相关文章:

javascript - 如何使用Jquery在对话框中显示要删除的项目?

file - 获取浏览文件的绝对路径

coldfusion - 尝试从ColdFusion使用java.lang.string格式方法时获取“The format method was not found”

coldfusion - 使用 CFIMAP 预览电子邮件而不标记 SEEN 标志?

jquery - 一个不糟糕的 Jquery 旋转器?

javascript - 我可以将一组对象放入一个 JSON 对象中吗?

jquery - 无法让 Jquery 点击更改类

c# - 如何在 C# 中捕获 SQLException?

sql - SQL Server SQL_Latin1_General_CP1_CI_AS 能否安全地转换为 Latin1_General_CI_AS?

c# - ExecuteNonQueryAsync sql 命令为 SP 提供 DB 中的多个参数集