Recibo esta respuesta JSON desde el back-end.
{ "data": [ { "id": 6, "firstname": "fname", "middlename": "mname", "lastname": "lname", }, ... ] }Crear una tabla a partir de la respuesta JSON usando jquery datatable usando una solicitud ajax.
$number = 1; $("#people-table").DataTable({ ajax: { url: window.location.href + '/fetchdata', method: "GET", dataType: "json", dataSrc: "data" }, columns: [ { render: function () { return $number++; } }, { data: 'firstname' }, { data: 'middlename' }, { data: 'lastname' }, ] }); <table> <thead> <tr> <th>No.</th> <th>Profile</th> <th>Firstname</th> <th>Middlename</th> <th>Lastname</th> </tr> </thead> <tbody> {{-- data --}} </tbody> </table>Obtengo el resultado adecuado para este código en la tabla html, pero ahora quiero combinar los tres campos nombre, segundo nombre y apellido en un campo llamado nombre completo usando jquery datatable
Puede hacer algo como esto en lugar de DataTables. Este es un ejemplo puro de javascript.
HTML
<thead> <tr> <th>No.</th> <th>Profile</th> <th>Full Name</th> </tr> </thead> <tbody id="myDataTable"></tbody>JavaScript
<script> function MyFunction(){ $.ajax({ url: 'your-url' , type: 'get', datetype:"json", success: function(res){ var myData = res.data //collection from backend let str = ""; myData.forEach((data, key) => { str += ` <tr> <td> ${data.no} </td> <td> ${data.profile} </td> <td> ${data.firstname} ${data.middlename} ${data.lastname} </td> </tr> ` }) $("#myDataTable").html(str); } }) } </script>Asegúrese de llamar a su función cada vez que necesite que se muestre.