2013-04-29 58 views
1

我正在尝试第一次使用开源Kendo Grid。我有了基本的网格并运行得很好,但是现在我需要添加一个搜索功能,并搜索名字和姓氏。我试图做到这一点在阿贾克斯,但我被困在了一个错误:向Kendo Grid添加文本搜索

错误:无法调用方法的不确定

我的代码“读”:

 <div id="search-index"> 
       <div class="editor-field"> 
        <label>First Name:</label> 
        @Html.TextBox("FirstName") 

        <label style = "margin-left: 15px;">Last Name:</label> 
        @Html.TextBox("LastName", "", new { style = "margin-right: 15px;" }) 
       </div>    
       <div id="search-controls-index"> 
         <input type="button" id="searchbtn" class="skbutton" value="Search" /> 
         <input type="button" id="addPersonbtn" class="skbutton" value="Add New Person" onclick="location.href='@Url.Action("AddPerson", "Person")'"/> 
       </div> 
     </div> 

     <div id="index-grid"></div>   
    </div> 



    $(document).ready(function() { 


     var grid = $('#index-grid').kendoGrid({ 
      height: 370, 
      sortable: true, 
      scrollable: true, 
      pageable: true, 
      dataSource: { 
         pageSize: 8, 
         transport: { 
            read: "/Home/GetPeople", 
            dataType:"json" 
            } 
         }, 
      columns: [ 
           { field: "FirstName", title: "First Name" }, 
           { field: "LastName", title: "Last Name" }, 
           { field: "Gender", title: "Gender" }, 
           { field: "DOB", title: "Date of Birth", template: '#= kendo.toString(new Date(parseInt(DOB.substring(6))), "MM/dd/yyyy") #' }, 
           { field: "IsStudent", title: "Is a Student?" }] 
     }); 


     $("#searchbtn").on('click', function() { 
      var fsname = $("#FirstName").val(); 
      var ltname = $("#LastName").val(); 

      $.ajax({ 
       type: 'GET', 
       url: '@Url.Content("~/Home/GetPeople")', 
       data: { fname: fsname, lname: ltname }, 
       success: function (data) { 
        grid.dataSource.read(); 
       }, 
       error: function() { 
        $("#index-grid").html("An error occured while trying to retieve your data."); 
       } 
      }); 
     }); 
    }); 

应该的问题,但这里是我的控制器(采用asp MVC 3):

public JsonResult GetPeople(string fname, string lname) 
    { 
     if (((fname == "") && (lname == "")) || ((fname == null) && (lname == null))) 
     { 
       var peopleList = repo.GetPeople(); 

       return Json(peopleList, JsonRequestBehavior.AllowGet); 
     } 
     else 
     { 
      var personResult = repo.GetSearchResult(fname, lname); 

      return Json(personResult, JsonRequestBehavior.AllowGet); 
     } 
    } 
+0

它是否对GetPeople进行初始页面加载调用? – Myzifer 2013-05-01 09:04:30

+0

仅供参考,您可以使用'string.IsNullOrWhitespace(fname)'来检查空,空字符串或空白。不需要手动进行所有那些检查......非常混乱。 – 2014-08-02 04:51:36

回答

2

的问题是,$("#grid").kendoGrid()返回其不具有dataSource字段jQuery对象。以下是如何获得对客户端对象的引用:http://docs.kendoui.com/getting-started/web/grid/overview#accessing-an-existing-grid

+0

我可能错过了一些东西,但在我看来,我已经在追随它。变量“网格”是整个网格的数据源,等等。我对编程总的来说很陌生,所以我可能无论如何都会犯这个错误。我搜索了几天,并没有发现任何人将文本搜索与Kendo网格相结合。你知道有关它的任何提及吗? – BattlFrog 2013-04-29 21:17:32

+0

您是否检查过我发送的帮助文章?它显示了如何获取对网格客户端对象的引用。 – 2013-04-30 08:19:03

+0

是的,但我仍然无法弄清楚如何用文本框搜索过滤我的网格。我对编程还很陌生,我知道我最终会想出来,但我已经花了6个小时的时间了,需要继续前进。感谢您的时间。 – BattlFrog 2013-04-30 19:48:07