jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Getting a certain value from Json, inside a jason..
This topic contains 8 replies, has 2 voices, and was last updated by Programmerswe 9 years, 9 months ago.
-
Author
-
Hi!
Im currently filling my grid:var FillDataGrid = function () {
var source = {
datatype: “json”,
datafields: [
{name: ‘Customer’, type: ‘string’},
{name: ‘Name’, type: ‘string’},
{name: ‘json’, type: ‘string’}
],url: subPath + ‘xx’
};This works fine, but lets sat that the json field, contains another json structure and displays this in the grid cell:
{‘Comment’: ‘Hello World!’}’How can i choose, in the best way to just display “Hello World!” ?
CheersHello Programmerswe,
You can use the mapping functionality of the dataAdapter for this.
example:datafields: [ {name: 'Customer', type: 'string'}, {name: 'Name', type: 'string'}, {name: 'json', type: 'string', map: "json>comment"} ],
You can read more about it in the dataAdapter documentation.
Best Regards,
VladimirjQWidgets Team
http://www.jqwidgets.comTried it, when i use map: “json>Comment”, nothing is shown.
Here is a fiddle to see it in action:
Best Regards,
VladimirjQWidgets Team
http://www.jqwidgets.comNotice a problem for me. My return is similar like your data. But with extra ” that needs to be removed somehow.
Comment”:”{‘Comment’: ’Hello World!’}” should be “Comment”:{‘Comment’: ’Hello World!’}
Know any way to make that happened?
Ps: Thanks for the response!Hello Programmerswe,
Ideally you would want to get your data in correct JSON format, and not as a string. So if can modify the server output that would be the desired way to go.
However if you really need to parse the string, you can try doing it the loadComplete function of the dataAdapter.
You can look at this fiddle for an example of how it could be done.Note however that for the JSON.parse() function used in the example to work, the string needs to be formatted with the double quotes used for the inner values.
'Comment':'{"Comment": "Hello World!"}'
Otherwise you will need to parse it yourself and set the correct data to the appropriate record field.
You can try using replace for that e.g.:
records[i].json = JSON.parse(originalRecords[i].json.replace(/\'/g,'"')).Comment;
Also note that in case you need to send back data to the server (e.g. comment editing) you may need to write quite complex logic to return the same format.
I hope that helped you.
Best Regards,
VladimirjQWidgets Team
http://www.jqwidgets.comCan you link the right fiddle please? I think you might sent the wrong one.
Hello Programmerswe,
I am really sorry, I have missed to share it after I forked it.
You should be able to see it now.Best Regards,
VladimirjQWidgets Team
http://www.jqwidgets.comThanks. Decided to just send it as a normal string instead for now.
Might implement this later on!
Thank you Vladimir! -
AuthorPosts
You must be logged in to reply to this topic.