How to send editor fields as a JSON format.
How to send editor fields as a JSON format.
Madhavi Bhimisetty
Posts: 11Questions: 1Answers: 0
How do I need to send the editor fields as a JSON fromat. While creating,edit the record using editor datatables.
Replies
You don't need to worry about this in case you use Editor on client and server side. That is one of the advantages using Editor.
Just to add to that, if you are writing your own server-side code and want the data in the request body, you can do so by using the
ajax.data
option. The final example on that page shows how it can be done.Allan
In my payload data going like below
data[4][zoneId]: 4
data[4][name]: zone4
data[4][description]: zone3 from file
data[4][status]: Inactvie
action: edit
typeName: Zones
I want like below
zoneId : 4
name: zone4
description: zone3 from file
status : Inactive
action : edit
typeName : Zones
This is my Javascript editor code.
Please help me I am struck here.
editor = new $.fn.dataTable.Editor( {
ajax: {
url: extract_url('/rest/updateType'),
data: { "_csrf": CSRF_TOKEN,typeName : 'Zones'}
},
table: "#zones-search-results",
idSrc: 'zoneId',
dataType: 'json',
fields: [ {
label: "ID :",
name: "zoneId",
type:"hidden",
data :"zoneId"
}, {
label: "Name :",
name: "name",
data :"name"
}, {
label: "Description :",
name: "description",
data :"description"
},
{
label: "Status :",
name: "status",
data : status,
type: "select",
}
]
If you make those changes you cannot use Editor on the server because Editor won't understand the modified format.
In case you still want that you can change the format on "preSubmit" (see the example at the bottom):
https://editor.datatables.net/reference/event/preSubmit
Good luck!
Here is an example from my own coding making modifications to the JSON to be sent to the server:
Thanks for your immediate reply,
What ever you have given example its deleting data . But I want to send the data with key and value pair.
Ex : name: zone4 - I want like this data to send to the server.
Not Like this (data[4][name]: zone4) - Actually Its going like this.
In my payload data going like below
data[4][zoneId]: 4
data[4][name]: zone4
data[4][description]: zone3 from file
data[4][status]: Inactvie
action: edit
typeName: Zones
I want like below
zoneId : 4
name: zone4
description: zone3 from file
status : Inactive
action : edit
typeName : Zones
Just go ahead!
Your question is a pure Javascript question, not a Datatables question. If you don't know how to modify or replace a data object that gets passed into an event handler I would advise you to ask this question on SO.
Thank you,
I resolved the issue.