The function in replacements of ajax wont be fired

The function in replacements of ajax wont be fired

ericyuhericyuh Posts: 1Questions: 1Answers: 0

Hi all, excuse me.

I'll appreciated if someone can give me advise.

I cannot get the current DT_RowID after I click the update butten. my api has retrieved the ajax data from the editor, but I still need to process before the ajax data send.

Thanks!

editorList = new $.fn.dataTable.Editor({
ajax: {
edit: {
url: MyApiUrl,
headers: {
Authorization: 'Bearer ' + jsApiToken
},
contentType: "application/json",
method: 'put',
replacements: {
controller: function (key, id, action, data) {
console.log('this function won't be fired after the update button has been click);
return id;
}
},
data: function (d) {
console.log('need to get the DT_RowID before send the ajax data');
return JSON.stringify(d);
}
}
}

Answers

  • rf1234rf1234 Posts: 2,981Questions: 87Answers: 421
    edited September 2022

    If you use the "select" extension you can get the id of the row selected for editing.

    https://datatables.net/extensions/select/

    Using that you can do something like this:

    var rowId; //as a global variable for example
    
    table
       .on ( 'select', function (e, dt, tp, indexes) {
           var selected = table.row( {selected: true} );
           if ( selected.any() ) {
               rowId = selected.data().DT_RowId;
           }
        });
    

    You can also retrieve the rowId on "preSubmit".

    var rowId; //as a global variable for example
    
    editor.
        .on( 'preSubmit', function ( e, d, action ) {
            rowId = Object.keys(d.data)[0];; //row_xxx (edit) or 0 (create)
        });
    
Sign In or Register to comment.