Datatables Editor is sendimg [object Object] instead of actual JSON
Datatables Editor is sendimg [object Object] instead of actual JSON
I am not sure if I am overlooking something, but the data I populate my datatables with has some JSON within it. And at initialization of the table the data is parsed and the result is displayed properly. However, when I use editor to edit a field and send to server (I need to send all data for the row), the parts of the data that are JSON are sent as [object Object] and not actual JSON data (The raw data also shows [object Object]).
Is there something I am overlooking in a setting for Editor to send the JSON packet correctly? I do know I can use the individual JSON entries in the editor field descriptor, but it uses multiple parts of the JSON to calculate a response. I was ignoring the problem until now, as I am working with the data instead of just displaying it.
This question has an accepted answers - jump to answer
Answers
Can you show me your Editor initialisation, any events you use from it and ideally a link to the page you are working on please?
Allan
Sorry, I did not think the question would need a code example. Everything works as it should aside from datatables not sending the actual JSON.
assocs and detailData are both JSON data stored in the datatables data array. I can filter, order, etc without an issue, and I checked to make sure the data was unaltered before sending to editor.
Ah I see - it is just specific fields that are the issue, not all of them. I strongly suspect that it is because those fields are JSON data already, but Editor doesn't have a built in JSON editor.
for example is a text field. So if you attempt to put an object into it it will indeed be
[object Object]
since that is thetoString()
of a JSON object. That isn't a problem with the data being sent to the server, but rather with the field.In your original data source is
detailData
a JSON object? I'm presuming so. And you want it to display as JSON for the end user to edit?Allan
I was confusing myself seeing datatables JSON output. The detailData is an object and assocs is an array of objects. I am sorry I was not clear on that point. However, I realize that I do not need to send those fields, so I removed them from the editors list.
Sometime in the past when I picked up this lovely bit of software I remember having to send all the data or the datatable would complain it was missing fields (probably user error since I was learning a new package) when the editor would send the data back, and I never checked if I still had the same issue, being stuck in the old way of thought.
However, is there a way to fix this issue? There may come a time I want to use an object or an array of objects as stored data in a field to be sent to the server. And yes, I do have a field that stores the data as an object that people do edit. However, I handle that code individually and make the edit outside of editor giving the complex nature of that particular field.
And if you are curious on that field, it is for shipping postage/shippers. Allowing the user to select multiple methods of shipping the item and when they are done the field shows them what was selected, and that data is used to populate other cells to display the postage they selected.
If it is a field where the end user can plain text edit the JSON string, then you could use a get and set formatter on the client-side:
fields.getFormatter
andfields.setFormatter
- i.e. toJSON.parse(val)
in the get formatter andJSON.stringify(val)
in the set formatter.However, if the end user can't interact with that field in the Editor form, just drop that field. Editor doesn't need to operate on all fields, just because they are in the data set. It will happily ignore them . Example of that here.
The final option, if you are using a JSON editor and want to embed it into Editor, is to create a custom field type plug-in. It can then process the value (JSON in this case, perhaps of a certain schema) as needed for editing. Editor would just set and get the value from that field type plug-in.
Allan
Thank you for the information.