0

[![enter image description here][1]][1]i have a nested table , I want to having button to generate the row data to info.php by post method (looks like info.php?user = data[0] & key2 = data2) in one column for each row , I have one button but I need one button and perform some MySql when they are clicked to get the row data . when click the button will get to every columns data in the row and post these data to info.php and view in popup window,

How can I perform post the row data in the nested datatable to other php using the button?

my code

click the button ,cannot get the row data ?

$('#example tbody').on( 'click', 'button', function () {
     var index = $(this).closest('tr').index();
     var data = table.row( $(this).parents('tr') ).data();
      alert("References is "+ data[0] +"and section is "+ data[ 1 ]+ " and Stature Titles is "+data[2] );
} );
2
  • yeah. if you could post your html and what your have tried so far, that would be great :) Commented Oct 24, 2016 at 15:20
  • i have add part of my code Commented Oct 25, 2016 at 1:06

1 Answer 1

3

-UPDATED
just add class for button class='button-info'

 columns:[ 
   { data:'name' },
   { data:'position' }, 
   { data:'salary' },
   {
      "targets": -1,
       "data": null,
      "defaultContent": "<button class='button-info'>Click Me!</button>"
   }
 ]

first assign index value for every parent row

$("table tbody tr").each(function(index) {
    $(this).attr('index', index);
})

then add new event for click event of that button and get the parent tr index just get the index of your selected parent row using data attribute "index" added above

 var parent = $(this).closest('table').parents('tr').index();
 var parentIndex = $('tbody tr:nth-child('+(parent)+')').attr('index');

and to get your current row in nested data

var index = $(this).closest('tr').index();

so this is the final

$('table').on( 'click', 'td .button-info', function () {
    var parent = $(this).closest('table').parents('tr').index();
    var parentIndex = $('tbody tr:nth-child('+(parent)+')').attr('index');
    var currentIndex = $(this).closest('tr').index();
    var data = sections[parentIndex][currentIndex];
    console.log(data);
    return;
    window.open("/info.php?name=" + data.name + "&sal=" + data.salary);
} );

See this updated JSFiddle

Sign up to request clarification or add additional context in comments.

25 Comments

hi Newbee Dev, from your code ,it's get the parent data not nested datatable data, I want to get nested datatable data not parent data , I have update my code ,but still cannot work ,can you help me how to fix ?
ah okay. my mistake
@user6556472 it's okay now check it
yes, alert data is that I wnat , but how to post these data to popup info.php like info.php?name = data.name & sal = data.salary when click the button ?
@user6556472 you want to redirect to that page?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.