2016-09-28 79 views
0

我有一個劍道電網,目前我們創建作爲數據源:我們可以在Kendo Grid中讀取複雜對象嗎?

var gridDataSource = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: '@Url.Action("GetData", "Report")', 
       dataType: "json" 
      } 
     }, 
     group: [ 
      { field: "Name" }, 
     ], 
     pageSize: 20, 
     serverPaging: false, 
     serverFiltering: false, 
     sort: { field: "Date", dir: "desc" }, 
     schema: { 
      ...... 
      model: { 
       fields: { 
        Date: { type: "date" }, 
        Name: { type: "string" }, 
        Version: { type: "string" }, 
        Count: { type: "number" } 
       } 
      } 
     } 
    }); 

它調用的GetData報告中獲取數據。返回的數據是具有日期,名稱,版本和計數的類ReportRow的列表。

在後端,這個報告將保存在DocumentDB,該做的GetData是:

public async Task<ContentResult> GetData() 
{ 
    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    string json = string.Empty; 
    try 
    { 
     // this json has the time stamp attribute and Rows (array, which is what the grid shows) 
     json = await _reportRepository.FindDocumentByQueryStringAsync("....."); 
     if (!string.IsNullOrEmpty(json)) 
     { 
      // convert the json 
      .... 

      List<ReportRow> rowList = new List<ReportRow>(); 
      ... 
      { 
       // parse the json, get results 
       rowList = reportResults.Rows; 
      } 
      ... 

      string ojson = serializer.Serialize(rowList); 
      return this.Content(ojson, "application/json"); 
     } 
    } 
    catch (Exception ex) 
    { 
     _log.LogError("..."); 
    } 
    return this.Content(json, "application/json"); 
} 

當我們表現出的報告,我們要顯示在報表的時間戳記。我的想法是:我們可以在_reportRepository中創建另一個方法來返回報告時間戳,在控制器中調用它並傳遞給視圖。但同事問道:既然我們在GetData中得到了時間戳,是否有一種方法可以使用它,這樣我們就不必更改_reportRepository並再次打電話了?

所以,我的問題是:如果在GetData中,我們直接將json傳遞給Kendo網格,我應該如何在gridDataSource中進行更改?我們如何在模式中定義模型?或者,劍道的模型必須是簡單的類型,即字符串,數字,日期......?如果我們能做到這一點,我應該在閱讀事件中改變什麼?

我搜索了,但找不到答案。

感謝

+0

讓我看看我是否明白你的意思:你想獲得並使用'read'請求中的信息作爲其他目的?如果是的話,我知道你是如何做到的。 – DontVoteMeDown

+0

可否請您詳細說明您的場景或提供一些截圖以瞭解需求? –

回答

1

我的理解是,有將在服務器的響應單一時間戳信息,即該信息不會每個網格行顯示。在這種情況下,您的方案將需要使用的schema.data,類似於第二個例子在這裏:

http://docs.telerik.com/kendo-ui/framework/datasource/crud#read-remote

的服務器響應將是這樣的:

{ 
    "yourTimeStamp": "foo", 
    "itemCount": 10, 
    "items": [{ 
     "ProductID": 1, 
     "ProductName": "Bananas" 
    },{ 
     "ProductID": 2, 
     "ProductName": "Pijamas" 
    }] 
} 

數據源配置應該看看像這樣(注意schema.data部分):

var dataSource = new kendo.data.DataSource({ 
    transport: { 
     read: { 
      url: "url" 
     } 
    }, 
    schema: { 
     data: "items", 
     total: "itemCount" 
    } 
}); 

最後的任務是提取時間戳的Infor因爲它將在網格行之外使用。這可以通過兩種方式來完成:

在一個側面說明,網格數據項也可以包含與對象嵌套字段,如下所示:

http://demos.telerik.com/kendo-ui/grid/editing-custom

(類別字段)

相關問題