Editor fields data problem
Editor fields data problem
Hello,
I'm facing a small problem with the fields object when initializing the editor.
dataEditor = new editor({
ajax: "/endpoint",
table: '#' + targetDiv + "-table",
idSrc: "0",
fields
});
Atm fields are like this:
name: 0,
type:text
name: 1,
type:text
name: 2,
type:text
This is due to the fact that I initialize the table this way:
let options = {
buttons: parsedButtons,
data: parsedRows,
columns: [0:{"title" : "firstname"}, 1:{"title": "lastname"} 2:{"title": "description"}],
.....
}
$('#' + targetDiv + "-table").removeAttr('width').DataTable(options);
I do this because my data has the following format:
{
"title" : "DATATABLE",
"filter" : true,
"columns" : [ {
"editable" : false,
"type" : "text",
"title" : "firstname",
}, {
"editable" : false,
"type" : "text",
"title" : "lastname",
}, {
"editable" : false,
"type" : "text",
"title" : "description",
}],
"rows" : [ {
"row" : [ {
"text" : "Allan",
"color" : null,
"backgroundColor" : "#FFF"
}, {
"text" : "Jardine",
"color" : "#000",
"backgroundColor" : "#FFF"
}, {
"text" : "Business Owner",
"color" : "#000",
"backgroundColor" : "#FFF"
} ]
}
But what I want to do is use names instead of numbers for the fields.
name: firstname,
type:text
name: lastname,
type:text
name: description,
type:text
And if I do this, I get the following error:
Uncaught Unable to automatically determine field from source. Please specify the field name. For more information, please refer to https://datatables.net/tn/11
I have also tried to add name parameter to columns but with no success.
Is there any easy way to achieve this?
Thanks in advance!
Answers
Can you show me your Editor and DataTables initialisation for when you get that error message please? It means that the Editor
field.name
property doesn't match a DataTablescolumns.data
property.The data for the row in an array - you could use
data: '0.text'
to get the data from array index 0 -> object propertytext
. And you could use Editor configured the same way.But I'd be really careful about using arrays like that. Adding a new entry or even worse, adding one in the middle of the array, would really mess things up.
Allan
This is the initialisation when I get the error:
If I try to add data to columns:
I'm getting the following warning:
I've got it!
Based on your sugestion, I changed the "data" from datatable generator to:
{0: [{"firstname" : "Allan"}, "lastname": "Jardine"}, {"description" : "Business owner"}], ...}