简体   繁体   中英

Datatable reorder issue

I'm having an issue with the following code. What I'm trying to do is to populate a modal form with data from the row on which you pressed the edit button. The script works fine until I try to reorder the table: when the table is reordered the script is getting the information of the pre-reordered table.

This is the script that get called when the edit button get clicked:

 $(document).ready( function () { //create datatable //declaring a variable without var makes it global table_movimenti = $('#movimenti').DataTable(); $('#movimenti table tbody tr td').on('click', function () { $("#transaction_ID").val($(this).find("td:eq(6)").attr('id')); $("#data_cont").val($(this).find("td:eq(0)").text()); $("#data_valuta").val($(this).find("td:eq(1)").text()); $("#importo").val($(this).find("td:eq(2)").text()); $("#divisa").val($(this).find("td:eq(3)").text()); $("#causale").val($(this).find("td:eq(4)").text()); var categoria=$.trim($(this).find("td:eq(5)").text()); $("#categoria option").each(function() { if($(this).text() == categoria){ //$(this).prop("selected", true); $(this).attr("selected", "selected"); return; } }); }); 

This is the table:

 <?php echo "<table id='movimenti' class='table table-bordered table-striped table no-margin'>"; echo "<thead>"; echo "<tr>"; echo "<th>Data contabile</th>"; echo "<th>Data valuta</th>"; echo "<th>Importo</th>"; echo "<th>Divisa</th>"; echo "<th>Causale</th>"; echo "<th>Categoria</th>"; echo "<th>Edit</th>"; echo "</tr>"; echo "</thead>"; echo "<tbody>"; while($row = mysqli_fetch_array($result)){ echo "<tr class='table_row'>"; echo " <td>" . $row['Data_cont'] . "</td>"; echo " <td>" . $row['Data_valuta'] . "</td>"; echo " <td>" . $row['Importo'] . "</td>"; echo " <td>" . $row['Divisa'] . "</td>"; echo " <td>" . $row['Causale'] . "</td>"; echo " <td>"; foreach ($categories as $value){ if ($value['ID'] == $row['Categoria']){ echo " <span class='label label-success'>"; echo "". $value['Descrizione'] .""; echo "<input type='text' class='label label-success' id='categoria' name='categoria' value='". $value['Descrizione'] ."'/>"; echo "</span></td>"; } } 

In the end I've completely change the approach and now I'm building my table using ajax option in datatable. In this way I'm able to retrieve all the data in the row using the same ajax option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM