Editor fields data problem

Editor fields data problem

Luis EsnaolaLuis Esnaola Posts: 5Questions: 2Answers: 0
edited January 2019 in Priority support

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

  • allanallan Posts: 63,488Questions: 1Answers: 10,467 Site admin

    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 DataTables columns.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 property text. 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

  • Luis EsnaolaLuis Esnaola Posts: 5Questions: 2Answers: 0
    edited January 2019

    This is the initialisation when I get the error:

    $('#' + targetDiv + "-table").DataTable({
        buttons: parsedButtons,
        data: {0: ["Allan", "Jardine", "Business owner"], 1: ["John", "Doe", "Lorem Ipsum"]},
        columns: [
          {"name" : "firstname", "title": "First Name"}, 
          {"name": "lastname", "title": "Last Name"}, 
          {"name": "description", "title": "Description"}]
        .....
    });
    
    
    dataEditor = new editor({
           ajax:   "/endpoint",
           table: '#' + targetDiv + "-table",
           idSrc: "0",
           fields :[ 
               {name: "firstname", type:"text"}, 
               {name: "lastname", type:"text"}, 
               {name: "description", type:"text"}]
       });
    

    If I try to add data to columns:

    columns: [
     {"name" : "firstname", "title": "First Name", "data" : "firstname"}, 
     {"name": "lastname", "title": "Last Name", "data" : "lastname"}, 
     {"name": "description", "title": "Description", "data" : "description"}]
    

    I'm getting the following warning:

    DataTables warning: table id=contenido-table - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

  • Luis EsnaolaLuis Esnaola Posts: 5Questions: 2Answers: 0

    I've got it!

    Based on your sugestion, I changed the "data" from datatable generator to:

    {0: [{"firstname" : "Allan"}, "lastname": "Jardine"}, {"description" : "Business owner"}], ...}

This discussion has been closed.