Estoy tratando de obtener datos seleccionados de DataTable, pero no puedo recuperar los datos; sin embargo, pude recuperar el número de índice de la fila. Cualquiera puede ayudarme a obtener los datos de la fila seleccionada. Proporcionaré un violín de trabajo para un trabajo más fácil.
$(document).ready(function() { var table = $('#example').DataTable({ 'ajax': 'https://gyrocode.github.io/files/jquery-datatables/arrays_id.json', 'columnDefs': [ { 'targets': 0, 'checkboxes': { 'selectRow': true } } ], 'select': { 'style': 'multi' }, 'order': [[1, 'asc']] }); // Handle form submission event $('#frm-example').on('submit', function(e){ var form = this; var rows_selected = table.column(0).checkboxes.selected(); // Iterate over all selected checkboxes $.each(rows_selected, function(index, rowId){ // Create a hidden element $(form).append( $('<input>') .attr('type', 'hidden') .attr('name', 'id[]') .val(rowId) ); }); // FOR DEMONSTRATION ONLY // The code below is not needed in production // Output form data to a console $('#example-console-rows').text(rows_selected.join(",")); // Output form data to a console $('#example-console-form').text($(form).serialize()); // Remove added elements $('input[name="id\[\]"]', form).remove(); // Prevent actual form submission e.preventDefault(); }); });Simplemente podría usar, dentro de su envío:
let selectedValues = table.rows( { selected: true } ).data();Y luego recorre tus valores.