admin管理员组文章数量:1405630
Could someone help me figure out how to get jsGrid to load my data (shown within the "Data:" section). I'm using the "pageLoading" option but it would appear the controller is not wanting to recognize my data.
Do I need to pull out the "data" from the response? If so, how would I do that?
jsGrid Data Format:
{
data: [ { ..first item ...}, { ..second item..}, ...],
itemsCount: n
}
Data:
[{"ROW_NUMBER":1,"data":"[{\"data\":[{\"STUDENTPIDM\":1,\"STUDENTID\":111,\"STUDENTNAME\":\"Presley, Elvis\",\"AIDYEAR\":\"2425\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Wish he was still here\"},{\"STUDENTPIDM\":2,\"STUDENTID\":222,\"STUDENTNAME\":\"Monroe, Marilyn\",\"AIDYEAR\":\"2324\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Really miss her.\"},{\"STUDENTPIDM\":3,\"STUDENTID\":333,\"STUDENTNAME\":\"Dean, James\",\"AIDYEAR\":\"2425\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Cool Actor\"}],\"itemcount\":\"3\"}]"}]
Controller Code:
controller: {
loadData: function (filter) {
return fetch("/internal/dataPackage.XXXX")
.then(response => response.json())
.catch((error) => {
toastr["error"]("Something went wrong.", "Error");
});
Error:
Uncaught (in promise) TypeError: this.data is undefined
Could someone help me figure out how to get jsGrid to load my data (shown within the "Data:" section). I'm using the "pageLoading" option but it would appear the controller is not wanting to recognize my data.
Do I need to pull out the "data" from the response? If so, how would I do that?
jsGrid Data Format:
{
data: [ { ..first item ...}, { ..second item..}, ...],
itemsCount: n
}
Data:
[{"ROW_NUMBER":1,"data":"[{\"data\":[{\"STUDENTPIDM\":1,\"STUDENTID\":111,\"STUDENTNAME\":\"Presley, Elvis\",\"AIDYEAR\":\"2425\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Wish he was still here\"},{\"STUDENTPIDM\":2,\"STUDENTID\":222,\"STUDENTNAME\":\"Monroe, Marilyn\",\"AIDYEAR\":\"2324\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Really miss her.\"},{\"STUDENTPIDM\":3,\"STUDENTID\":333,\"STUDENTNAME\":\"Dean, James\",\"AIDYEAR\":\"2425\",\"CATEGORY\":\"Deceased\",\"CATEGORIES\":null,\"COMMENTS\":\"Cool Actor\"}],\"itemcount\":\"3\"}]"}]
Controller Code:
controller: {
loadData: function (filter) {
return fetch("/internal/dataPackage.XXXX")
.then(response => response.json())
.catch((error) => {
toastr["error"]("Something went wrong.", "Error");
});
Error:
Uncaught (in promise) TypeError: this.data is undefined
Share Improve this question asked Mar 7 at 21:05 user3685388user3685388 337 bronze badges1 Answer
Reset to default 1From what I see, jsGrid expects to receive data in the format of an object -> keys, where the key data should hold an array. The data returned by the API is a string. By calling response.json()
, we parse the first level of nesting — the first string, but not the nested string inside the data
key. So, the result of response.json()
will be an array, inside which there will be an object, and this object will have two keys: "ROW_NUMBER"
and "data"
. The data should be an object, but since .json()
only worked for the top level and not the nested properties, the value of the data key is a string. We need it to be an object, so it needs to be parsed.
let parsedData = JSON.parse(result[0].data);
Since result is an array, we must access its data by the [0]
key, even though the array has only one element. The value at the .data
key is a string, which we want to parse into an object.
Since JSON.parse(result[0].data)
also doesn't return a simple JS object but an array containing an object with data, we also apply [0]
to the parsed result (JSON.parse(result[0].data)[0]
), which gives us the JS object with the data
and itemcount
keys directly into the parsedData
variable.
After that, data will become an array, but itemcount
will still be a string. Therefore, to convert it to a number, we use the parseInt()
function.
return fetch("/internal/dataPackage.XXXX")
.then(response => response.json())
.then(result => {
let parsedData = JSON.parse(result[0].data)[0];
return {
data: parsedData.data,
itemsCount: parseInt(parsedData.itemcount, 10)
};
})
.catch((error) => {
toastr["error"]("Something went wrong.", "Error");
});
EDIT:
if anyone wants to see the implementation you can find it here: https://codepen.io/Neriday/pen/azbyBoy
If anyone wants to read more about how everything works here, you can go to the discussion below, where the author of the question and I discussed everything in detail :)
本文标签: javascriptjsGrid Loading Data using Option quotpageLoadingquotStack Overflow
版权声明:本文标题:javascript - jsGrid Loading Data using Option "pageLoading" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744909563a2631830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论