1

我有一个 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>
4

2 回答 2

1

序列化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 示例使用映射的列名,而不是您的代码和上面的代码中显示的动态列名。您可能希望将它们设为静态或自己映射它们。

于 2019-07-31T20:13:42.693 回答
1

我喜欢亚历克斯的方法,但我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>

函数签名

在此处输入图像描述

函数结果

在此处输入图像描述

另请参阅 使用 cfscript 函数的 JSON 响应

于 2019-07-31T21:42:16.373 回答