DataTable doesn't refresh the record on update

DataTable doesn't refresh the record on update

VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

Hi overthere!

My poor datatable doesn't want to refresh the record after submitting changes to the server (I return the updated record from server).

I prepared the example on heroku https://datatbl.herokuapp.com/

Is there anybody who could take a look at my code and say what I missed?

PS. I use the latest versions of DataTable, Select and Editor extensions and super-feature idSrc instead of putting DT_RowId in each record.

This question has an accepted answers - jump to answer

Answers

  • btreebtree Posts: 99Questions: 14Answers: 11

    Hi,

    I see you use a .json as source. You need to reload the source when you send the POST Data.

    editor.on( 'create', function ( e, json, data ) {
         table.ajax.reload();
    } );
    

    Cheers
    Hannes

  • VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

    No no no) The basic example http://editor.datatables.net/examples/simple/simple.html works perfectly (as I wanted to) and doesn't reload the table)))

    There is something else what I've missed ....

  • btreebtree Posts: 99Questions: 14Answers: 11
    edited November 2015

    Hi,

    maybe you need to build your POST response different.

    $row = array(id: 3, name: ....);
    $output = array(data =>  row);
    echo json_encode($output);
    

    Cheers
    Hannes

  • VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

    As you can see in console I build the response in proper way i.e.

    {"data":
    {"id":4,
    "name": "Name-3", ....
    }
    }
    The only thing I suspect is that I do not use DT_RowId within each row in favor of idSrc.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    In your DataTables initialisation you have:

    idSrc: 'id',

    however, it should actually be rowId (it is not the same name as Editor uses since it is slightly different in how it works):

    rowId:     'id',
    

    Could you possibly try that and also update to DataTables 1.10.10.

    Those two things I think will resolve the issue.

    Regards,
    Allan

  • VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

    Hi Allan,

    I have replaced idSrc with rowId and upgraded to 1.10.10 ... unfortunately it didn't work (((
    You may see at https://datatbl.herokuapp.com/

    and I've also created one more example using DT_RowId in recordset ... https://datatbl.herokuapp.com/channels ... but the same issue as above.

    I don't understand what I miss)

    Best,
    Vyacheslav

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Thanks for updating - I see what the issue is now. The problem is that the JSON returned by the server is not in the format that Editor expects.

    Specifically, it expects data to be an array, but you currently have it as an object.

    For example:

    {
        "data": {
            "id": 1,
            "name": "Name-0123",
            "last_name": "Last name-01",
            ...
        }
    }
    

    should actually be:

    {
        "data": [
            {
                "id": 1,
                "name": "Name-0123",
                "last_name": "Last name-01",
                ...
            }
        ]
    }
    

    This is required to support Editor's multi-row editing abilities.

    Allan

  • VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

    Oh! Thanks a lot! ... Life goes on!

This discussion has been closed.