我们可以使用 OData 客户端为 syncfusion 网格创建我们的请求,但使用正常响应(Reqular WebAPI)

Can we create our request with OData client for syncfusion grid but use normal response (Reqular WebAPI)

我在使用 DataManager 和 OData4Adaptor 消耗响应时遇到问题,Syncfusion 是否存在某种限制,它只消耗来自 OData WebApi 的数据,而不是普通的 WebAPI。

下面正在运行:

Api 回复:

{
    "@odata.context": "http://localhost:32097/odata/$metadata#Movies",
    "@odata.count": 30,
    "value": [{
        "Id": 1,
        "Title": "StarWars - The Force Awakens",
        "ReleaseDate": "2015-10-25T00:00:00+05:30",
        "Rating": "FiveStar",
        "LastModifiedOn": "2017-12-20T16:43:14.3413207+05:30"
    }]
}

以下无效:

Api 回复:

{
    "count": 15,
    "message": "",
    "value": [{
        "code": "asdf",
        "description": "Test ASDF",
        "createdBy": "SA        ",
        "createdDate": "2017-12-13T06:53:30.183"
    }]
}

ODataV4Adaptor 需要标准 OData V4 格式的响应。要使用 ODataV4Adaptor 接受其他响应格式,您可以通过扩展 ODataV4Adaptor 来编写自定义适配器。

import { DataManager, Query, ODataV4Adaptor } from '@syncfusion/ej2-data';

class CustomODataAdaptor extends ODataV4Adaptor {
      processResponse(data: Object, dataManager: DataManager, query: Query) {
       /**
        * Return the result in the { result, count } pair when query has requiresCount() enabled.
        * else simply return the entity collection.
        *  Response structure:
        *  {  
        *   "count": 15,
        *   "value": [{}, {}.....]
        *    }
        */
       return query.isCountRequired ? { result: data['value'], count: data['count'] } : data['value'];
   }
}

//Assign the custom adaptor as follows
new DataManager({ url: SERVICE_URI, adaptor: new CustomODataAdaptor })
   .executeQuery(new Query().take(8).requiresCount()).then((e) => {
  // e.result will hold the result
});

现在 DataManager 可以接受来自非 OData WebAPI 的响应。

有关 DataManager 中更多可用的适配器,请查看以下帮助链接。

数据适配器http://ej2.syncfusion.com/15.4.17/documentation/data/adaptors.html 正在编写自定义适配器http://ej2.syncfusion.com/15.4.17/documentation/data/adaptors.html?lang=typescript#writing-custom-adaptor