Editor - after postEdit, problems getting ID to send with ajax call

Editor - after postEdit, problems getting ID to send with ajax call

asleasle Posts: 96Questions: 28Answers: 0

I have a postEdit function that works fine but I cannot get the row ID to send with an ajax call. What am I doing wrong?

// function after edit
editor.on( 'postEdit', function ( e, json, data ) {
    // Below line works fine and returns cells value
    alert( 'Row is edited with data: '  + data.fritekst + ' : ' + data.utedel_varenr);
    var table = $('#example').DataTable();

    // Below alerts "undefined" 
    console.log( table.row( this ).data()); 
    var id = table.row( this ).id();
               alert( 'Clicked row id '+id );
    var idrow= table.row( { selected: true } ).id();

    // Below alerts "NaN"
               alert(+idrow);

     // Below sends data to update.php             
 /*   $.ajax({
        type: "POST",
        url: "update.php",
        data: {
                id: id,
                date: data.fritekst
        }
    }) */
} );

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    Do alert(idrow) without the +.
    The row id isn't numeric I guess, it is "row_1234" or similar. Just take a look and use a substring without the prefix.

  • asleasle Posts: 96Questions: 28Answers: 0
    edited June 2022

    I solved this with adding this to the controller file "staff.php" file:

    ->fields(
            Field::inst( 'id' )->set(false), // ID is automatically set by the database on create......
                    Field::inst( 'aktiv' ) ..........
    

    I notice the json data sent from the server script -> DT_RowId: "row_37" and now with the code above I also get:
    DT_RowId: "row_37", id: "37"

    So now this works and I can send the id:

                var id = (data.id);
                alert( 'Clicked row id:  '+id );
    

    I am learning all the time :-)

  • asleasle Posts: 96Questions: 28Answers: 0
    edited June 2022

    I wonder why I am not able to get the rows id with
    var idrow= table.row( { selected: true } ).id();
    There is no click function on the row but "editor.on" so I thought maybe activating editor (that is what I do when I click the field to edit it?) would let me get the rows ID. Now I have defined a new $.fn.dataTable.Editor() and only on one field do I have inline edit

    columns:[ 
    { data: "fritekst", render: editIcon },.....
    ]
    

    I am ok with using data.id but just wanted to understand when I can/should use the above table.row( { selected: true } ).id()

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    Answer ✓

    The row().id() docs state this:

    This method can be used to get a row's id, as specified by the row's data and the rowId option.

    Are you using the rowId option to set the row ID to rowId: "id"?

    Kevin

  • asleasle Posts: 96Questions: 28Answers: 0

    Sorry, I thought I had answered your comment. I tried the rowID option but that was "undefined". But I got the id from data.id .

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited June 2022 Answer ✓

    The Datatable options are case sensitive. Using rowID won't work. Use rowId instead. I provided a simple example showing the use of rowId when selecting a row:
    http://live.datatables.net/jofojege/1/edit

    Can you post a link to your page or a test case showing that using rowId doesn't work?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

Sign In or Register to comment.