1

如何从服务器端的 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>

测试1.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>
4

1 回答 1

1

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

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

编辑:下面的示例 JsonRead 函数。如果您没有将返回类型指定为stringans returnformatplain则必须将返回类型设置为anyreturnformatto JSON

remote string function JsonRead(string RemoteToken) returnFormat="plain" output="false" {
    if ( TestToken(arguments.RemoteToken) ) {
        return serializeJSON(QueryToStruct(QueryAllUsers()));
    }
}

我也使用dataTypeasJSON所以你的数据源看起来像这样:

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
...

此外,由于这必须是一个远程功能,您需要提供某种安全检查来防止未经授权的访问。我使用RemoteToken上面显示的。

于 2013-06-06T14:28:25.267 回答