jQuery UI Widgets › Forums › Grid › How to format nested array field
Tagged: angular datagrid, datagrid, datagrid data binding, jqxGrid ;
This topic contains 4 replies, has 3 voices, and was last updated by jqwidgetuser 5 years, 5 months ago.
-
Author
-
The server returns the array of users. Each user has two string fields
IDandemailand one array fieldtasks:users: { ID: "5f31b39eb2b316435a8a91f5", email: 'Elio.Petersen@yahoo.com', tasks: [ { task_name: "Task_A", ID: "5f31b39eb2b316435a8a91f3", }, { task_name: "Task_B", ID: "5f31b39eb2b316435a8a91f3", } ] }I have defined the
datafieldsforjqxgrid:datafields: [ { name: 'ID', type: 'string' }, { name: 'email', type: 'string' }, { name: 'tasks', type: 'array' } ]jqxgridrenders theuserIDandemailcorrectly. But thetaskscolumn cell shows[object Object].
Instead I would like to display theTask_A, Task_Bwhich are values stored in eachtaskintask_nameattribute.Is it possible to achieve? How?
Hello jqwidgetuser,
I would like to mention that we have a lot of similar topics in our forum that could be useful.
Please, take a look at this topic:
https://www.jqwidgets.com/community/topic/nested-json-with-jqxinput/
Also, I would like to suggest you look at this tutorial:
https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxgrid/jquery-grid-datasources.htm
Please, let me know if you still have trouble with this.Best Regards,
Hristo HristovjQWidgets team
https://www.jqwidgets.comThank you for the links!
With the nestedtaskJSON data object attribute storing single dictionary I canmap: 'task>task_name'users: { ID: "5f31b39eb2b316435a8a91f5", email: 'Elio.Petersen@yahoo.com', task: { task_name: "Task_A", ID: "5f31b39eb2b316435a8a91f3", } }datafields: [ { name: 'tasks', map: 'task>task_name' }, ]The JSON object in my case has many
tasksusers: { ID: "5f31b39eb2b316435a8a91f5", email: 'Elio.Petersen@yahoo.com', tasks: [ { task_name: "Task_A", ID: "5f31b39eb2b316435a8a91f3", }, { task_name: "Task_B", ID: "5f31b39eb2b316435a8a91f3", } ] }The
map: 'task>task_name'doesn’t work with nested array fieldtaskshere becausemapreferences to a single task using atask.task_namepath. To deal with the arraytasksI have to use theindexof element in thetasksarray, such astasks[index].task_name.What is the syntax or an approach to make
map:support an array of objects in the nested JSON fieldtasks? Is there a reserved word for theindex?Hi jqwidgetuser,
The DataGrid does not have built-in solution to display this kind of data. It will need to be mutated/transformed before binding it to the DataGrid.
My suggestion is when you receive the data from the server, loop through the data and prepare a local array with the records which you want to display in the DataGrid. After that, bind that local array to the DataGrid.Best regards,
Peter StoevjQWidgets Team
https://www.jqwidgets.com/Thanks for a quick response!
I went ahead and implemented it using
beforeLoadComplete:let users = { ID: "5f31b39eb2b316435a8a91f5", email: 'Elio.Petersen@yahoo.com', tasks: [ { task_name: "Task_A", ID: "5f31b39eb2b316435a8a91f3", }, { task_name: "Task_B", ID: "5f31b39eb2b316435a8a91f3", } ] } source: any = { datatype: 'json', datafields: [ { name: 'ID', type: 'string' }, { name: 'email', type: 'string' }, { name: 'tasks', type: 'array' } ], url: "https://www.mydomain.com/data", cache: false, root: 'Rows', beforeLoadComplete: (data: any, dwnData: any) => { let _data = []; for (let i = 0; i < data.length; i++) { let item = data[i]; let _tasks = []; for (let n = 0; n < item.tasks.length; n++) { _tasks.push(item.tasks[n].task_name); }; item["tasks"] = _tasks; _data.push(item); } return _data; }, } -
AuthorPosts
You must be logged in to reply to this topic.