首页 > 解决方案 > 试图从 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>

标签: coldfusionjqgridcfmlcffunction

解决方案


序列化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>

请注意,我们没有调用serializeJSONresult因为returnFormat="json"已经为我们这样做了。关于您的colNames:您的 JS 示例使用映射的列名,而不是您的代码和上面的代码中显示的动态列名。您可能希望将它们设为静态或自己映射它们。


推荐阅读