Save data input from a datatable as a JSON object
To save data input from a datatable as a JSON object, you can use the rows().data() method of the datatable object to retrieve all the rows in the table, and then loop through them to extract the data from the input fields. Here's an example:
// get the datatable object var table = $('#example').DataTable(); // get all the data in the table var data = table.rows().data(); // create an array to store the data var dataArray = []; // loop through the rows and extract the data data.each(function (value, index) { var row = {}; row['name'] = value[0]; // assuming the first column contains the name row['email'] = value[1]; // assuming the second column contains the email row['phone'] = value[2]; // assuming the third column contains the phone dataArray.push(row); }); // convert the array to a JSON object var jsonData = JSON.stringify(dataArray); // send the JSON object to the server using AJAX $.ajax({ url: 'save-data.php', type: 'POST', data: {data: jsonData}, success: function(response) { console.log(response); } });
In this example, we assume that the table has three columns containing the name, email, and phone data. We loop through each row and extract the data from the input fields. Then, we convert the array of data to a JSON object using JSON.stringify(). Finally, we send the JSON object to the server using an AJAX request, where it can be processed and saved to a database or file.
Komentar
Posting Komentar