2016-09-28 112 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

(类别字段)

相关问题