2013-06-24 88 views
0

我有這樣的網格添加新行空劍道電網

$("#email-grid").kendoGrid({ 
    dataSource: { 
     transport: { 
      read: { 
       url: "operations/get_emails_sales_reps.php?salesRepsId=" + salesRepsId, 
       type: "GET" 
      }, 
      update: { 
       url: "operations/edit_email.php?salesRepsId=" + salesRepsId, 
       type: "POST", 
       complete: function (e) { 
        $("#email-grid").data("kendoGrid").dataSource.read(); 
       } 
      }, 
      destroy: { 
       url: "operations/delete_email.php", 
       type: "POST", 
       complete: function (e) { 
        $("#email-grid").data("kendoGrid").dataSource.read(); 
       } 
      }, 
      create: { 
       url: "operations/add_email.php?salesRepsId=" + salesRepsId, 
       type: "POST", 
       complete: function (e) { 
        $("#email-grid").data("kendoGrid").dataSource.read(); 
       } 
      }, 
     }, 


     schema: { 
      data: "data", 
      total: "data.length", //total amount of records 
      model: { 
       id: "SalesRepId", 
       fields: { 
        EmailType: { 
         defaultValue: { 
          EmailTypeId: 2, 
          EmailTypeName: "Home" 
         } 
        }, 
        EmailText: { 
         type: "string" 
        }, 
        IsMainEmail: { 
         type: "boolean" 
        } 
       } 
      } 

     }, 
     pageSize: 5, 
    }, 
    height: 250, 
    filterable: true, 
    sortable: true, 
    pageable: true, 
    reorderable: false, 
    groupable: false, 
    batch: true, 
    navigatable: true, 
    toolbar: ["create", "save", "cancel"], 
    editable: true, 
    columns: [{ 
     field: "EmailType", 
     title: "Type", 
     editor: EmailTypeDropDownEditor, 
     template: "#=EmailType.EmailTypeName#" 
    }, { 
     field: "EmailText", 
     title: "Email", 

    }, { 
     field: "IsMainEmail", 
     title: "Main?", 
     width: 65, 
     template: function (e) { 
      if (e.IsMainEmail == true) { 
       return '<img align="center" src ="images/check-icon.png" />'; 
      } else { 
       return ''; 
      } 
     } 
     // hidden: true 

    }, { 
     command: "destroy", 
     title: "&nbsp;", 
     width: 90 
    }, 

    ] 
}); 

在服務器端的代碼(get_emails_sales_reps.php)

<?php 
require_once ("../lib/salesrep.php"); 
require_once ("../lib/helper.php"); 

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

$options = array(); 

$result = SalesRep::getRepEmails($_GET["salesRepsId"]); 
if (isset($result) && $result != null) { 
    $result = _object_to_array($result); 
    if (isset($result[0]) && is_array($result)) { 
     for ($i = 0; $i < count($result); $i++) { 
      $result[$i]["EmailType"] = array("EmailTypeName" => $result[$i]["EmailType"], "EmailTypeId" => $result[$i]["EmailTypeId"]); 
     } 
    } else { 
     $result["EmailType"] = array("EmailTypeName" => $result["EmailType"], "EmailTypeId" => $result["EmailTypeId"]); 
    } 


    if (isset($result) || $result != null) { 
     echo "{\"data\":" . json_encode($result) . "}"; 
    } else { 
     echo "{\"data\": {} }"; 
    } 
} 
?> 

當電網有一個記錄或更多,我可以添加新記錄沒有任何錯誤,但是當網格沒有記錄並嘗試添加新記錄時。我得到這個錯誤

Uncaught TypeError: Cannot read property 'length' of undefined

請問,我該如何解決這個問題?

+0

我已經解決了這個問題,謝謝 – Kamal

+1

請回答你的問題,讓其他人面臨同樣的困難,可以從你的幫助.. :) – Paritosh

回答

0

我已經通過編輯php文件解決了這個問題。當結果爲空(空)我必須返回一個空的JSON數組這樣

else { 
     // the result is null 

    echo "{\"data\": [] }"; 
} 
+0

這不是一個好的方式來發布和回答問題,只有你自己.http://stackoverflow.com/questions/how-to-ask –

+0

@dotNETkid如果你回答你的問題是絕對沒有問題的,即使它是首選如果你的帖子沒有得到答案。 –

+0

關於[kendo網格的完整crud操作可以在這裏找到](http://conceptf1.blogspot.com/2013/11/kendo-ui-data-source-crud.html) –