row.add not work

row.add not work

Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

I have the following code:

success: function(msg) {
  var dat = $.parseJSON(msg);
  console.log(dat);
    if(dat.msg == '1'){
    table.row.add( [
        dat.data.id_documento,
        dat.data.identificador,
        dat.data.nombre_documento,
        dat.data.promotor,
        dat.data.estado,
        dat.data.accion
    ] ).draw( false );
    console.log(dat.data.id_documento);

where console print value -> 19.
DataTables warning: table id=datatable - Requested unknown parameter 'id_documento' for row 18, column 0.
i have no idea why that warning.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    IIRC your Datatables is using object based data, ie, you used columns.data. You are trying to add an array not an object. The format for row.add() needs to be the same as your original data.

    Kevin

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Yes, I use columns.data,

          var table = $('#datatable').DataTable({
            "autoWidth": false,
            "order": [[ 0, "desc" ]],
            "processing": true,
            "ajax": {
              "url":"<?php echo base_url() ?>documentos/getDocExpAjax",
              "type": "POST",
              "data": {
                id_expediente: $("#expedientes").val()
              },
            },
            "columns": [
              { "data": "id_documento" },
              { "data": "identificador" },
              { "data": "nombre_documento" },
              { "data": "promotor" },
              { "data": "estado"},
              { "data": "accion"}
            ],
            "language":
              {
                "url": "<?php echo base_url('vendors/datatables.net/i18n/Spanish.json') ?>"
              },
          });
    

    the data is similar in columns:
    {"msg":"1","data":{"id_documento":"21","identificador":"asdasfd","nombre_documento":"asdfasdf","promotor":"Wilmer Hilaquita","estado":"Activo","accion":"moredata"}}

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    Answer ✓

    {"msg":"1","data":{"id_documento":"21","identificador":"asdasfd","nombre_documento":"asdfasdf","promotor":"Wilmer Hilaquita","estado":"Activo","accion":"moredata"}}

    You should be able to simply use:

        table.row.add( dat.data ).draw( false );
    

    This should result in {"id_documento":"21","identificador":"asdasfd","nombre_documento":"asdfasdf","promotor":"Wilmer Hilaquita","estado":"Activo","accion":"moredata"} being added.

    Kevin

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Thanks, it works fine, but I have a question, I previously loaded the data using table.row.add([dat.data.column1,dat.data.column2,...]).draw(false);

    why now it does not work ?, if I could clarify this doubt please.
    Or if you have a link to verify the reference.

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    Answer ✓

    This doc explains the use of arrays and objects with datatables.

    Thanks, it works fine, but I have a question, I previously loaded the data using table.row.add([dat.data.column1,dat.data.column2,...]).draw(false);

    When this worked did you have columns.data defined?

    Kevin

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Had not defined columns.data.

This discussion has been closed.