如何使用ColdFusion从Kendo Grid Models中检索字段值

时间:2013-06-06 13:00:07

标签: coldfusion kendo-grid

如何从服务器端的Kendo模型中获取发布的字段值?我在服务器端有一个记录器。这表明我收到了所有字段,包括列名和值。但是,我不确定如何检索这些值:

使用过的脚本:

 <script>
                $(document).ready(function () {
                    var crudServiceBaseUrl = "http://localhost:8500/test/test1.cfc?method=",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read:  {
                                    url: crudServiceBaseUrl+"JsonRead",
                                    dataType: "json"                                    
                                },
                                create: {
                                    url: crudServiceBaseUrl+"JsonCreate",
                                    dataType: "json"
                                },
                                 parameterMap: function(options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return {models: kendo.stringify(options.models)};
                                    }
                                    return options;
                                }
                            },
                            batch: true,
                            pageSize: 20,
                            schema: {
                                type: "json",
                                model: {
                                    id: "productid",
                                    fields: {
                                        productid: { editable: false, nullable: true },
                                        productname: { validation: { required: true } },
                                        unitprice: { type: "number", validation: { required: true, min: 1} },
                                        discontinued: { type: "boolean" },
                                        unitsinstock: { type: "number", validation: { min: 0, required: true } }
                                    }
                                }
                            }
                        });

                    $("#grid").kendoGrid({
                        dataSource: dataSource,
                        pageable: true,
                        height: 430,
                        toolbar: ["create"],
                        columns: [
                            "productname",
                            { field: "unitprice", title: "Unit Price", format: "{0:c}", width: "100px" },
                            { field: "unitsinstock", title:"Units In Stock", width: "100px" },
                            { field: "discontinued", width: "100px" },
                            { command: ["edit", "destroy"], title: "&nbsp;", width: "172px" }],
                        editable: "inline"
                    });
                });
</script>

test1.cfc

    <cfcomponent>
<cffunction name="init">
<cfreturn this>
</cffunction>

<cffunction name="JsonRead" returntype="any" description="Return all Product" access="remote">  
 <cfquery name="getallproducts" datasource="DataSource">       
     SELECT * from Products
    </cfquery>

    <cfset var aTmp = arraynew(1)>     
    <cfif getallproducts.recordcount>     
        <cfloop query="getallproducts"> 
            <cfset stTmp = structNew()> 
            <cfloop list="#lcase(getallproducts.columnlist)#" index="col"> 
            <cfset stTmp[col] = getallproducts[col][currentRow]> 
            </cfloop>
            <cfset arrayAppend(aTmp,stTmp)> 
        </cfloop> 
     <cfelse> 
        <cfset stTmp = structNew()> 
            <cfloop list="#lcase(getallproducts.columnlist)#" index="col"> 
                <cfset stTmp[col] = ""> 
            </cfloop>      
            <cfset arrayAppend(aTmp,stTmp)> 
     </cfif> 

  <cfset ss=#SerializeJSON(aTmp)#>

    <cfreturn ss>

</cffunction>

<cffunction name="JsonCreate" returntype="void" description="Create New Row" access="remote">   
            <cfargument name="models" type="string" required="yes">           
            <cfset data = urldecode(arguments.models)>
            <cfset data = deserializeJSON(data, false)>
</cffunction>


</cfcomponent>

1 个答案:

答案 0 :(得分:1)

我认为您要求的是一种在保存,更新或删除后获取来自您的Kendo Grid的数据的方法。下面是一个示例,您可以执行一些操作来循环存储在来自Kendo的模型参数中的数据。请注意,如果您将网格中的batch设置为true,那么您将有来自网格的多行数据。

remote void function JsonCreate(string models) output="false" {
    var data = urldecode(arguments.models);
    data = deserializeJSON(data, false);
}

编辑:下面的示例JsonRead函数。如果您未将返回类型指定为string ans returnformatplain,则必须将返回类型设置为any,将returnformat设置为{{1} }}。

JSON

我还使用remote string function JsonRead(string RemoteToken) returnFormat="plain" output="false" { if ( TestToken(arguments.RemoteToken) ) { return serializeJSON(QueryToStruct(QueryAllUsers())); } } 作为dataType,因此您的数据源看起来像这样:

JSON

此外,由于这必须是远程功能,因此您需要提供某种安全检查以防止未经授权的访问。我使用上面显示的var serviceURL = kendoURL + "/services/Service.cfc?RemoteToken=" + RemoteToken + "&method=", dataSource = new kendo.data.DataSource({ transport: { read: { url: serviceURL + "JsonRead", dataType: "JSON" }, update: { url: serviceURL + "JsonUpdate", dataType: "JSON" }, destroy: { url: serviceURL + "JsonDelete", dataType: "JSON" }, create: { url: serviceURL + "JsonCreate", dataType: "JSON" }, parameterMap: function(options, operation) { if (operation !== "read" && options.models) { return {models: kendo.stringify(options.models)}; } } }, batch: true ...