Having last clicked row values as array

Having last clicked row values as array

goktuginal41@gmail.comgoktuginal41@gmail.com Posts: 57Questions: 22Answers: 0

Hello everyone,

I have edit button in each row. I'm trying to keep the values in the row of the edit button I clicked last as an array. But it does not work. When I click first edit button in first row, a modal window appears. I close the the modal window and then click second edit button in second row. Below code gets all data of first and second rows but I only want the last row data. How can I make that happen?

In short, when I click on the first edit button, I keep the data of that row on the appeared modal window, but when I close the that window and click on the edit button of the second row, I see the data of both the first row and the second row on the window. What I want is to see only the information of the row I clicked on.(Maybe it is javascript issue?)

var fakeArray = table.rows().data().toArray();

Answers

  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421

    Are you using the Select extension?
    https://editor.datatables.net/manual/getting-started#DataTables-initialisation

    If so and you are using single select you could do:

    table.row({selected:true}).data().toArray();
    
  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    Use a delegated event handler like this example. You my need to make the selector more specific then get the closest td or tr, for example:

        $('#example tbody').on('click', 'tr .editButton', function () {
            var td = $(this).closest('td');
            var data = table.row( td ).data();
            alert('You clicked on ' + data[0] + "'s row");
        });
    

    var fakeArray = table.rows().data().toArray();

    This will get all the rows in the table.

    Kevin

  • goktuginal41@gmail.comgoktuginal41@gmail.com Posts: 57Questions: 22Answers: 0

    Thank you for the responds. I think it is a bit out of datatables topic but I wanna ask anyway. Let me explain the problem briefly.
    I have edit modal and inside this model I have switch button call new modal. And when I switch this button that new modal window appears with data of the rows(problem is here because the whole data of first and second rows appear).
    In the code below, when I call the alert function, 1 and then, 2 appear on the screen (first and second rows ids). It should have appeared only 2 because the edit button I clicked last is on the second row.

    In short, it enters into the** is_index_sample_edit function** twice even though I click switch button once! I couldn't understand the logic. Sorry it is a bit confusing.

    $("#example").on('click','tr #edit', function() {
                var tr = $(this).closest('tr');
                var data = table.row(tr).data();
                $("#id").val(data.id);
                $("#p_id_s").val(data.p_id_s);
       
                 $("#is_index_sample_edit").on('change', function() { //switch button
                  alert(data.id)
                  if ($(this).is(':checked')) { 
                     $("#editModal").modal('hide');
                     $("#indexsampleEditModal").modal('show');
                     var fakeArray = table.rows().data().toArray();
                     ............
                  }
                  else { 
                  .............
                   }
    }
    
Sign In or Register to comment.