Perdonami ma ti spieghi veramente male, detto questo ti riscrivo il codice corretto, anche se la soluzione era abbastanza intuibile visto che è copiato (quasi) pari pari dalla documentazione ufficiale.
ps. Sconsiglio vivamente il tuo tipo di approccio, che va ad inserire i dati json o di altro tipo all'interno del file javascript.
html file
data.jsoncodice:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css"> <title>Document</title> </head> <body> <table id="example" class="display" style="width:100%"> <thead> <tr> <th></th> <th>Name</th> <th>Position</th> <th>Office</th> <th>Salary</th> </tr> </thead> <button id="btn" onclick="test()"> test </button> </table> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script> <script> /* Formatting function for row details - modify as you need */ function format(d) { // `d` is the original data object for the row return ( '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' + '<tr>' + '<td>Full name:</td>' + '<td>' + d.name + '</td>' + '</tr>' + '<tr>' + '<td>Extension number:</td>' + '<td>' + d.extn + '</td>' + '</tr>' + '<tr>' + '<td>Extra info:</td>' + '<td>And any further details here (images etc)...</td>' + '</tr>' + '</table>' ); } function test() { var table = $('#example').DataTable({ ajax: './data.json', columns: [ { className: 'dt-control', orderable: false, data: null, defaultContent: '', }, { data: 'name' }, { data: 'position' }, { data: 'office' }, { data: 'salary' }, ], order: [[1, 'asc']], }); // Add event listener for opening and closing details $('#example tbody').on('click', 'td.dt-control', function () { var tr = $(this).closest('tr'); var row = table.row(tr); if (row.child.isShown()) { // This row is already open - close it row.child.hide(); tr.removeClass('shown'); } else { // Open this row row.child(format(row.data())).show(); tr.addClass('shown'); } }); }; </script> </body> </html>
codice:{ "data": [ { "id": "1", "name": "Tiger Nixon", "position": "System Architect", "salary": "$320,800", "start_date": "2011/04/25", "office": "Edinburgh", "extn": "5421" }, { "id": "2", "name": "Garrett Winters", "position": "Accountant", "salary": "$170,750", "start_date": "2011/07/25", "office": "Tokyo", "extn": "8422" }, { "id": "3", "name": "Ashton Cox", "position": "Junior Technical Author", "salary": "$86,000", "start_date": "2009/01/12", "office": "San Francisco", "extn": "1562" }, { "id": "4", "name": "Cedric Kelly", "position": "Senior Javascript Developer", "salary": "$433,060", "start_date": "2012/03/29", "office": "Edinburgh", "extn": "6224" }, { "id": "5", "name": "Airi Satou", "position": "Accountant", "salary": "$162,700", "start_date": "2008/11/28", "office": "Tokyo", "extn": "5407" } ] }

Rispondi quotando
