Unknown parameter when updating using Editor

Unknown parameter when updating using Editor

lthammlthamm Posts: 13Questions: 2Answers: 0
edited March 2020 in Free community support

This is my setup:

Editor:
var editor = new $.fn.dataTable.Editor({
      ajax: {
             url: „editData“,
             type: 'GET',
      },
      table: '#result-table',
      idSrc: 1, 
      fields: [{
        label: ’Stock’,
        name: ‚stock‘
      }
    });

Datatable with Ajax-Data outside datable constructor:

$.ajax({
        url: „getData“,
        type: "GET",
        data: form_data
      }).done(function(response) { 

          var table = $("#result-table").DataTable({
            "bDestroy": true,
            dom: 'BRlfrtip',
            data: response.data,
            columns: response.columns,
            keys: true,
            select: true,
            rowId: 1,
    ….
}

idSrc and rowID both accept an integer, but not a string („id“) - which would be the matching column in the JSON. Using „id“ instead of the column index results in no effect.
With the column index the id is correctly set.

The server’s json response looks like this:

{
  "columns": [
    {
      "title": „name“
    },
    {
      "title": „id“
    }
    ….
  ],

  "data": [
    [
      „John“,
      „23423JDH“,
      null,
      107,
      null,
      "nan",
      0,
      27.0
    ],
..
}

In case it matters, right now I am only using mock data for return during editData and I only try to edit the "John" row. This is how the response looks

{
  "data": [
    {
      "name": "John",
      "id": "23423JDH",
  ...
      "stock": 107,
...
    }
  ]
}

When I try to update a row I get this error:

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

The debugger shows no errors and confirms I am using up to date sources.
Any help on this is greatly appreciated!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Hi,

    So the issue here is that you are returning a different data structure from the Editor Ajax data array compared to the DataTables one. The Editor data array contains objects, while your DataTables initial data contains arrays. They need to match.

    So your Editor one might be:

    {
      "data": [
        [
          "John",
          "23423JDH",
          ...
          107,
          ...
        ]
      ]
    }
    

    Allan

  • lthammlthamm Posts: 13Questions: 2Answers: 0

    Hi Allan,

    this also explains why I need to reference my columns via index and not via name. Now your solution is kind of obvious but I've thought that Datatables would merge data and column names internally.

    This helped me a lot!

    All the best
    Lennart

This discussion has been closed.