Kendo Grid Update无法正常工作

时间:2017-02-07 12:08:00

标签: php telerik kendo-grid kendo-datasource

我正在一个项目中工作,我必须使用Kendo Grid更新数据库的数据。所以,我使用Kendo的CURD属性,PHP作为服务器端语言。

我遇到的问题是当我点击保存按钮时数据没有更新。 我没有找到任何博客或网站正确解释如何使用CURD与PHP,但我找到了一个博客,我遵循链接中提到的相同程序:http://www.telerik.com/blogs/get-rolling-with-kendo-ui-and-php-ndash-part-2

以下是我的代码:

<!DOCTYPE html> 
<html>
    <head>
        <title>Set Goals</title>


        <link rel="stylesheet" href="Kendostyles/kendo.common.min.css" />

    <link rel="stylesheet" href="Kendostyles/kendo.flat.min.css" />
    <link rel="stylesheet" href="Kendostyles/kendo.flat.mobile.min.css" />



    <script src="Kendojs/jquery.min.js"></script>
    <script src="Kendojs/kendo.all.min.js"></script>

         <script>


                $(document).ready(function () {
                    var crudServiceBaseUrl = "http://localhost/Test/Goals_data.php",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read:  {
                                    url: crudServiceBaseUrl,

                                    dataType: "json"
                                },
                                update: {
                                    url: crudServiceBaseUrl,
                                    type: "POST",
                                    dataType: "json",

                                    error: function(e) {
      alert(e.responseText);
    }

                                },

                                destroy: {
                                    url: crudServiceBaseUrl ,
                                    dataType: "json"
                                },
                                create: {
                                    url: crudServiceBaseUrl ,
                                    dataType: "json"
                                },
                                 parameterMap: function(options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return {models: kendo.stringify(options.models)};
                                    }
                                }

                            },

                            pageSize: 20,
                            schema: {
                                model: {
                                    id: "month_id",
                                    fields: {
                                        month_id: { editable: false, nullable: false },
                                        month_desc: { validation: { required: true },editable: false },
                                        revenue: { type: "number", validation: { required: true, min: 1} },

                                        wheels_count: { type: "number", validation: { min: 1, required: true } },
                                        hours: { type: "number", validation: { min: 1, required: true } },
                                        invoice_count: { type: "number", validation: { min: 1, required: true } },


                                    }
                                    /*Revtotal: function() {
                                  return this.get("UnitPrice") *  this.get("UnitsInStock");
                                }*/
                                }
                            },

                            save: function(){

                                this.refresh();
                            },

                             aggregate: [ { field: "month_desc", aggregate: "count" },
                                          { field: "revenue", aggregate: "sum" },
                                          { field: "wheels_count", aggregate: "sum" },
                                          { field: "hours", aggregate: "sum" },
                                          { field: "invoice_count", aggregate: "sum" },
                                          //{field: "Revtotal", aggregate: "sum"}
                                          ]
                        });

                    $("#grid").kendoGrid({
                        dataSource: dataSource,

                        pageable: true,


                        columns: [
                            { field:"month_desc", title: "Month" ,editable: false,aggregates: ["count"], footerTemplate: "Total ", width: "100px"},
                            { field: "revenue", title:"Revenue", format: "{0:c}", width: "120px" ,aggregates: ["sum"],footerTemplate: "#=sum#"},
                            { field: "wheels_count", title:"Wheels", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},
                            { field: "hours", title:"Hours", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},
                            { field: "invoice_count", title:"Invoice", width: "120px",aggregates: ["sum"],footerTemplate: "#=sum#"},

                           /* {
                        field: "Revtotal",
                        title: "Total",
                        width: "120px",
                        editable: false,
                        template: "#= Revtotal() #",
                        aggregates: ["sum"],footerTemplate: "#= sum #"
                    },*/
                            ],


  editable: true,
  navigable: true,  // enables keyboard navigation in the grid
  toolbar: [ "save", "cancel" ]  // adds save and cancel buttons

                    });

                   /* var item = {
revenue: $("#txtrevenue").val(),
wheels_count: $("#txtwheels_count").val(),
hours: $("#txthours").val(),
invoice_count: $("#txtinvoice_count").val(),
}

dataSource.add(item);
dataSource.sync();
if(dataSource.sync())
{
console.log("done");
}*/
                });
            </script>


    </head>

    <body>

        <div id="grid" width=100% height=90%></div>

<a href="#" id="rt">return</a>


    </body>



</html>

**PHP code:**

<?php

require 'connection.php';





 // add the header line to specify that the content type is JSON
    header("Content-type: application/json");

    // determine the request type
    $verb = $_SERVER["REQUEST_METHOD"];


       // handle a GET 
    if ($verb == "GET") {
        $query2= mysql_query("select substr(a12.month_desc,1,3) month_desc,a12.month_id,a11.revenue,a11.wheels_count,a11.hours,a11.invoice_count from fct_order_month a11 left  outer join dim_month a12 on a11.month_id=a12.month_id where a11.franchise_id='1' and a12.month_end_date >'2013-01-01'
 and a12.month_start_date <'2013-12-31' group by a11.month_id,a11.franchise_id");

$arr= array();

  while ($row2=mysql_fetch_assoc($query2)) {

        $arr[]=$row2;
      }

      echo json_encode($arr);
    }

// handle a POST  
    if ($verb == "POST") {

 // DISCLAIMER: It is better to use PHP prepared statements to communicate with the database. 
        //             this provides better protection against SQL injection.
        //             [http://php.net/manual/en/pdo.prepared-statements.php][4]
        // get the parameters from the post. escape them to protect against sql injection.

       $rev = mysql_real_escape_string($_POST["revenue"]);
 $whl = mysql_real_escape_string($_POST["wheels_count"]);
$hr = mysql_real_escape_string($_POST["hours"]);
 $ic = mysql_real_escape_string($_POST["invoice_count"]);
 $mon = mysql_real_escape_string($_POST["month_id"]);



        $rs = mysql_query("UPDATE fct_order_month SET revenue = '" . $rev ."',wheels_count = '" . $whl ."',hours = '" . $hr ."',invoice_count = '" . $ic ."' WHERE month_id = '" .$mon. "' and franchise_id='1' ");
if ($rs) {

        echo json_encode($rs);
    }
    else {
        header("HTTP/1.1 500 Internal Server Error");
        echo "Update failed for Month: ";
    }


}


?>

我在做错误的地方? 任何人都可以帮助我。

0 个答案:

没有答案