问题
My JSON store (django rest framework) returns keys for "count", "next", "previous", and "results".
"count" is the number of rows available.
"next" is the url for the next page of results (e.g. ids 26-50).
"previous" is the url for the previous page of results (null in this case since this is the first page of results).
The "results" key contains the actual data objects I'd like to display in the OnDemandGrid.
How do I connect the "results" key data collection to the grid? Thank you for your help in advance.
The returned JSON ( collection: new Rest({target: '/api/events'?format=json'),}) ) looks like this:
{
"count":1411,
"next":"http://localhost/api/events/?format=json&page=2",
"previous":null,
"results": [
{"id":1,"event_type":"02","event_at":"2015-03-31T12:53:41Z","machine_id":1,"revs":4342,"color":5,"heads_info":"using http","tag":1,"hidden":false},
{"id":2,"event_type":"02","event_at":"2015-03-31T12:53:41Z","machine_id":1,"revs":4342,"color":5,"heads_info":"using http","tag":1,"hidden":false},
...
{"id":25,"event_type":"02","event_at":"2015-03-31T12:54:01Z","machine_id":1,"revs":4342,"color":5,"heads_info":"using http","tag":1,"hidden":false},
]
}
回答1:
It looks like you're trying to use this service with dstore/Rest
, but that has some specific expectations of the server request and response:
- The response must report the list of items in one of the following ways:
- Respond with a top-level array of items
- Respond with an object with an
items
property whose value is an array of items
- The response must report the total number of items in one of the following ways:
- If the response is an object, you may include a
total
property in the object - Otherwise, you must include a
Content-Range
header (in the formatX-Y/Z items
, where theZ
is of primary importance)
- If the response is an object, you may include a
- The request must support being informed of the range of items it should request out of the total result set via one of the following mechanisms:
- start and count GET parameters (specified to the store instance via
rangeStartParam
andrangeCountParam
) - Range headers (by setting
useRangeHeaders: true
) - Otherwise, by default, the store will pass a
limit
GET parameter in the formatlimit(count,start)
(or justlimit(count)
ifstart
is 0)
- start and count GET parameters (specified to the store instance via
If your server can't fulfill these requirements (e.g. it sounds like it has quite different requirements regarding ranging, since it uses pages instead), you're going to need to think about extending/implementing a custom store.
来源:https://stackoverflow.com/questions/30603306/how-do-i-connect-a-django-rest-framework-json-query-result-to-dgrid-ondemandgrid