2013-06-25 3 views
1

ajax 호출 결과를 사용하여 Kendo Grid를 채울 결정을 내리고 싶습니다. 배열에서 하나의 항목 만 받으면 그리드를 채 웁니다. 그렇지 않으면 여러 항목에 대해 다른 그리드가 있습니다.Ajax 결과로 검도 그리드 채우기

내 jQuery를

$.ajax({ 
     type: "POST", 
     dataType: "json", 
     url: 'Item/GetItems/', 
     data: { number: number }, 
     success: function (data) { 

      if (data.length == 1) { 
       var sGrid = $("#SingleGrid").data("kendoGrid").dataSource.data(data); 

       //I´ve also tried this 
       //sGrid.refresh(); 
      } 
      else { 
       var mGrid = $("#MultipleGrid").data("kendoGrid").dataSource.data(data); 

       //I´ve also tried this 
       //mGrid .refresh(); 
      } 
     }, 
     error: function() { 
     } 
    }); 

내 컨트롤러 액션

public ActionResult GetItems([DataSourceRequest] DataSourceRequest request, string number) 
    { 
     var items = _idg.GetItems(number); 
     return Json(items.ToDataSourceResult(request, ModelState)); 
    } 

I've은 방화범을 모니터링하고 그것은 오류를 보여줍니다. 난 그리드 중 하나를 채우는 결정을함으로써 서버에 두 번째 호출을 방지하기 위해 노력하고있어. 이 클라이언트 쪽과 같은 강제로 dataSource를 새로 고치는 방법이 있습니까? 내 위의 예에서

function TestGrid() { 
    var dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       type: "POST", 
       dataType: "json", 
       url: 'Item/GetItems/' 
      } 
     }, 
     schema: { 
      data: function (response) { 
       // Here the Total is always correct 
       return response.Total; 
      } 
     } 
    }); 

    dataSource.fetch(function() { 
     var kendoGrid; 
     var data = this.data(); 
     //Here the data does not include my Total 
     alert(data); 
     if (data.length == 1) { 
      kendoGrid = $("#SingleGrid").data("kendoGrid"); 
     } else { 
      kendoGrid = $("#MultipleGrid").data("kendoGrid"); 
     } 
     kendoGrid.setDataSource(dataSource); 
     kendoGrid.refresh(); 
    }); 
} 

######## 수정 됨

######합니다 (서버 두 번째 전화를 할 것 인 dataSource에 읽기 함수를 호출하지 않고) this.data()에서 전체 숫자에 도달 할 수 없습니다. Firebug로 디버깅 할 때 총 수는 항상 정확하다는 것을 알 수 있습니다. 어떤 아이디어?

답변

5

검도 데이터 소스의 새 인스턴스를 채우고 데이터를로드 한 다음 결과에 따라 그리드의 데이터 소스를 설정할 수 있습니다. 이 같은 아마 뭔가 :

var dataSource = new kendo.data.DataSource({ 
    transport: { 
    read: { 
     type: "POST", 
     dataType: "json", 
     url: 'Item/GetItems/', 
     data: { number: number }, 
    } 
    } 
}); 

그런 다음 결과에 서버와 행동에서 데이터를 가져 오기는 :

dataSource.fetch(function() { 
    var data = this.data(); 
    var kendoGrid; 
    if (data.length == 1) { 
    kendoGrid = $("#SingleGrid").data("kendoGrid"); 
    } else { 
    kendoGrid = $("#MultipleGrid").data("kendoGrid"); 
    } 
    // Replace the grids data source with our new populated data source 
    kendoGrid.setDataSource(dataSource); 
    kendoGrid.refresh(); 
}); 
+0

@boniestlawyer에 감사드립니다. 제안한대로 구현했지만 그리드는 여전히 데이터를 표시하지 않습니다. Firebug에서 데이터가 컨트롤러에서 전달되었지만 나타나지는 않았 음을 알 수 있습니다. 새 dataSource (total() 함수 사용)의 요소를 계산할 때 dataSource가 비어있는 것처럼 항상 아무 것도받지 못합니다. 아마도 나는 잘못된 함수를 사용하여 dataSource를 계산하거나 아마도 올바른 방식으로 데이터 소스를 채우지 않을 것입니다. 어떤 아이디어? :) – gardarvalur

+0

나는 그것이 브라우저에 내려 오는 데이터와 관련이있을 것이라고 생각합니다. 다음과 같이 kendo 데이터 소스에 schema-> data 함수를 추가해보십시오. http://docs.kendoui.com/api/framework/datasource#configuration-schema 함수를 디버그하여 응답을 검사하십시오. – boniestlawyer

+0

아마도 서버의 데이터가 올바르게 전달되지 않았거나 최소한 데이터 바인딩이 제대로되지 않았다고 제안하는 것 같습니다. dataSource가 항상 컨트롤러에서 DataSourceResult로 데이터를 읽는다 고 가정하는 것이 맞습니까? (내보기와 같이) – gardarvalur

0

당신은이 일을 할 수있다.

$("#grid").kendoGrid({ 
      dataSource: { 
       transport: { 
        read: { 
         url: "http://localhost:13073/home/KendoGriddata", 
         dataType: "json", 
         type: "Post", 
         data: function() { 
          var dt = { MultipleSearchText: 'rikin' }; 
          return dt; 
         } 
        } 
       }, 
       pageSize: 5  
      });