coldfusion - 试图从 cfc 函数中获取这样的数据
问题描述
我有一个 cfc,我需要在其中返回 jqgrid 的标头以进行动态绑定
我正在尝试像这样查询数据:
colNames: ['ID', 'Institution Name', 'Display Name', 'Short Name', 'Board of Education', 'Scheme Name','Subscription Date'],
colModel: [
{ name: 'institutionid', sortable: true, },
{ name: 'institutionname', sortable: true },
{ name: 'displayname', sortable: true },
{ name: 'shortname' ,sortable: true},
{ name: 'supportedfield', sortable: true },
{ name: 'schemename', sortable: true },
{ name: 'subscriptionto', sortable: true}
]
我可以很容易地得到 colNames,但是对于 colmodal,我怎样才能带来排序的元素:默认情况下对所有都是 true,格式应该像 arrayofstructs
谢谢
查询尝试
<cffunction name="headers" localmode="modern" access="remote" returnformat="json" hint="Handles the Functionality of returning the Table Headers">
<cfset columnsInfos = {}>
<cfset returnArray = []>
<cfset cList = QueryExecute("select top 1 * from mytable").columnList>
<cfset cListQueryObj = QueryNew(cList)>
<cfdump var="#cListQueryObj#" abort>
<cfset colNames = ListtoArray(cList)>
<cfloop query="#cListQueryObj#">
<cfset rowStruct = {}>
<cfloop list="#cList#" index="colname">
<cfset "rowStruct['#colname#']" = cListQueryObj[colname]>
</cfloop>
<cfset arrayAppend(returnArray,rowStruct)>
<cfdump var="#rowStruct#">
</cfloop>
<cfset columnsInfos["colModel"] = returnArray>
<cfset columnsInfos["colNames"] = colNames>
<cfreturn columnsInfos>
</cffunction>
解决方案
序列化query
对象不会返回预期结果。由于您使用的是 built-in returnFormat="json"
,因此最简单(可能也是唯一)的方法是使用结构数组,就像您的 JS 示例所示:
<cffunction name="headers" localmode="modern" access="public" returnformat="json" hint="Handles the Functionality of returning the Table Headers">
<!--- best practise: declare the returned scheme --->
<cfset result = {
"colNames": [],
"colModel": []
}>
<!--- get your table's columns (mockup) --->
<cfset columnList = "institutionid,institutionname,displayname,shortname,supportedfield,schemename,subscriptionto">
<!--- use the column names from your query? --->
<cfset result["colNames"] = listToArray(columnList)>
<!--- add an entry with "name" and "sortable" for every column --->
<cfloop list="#columnList#" index="columnName">
<cfset result["colModel"].add({
"name": columnName,
"sortable": true
})>
</cfloop>
<cfreturn result>
</cffunction>
请注意,我们没有调用serializeJSON
,result
因为returnFormat="json"
已经为我们这样做了。关于您的colNames
:您的 JS 示例使用映射的列名,而不是您的代码和上面的代码中显示的动态列名。您可能希望将它们设为静态或自己映射它们。
推荐阅读
- c++ - 如何设置 c++ boost graphml 节点和边 ID?
- openshift - 如何在 Openshift 中为我拥有令牌的特定用户获取用户角色?
- excel - 将下拉表单字段值从 Word 表导入 Excel
- javascript - 使用不包含的 XPATH 查找特定元素
- javascript - 如何通过移动范围输入来更改 div 的背景颜色
- statistics - 协方差矩阵而非原始数据的典型相关分析
- python - 试图从熊猫数据框中绘制数据
- javascript - 将羽毛灯箱应用到现有的播放视频
- kubernetes - 语法 - 为什么 kubernetes 标签会用“-”删除?
- javascript - 使用 HTML 元素创建模板