嵌套结构未正确排序
Nested structures not sorted correctly
我正在循环查询,构建结构数组
<cffunction name="QueryConvert" returntype="any" output="false">
<cfargument name="q" type="query" required="yes">
<cfargument name="page" type="numeric" required="no" default="1">
<cfargument name="rows" type="numeric" required="no" default="500">
<cfset var result = structnew()>
<cfset var rowStruct = structnew()>
<cfset var col = "">
<cfset result["page"] = arguments.page>
<cfset result["total"] = ceiling(arguments.q.TotalrecordCount/arguments.rows)>
<cfset result["records"] = arguments.q.TotalrecordCount>
<cfset result["rows"] = arraynew(1)>
<cfset queryDeleteColumn(arguments.q,'TotalrecordCount')>
<cfset queryDeleteColumn(arguments.q,'rowNum')>
<cfset columnLabels = QueryColumnArray(arguments.q)>
<cfset rowStruct = [:]><!--- Tada an ordered struct --->
<cfloop array="#columnLabels#" item="col">
<cfset rowStruct[col] = q["#col#"]>
</cfloop>
<cfdump var="#result#" abort>
<cfreturn result />
</cffunction>
但是当我查看嵌套结构时,键的顺序全部混淆了。我希望它们与数据库中列名的顺序匹配 table。
简短的回答是您需要使用有序结构。长版本如下所示:
首先我们需要看一些样本数据
<cfscript>
q = queryNew("id,name,category","Integer,Varchar,Varchar",
[{id=1, name="One", category="Cat"},{id=2, name="Two", category="Dog"}]
);
writedump(q);
</cfscript>
让我们看看示例数据。请注意,列与预期不同。
现在让我们准备好 columnLabels。请注意,我们正在创建一个数组。
</cfscript>
result.rows = [];
columnLabels = q.getMeta().getColumnLabels();
writedump(columnLabels);
</cfscript>
<cfloop query="q">
<cfset rowStruct = [:]><!--- Tada an ordered struct --->
<cfloop array="#columnLabels#" item="col">
<cfset rowStruct[col] = q["#col#"]>
</cfloop>
<cfset arrayappend(result.rows, rowStruct)>
</cfloop>
<cfdump var="#result.rows#">
多田
此外
使用 cfscript
更容易阅读
<cfscript>
for (row in q) {
rowStruct = [:];
for (col in columnLabels) {
rowStruct[col] = q["#col#"];
}
result.rows.append(rowStruct);
}
</cfscript>
现场版见:CFFiddle.com
我正在循环查询,构建结构数组
<cffunction name="QueryConvert" returntype="any" output="false">
<cfargument name="q" type="query" required="yes">
<cfargument name="page" type="numeric" required="no" default="1">
<cfargument name="rows" type="numeric" required="no" default="500">
<cfset var result = structnew()>
<cfset var rowStruct = structnew()>
<cfset var col = "">
<cfset result["page"] = arguments.page>
<cfset result["total"] = ceiling(arguments.q.TotalrecordCount/arguments.rows)>
<cfset result["records"] = arguments.q.TotalrecordCount>
<cfset result["rows"] = arraynew(1)>
<cfset queryDeleteColumn(arguments.q,'TotalrecordCount')>
<cfset queryDeleteColumn(arguments.q,'rowNum')>
<cfset columnLabels = QueryColumnArray(arguments.q)>
<cfset rowStruct = [:]><!--- Tada an ordered struct --->
<cfloop array="#columnLabels#" item="col">
<cfset rowStruct[col] = q["#col#"]>
</cfloop>
<cfdump var="#result#" abort>
<cfreturn result />
</cffunction>
但是当我查看嵌套结构时,键的顺序全部混淆了。我希望它们与数据库中列名的顺序匹配 table。
简短的回答是您需要使用有序结构。长版本如下所示:
首先我们需要看一些样本数据
<cfscript>
q = queryNew("id,name,category","Integer,Varchar,Varchar",
[{id=1, name="One", category="Cat"},{id=2, name="Two", category="Dog"}]
);
writedump(q);
</cfscript>
让我们看看示例数据。请注意,列与预期不同。
现在让我们准备好 columnLabels。请注意,我们正在创建一个数组。
</cfscript>
result.rows = [];
columnLabels = q.getMeta().getColumnLabels();
writedump(columnLabels);
</cfscript>
<cfloop query="q">
<cfset rowStruct = [:]><!--- Tada an ordered struct --->
<cfloop array="#columnLabels#" item="col">
<cfset rowStruct[col] = q["#col#"]>
</cfloop>
<cfset arrayappend(result.rows, rowStruct)>
</cfloop>
<cfdump var="#result.rows#">
多田
此外
使用 cfscript
<cfscript>
for (row in q) {
rowStruct = [:];
for (col in columnLabels) {
rowStruct[col] = q["#col#"];
}
result.rows.append(rowStruct);
}
</cfscript>
现场版见:CFFiddle.com