How do I send the row ID as a separate value in the top level?

How do I send the row ID as a separate value in the top level?

hunterbunterhunterbunter Posts: 2Questions: 1Answers: 0
edited October 2017 in Editor

I'm having trouble reading in the query string with the way that datatables editor sends the AJAX back. It sends the rowID in the key itself, but I need to know in advance what keys are being sent (using Go).

This is what is being sent back:

{ 
    action = edit
    data[1][email] = emailText
    data[1][name] = nameText
}

I can get it to send the ID inside the data[1][id] structure, but that's not what I want. I want to return the ID like this:

{
    action = edit
    id = 1
    data[1][email] = emailText
    data[1][name] = nameText
}

If I can get it like this I can at least use that id field to construct the proper keys for the rest.

Below is where I'm up to. I've figured out how to put the data in, but I don't know how to get the row's id:

email_editor = new $.fn.dataTable.Editor({
        ajax: {
            edit: {
                type: "POST",
                url: "/dataAPI/emailEdit",
                data: function(d){
                    d.id = ???; // what do I put here to get the id for this row?
                }
                
            }
        },
        table: "#email_table",
        fields: [
        {
            label: "Name",
            name: "Name"
        },{
            label:"Email",
            name: "Email"           
        },{
            label: "Password",
            name: "Password",
        }]
    });

Answers

  • hunterbunterhunterbunter Posts: 2Questions: 1Answers: 0
    edited October 2017

    Ok I solved it but it's not a great solution.

    Basically I created a global variable selected_row, then used this code:

    var table = $('#email_table').DataTable();
         
        $('#email_table tbody').on( 'click', 'tr', function () {
            selected_row = table.row( this ).data().DT_RowId; 
        } );
    

    to update the global whenever I clicked a row. I then edited the ajax call to:

     edit: {
                    type: "POST",
                    url: "/dataAPI/emailEdit",
                    data: function(d){
                        d.id = selected_row;
                    }
                }
    
    

    it'd be nice if there was an internal reference, as I can see this being buggy.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Editor has a legacy client/server protocol which will submit the id at the top level. You can enable that with the legacyAjax option.

    The downside is that you can't use multi-row editing since that structure doesn't allow for multiple rows of data.

    Allan

  • outianleizoutianleiz Posts: 1Questions: 0Answers: 0

    why you guys remove id from request. It is the most useful column. And can you tell me how to set the legacyAjax to enable that.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The information for the id is still available - its just one of the object keys, so you can loop over the data object getting the key names to get the primary key. This change was required in order to support multi-row editing where multiple keys can be submitted. Sending a single id wouldn't have been sufficient.

    The documentation for legacyAjax contains an example near the bottom of the page showing how to set that property if you need it. Keep in mind that it will be removed in v2 (whenever that happens!). If you can, I'd suggest using the newer style.

    Allan

This discussion has been closed.