using row().data()

using row().data()

chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1
edited December 2018 in Free community support

This youTube video I created summarizes my problem step by step: https://youtu.be/vKKMfBudoqc
I cannot figure out how to retrieve a specific row data using row().data() which is this link: https://datatables.net/reference/api/row().data()

Link to project: https://databasetable-net.000webhostapp.com/

"Working Code":

<script type="text/javascript"> 
$(document).on('click','.edit_btn',function (){
      var id = $(this).attr("id").match(/\d+/)[0];
        var edit_id = $('#example').DataTable().row( id ).data();
        var edit_id = edit_id[0];
    $.ajax({
         url: 'index.php',
          datatype: "json",
      data: { edit_id : edit_id },
      async: false,
        success: function(result) {
              //alert(edit_id);
                  $("#edit_id").val(edit_id);
        } //success func
    }); //ajax
}); //end ready
</script>

What I tried
I tried many things as in the youTube video. I could not get the original example to work on the link.

Should I use Jquery like this??
var last_name = $('#example').DataTable().row( last_name ).data();

Or php like this?
$('#editLastName').val( <?php echo $row['first_name']; ?> );

Or something within the ajax file?

I made a post on stackoverflow, but no one had any insight at all (which did not surprise me). Hoping kthorngren or someone sees this! Thanks.

Answers

  • chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1

    also, do i need the s in this? data-id='s-"0"' Someone on stackoverflow said to delete the 's'. the code "worked" both ways (with or without it).

  • chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1
    edited December 2018

    ****SPOILER****
    ANSWER IS BELOW!!
    I figured it out.

        $(document).on('click', '.edit_btn', function() {
          var rowData = $('#example').DataTable().row($(this).parents('tr')).data();
        });
    

    FULL ANSWER HERE:

    <script type="text/javascript">
    $(document).on('click', '.edit_btn', function() {
      var rowData = $('#example').DataTable().row($(this).parents('tr')).data();
      $('#editFirstName').val(rowData[1]);
      $('#editLastName').val(rowData[2]);
      $('#editZip').val(rowData[3]);
      //alert(rowData);
    });
    
  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Thanks for posting back with your solution.

    Allan

This discussion has been closed.