试图从 cfc 函数中获取这样的数据

trying to get the data like this from the cfc function

我有一个 cfc,我需要 return jqgrid 的 headers 用于动态绑定

我正在尝试这样查询数据:

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 我怎样才能带来 sort: 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 对象不会 return 预期的结果。由于您使用的是内置 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>

请注意,我们不会在 result 上调用 serializeJSON,因为 returnFormat="json" 已经为我们完成了。关于您的 colNames:您的 JS 示例使用映射的列名,而不是您的代码和上面的代码中所示的动态列名。您可能想让它们成为静态的或自己映射它们。

我喜欢 Alex 的方法,但我更喜欢 cfscript。我也喜欢局部变量。

<cfscript>
/**
 * @hint         Handles the Functionality of returning the Table Headers
 * @output       false
 * @returnFormat JSON
*/ 
public any function headers() {

  // best practise: declare the returned scheme
  var result = {
      "colNames": [],
      "colModel": []
  };

  // get your table's columns (mockup) 
  var columnList = "institutionid,institutionname,displayname,shortname,supportedfield,schemename,subscriptionto";

  // use the column names from your query? 
  result.colNames = listToArray(columnList);

  // add an entry with "name" and "sortable" for every column 
  for (var columnName in columnList)  {

     result.colModel.add({
        "name":     columnName,
        "sortable": true
    });
  }

  return result;
}

writedump(headers);
writedump(headers());
</cfscript>

函数签名

函数结果

另见 JSON response using cfscript function