Postingan

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.ph

Add highlight on hover for a datatable

To add highlight on hover for a datatable, you can use the rowCallback option in the DataTables initialization. Here is an example code snippet: $(document).ready(function() { $('#example').DataTable({ "rowCallback": function( row, data ) { $(row).hover(function(){ $(this).addClass('highlight'); }, function(){ $(this).removeClass('highlight'); }); } }); }); In this code, we're using jQuery's hover method to add or remove the class highlight to the row based on mouse enter or leave events. Then, we define the rowCallback function to handle this behavior for each row in the table. Note that in this example, we assume that you have defined a CSS class named highlight to style the hovered row. You can define this class in your stylesheet as follows: .highlight { background-color: yellow; } This will give the rows a yellow background when they are hovered over. You can change the styles

Enable multiple row selection in a DataTable

To enable multiple row selection in a DataTable, you can set the select option to 'multi' when initializing the DataTable, like this: $(document).ready(function() { $('#myTable').DataTable({ select: 'multi' }); }); This will enable multiple row selection using the standard click and shift-click behavior. You can then get the selected rows using the rows({selected: true}) method. For example, to get the data for all selected rows, you could use: var table = $('#myTable').DataTable(); var data = table.rows({selected: true}).data().toArray(); This would give you an array of objects, where each object represents a selected row and contains the row's data. You can also listen for the select and deselect events to perform actions when rows are selected or deselected. For example, to log the selected rows to the console whenever a row is selected or deselected, you could use: $('#myTable').on('select deselect&

Server-side paging in DataTables

Server-side paging in DataTables allows for large amounts of data to be loaded and displayed on a page without having to load the entire dataset. This is done by sending an Ajax request to the server for each new page of data, instead of loading all the data at once. Here are the basic steps to implement server-side paging in DataTables: 1. Configure the server-side script to return the appropriate subset of data based on the page number and page size. 2. Initialize DataTables with the server-side processing option set to true, and configure the Ajax settings to point to the server-side script and send the necessary parameters. 3. Set the paging options in DataTables to display the desired number of rows per page, and configure any additional options as needed. Here is an example of how to implement server-side paging in DataTables using PHP and MySQL: PHP script (server-side script to retrieve data): <?php // MySQL database connection details $host = 'localhost';

Generate an input form in a jQuery datatable

To generate an input form in a jQuery datatable, you can use the render function of a column definition to return an HTML string that contains the input element. Here's an example: HTML: <div class="col-md-12"> <button type="button" class="btn btn-sm btn-primary" id="mct_btn_tambah"><span class="fa fa-plus"></span> Tambah Tindakan</button> <table id="mct_table" class="table" style="width:100%;"></table> </div> JAVASCRIPT: <script type="text/javascript"> var mct = function () { this.table = ''; var self = this; this.init = function () { self.table = $('#mct_table').DataTable({ info: false, paging:false, sorting:false, searching:false, orderCellsTop: true, columns: [ {data: null, title:'No.', name: 'no', width: '2

Check if the FTP service is running on an Ubuntu server with PHP

You can use the exec() function in PHP to execute a shell command to check the status of the FTP service on a remote server. Here's an example code that uses the systemctl command to check if the FTP service is running on an Ubuntu server: $server = '192.168.1.100'; // Replace with your server IP $username = 'username'; // Replace with your server username $password = 'password'; // Replace with your server password // SSH connection $connection = ssh2_connect($server, 22); ssh2_auth_password($connection, $username, $password); // Execute systemctl command to check FTP service status $stream = ssh2_exec($connection, 'systemctl status vsftpd.service'); stream_set_blocking($stream, true); // Get command output $output = stream_get_contents($stream); // Check if FTP service is active if (strpos($output, 'Active: active (running)') !== false) { echo 'FTP service is running.'; } else { echo 'FTP service is not running.&

Fix the header of a DataTable,

To fix the header of a DataTable, you can use the fixedHeader extension provided by DataTables. This extension will fix the header of the table in place while allowing the rest of the table to scroll. Here's how you can use it: 1. First, include the datatables.fixedHeader.min.js file in your HTML document. You can download this file from the DataTables website or use a CDN link. <script src="https://cdn.datatables.net/fixedheader/3.1.9/js/dataTables.fixedHeader.min.js"/></script/> 2. Then, initialize your DataTable with the fixedHeader option set to true. $(document).ready(function() { $('#example').DataTable({ fixedHeader: true }); }); This will fix the header of your table in place while allowing the rest of the table to scroll. You can also customize the behavior of the fixed header by passing in additional options to the fixedHeader option. For example, you can change the offset of the header from the top of the viewp

Add a row to a DataTable using jQuery

To add a row to a DataTable using jQuery, you can use the row.add() method. Here's an example: var table = $('#myTable').DataTable(); var data = ['John', 'Doe', 'johndoe@example.com']; table.row.add(data).draw(); In this example, myTable is the ID of the DataTable, data is an array containing the data for the new row, and draw() is called to redraw the table with the new row added. You can also add a row using an object instead of an array. Here's an example: var table = $('#myTable').DataTable(); var data = { first_name: 'John', last_name: 'Doe', email: 'johndoe@example.com' }; table.row.add(data).draw(); In this example, data is an object containing the data for the new row, and the keys correspond to the column names in the table.

Convert JSON data to input text on a datatable using jQuery

To convert JSON data to input text on a datatable using jQuery, you can use the render option of the columns configuration to specify a function that returns the HTML markup for the input text. Here's an example: // Assuming JSON data in the format [{ "name": "John", "age": 30 }, ...] $(document).ready(function() { $('#myTable').DataTable({ data: myData, // myData is the JSON data columns: [ { data: 'name', title: 'Name' }, { data: 'age', title: 'Age' }, { data: null, title: 'Input', render: function(data, type, row, meta) { return '<input type="text" value="' + data.name + '" />'; } } ] }); }); In this example, the data parameter of the render function is the object containing the data for the current row. We extract the name property from this object and use it as the initia

Check if an IP address is active or not with PHP

To check if an IP address is active or not with PHP, you can use various approaches. Here are a few examples: 1. Using ping command with exec() function: $ip = '192.168.1.1'; if (exec(sprintf("ping -c 1 -W 2 %s", escapeshellarg($ip))) == true) { echo "IP is active"; } else { echo "IP is not active"; } This code sends a single ping request with a timeout of 2 seconds. If the IP address responds, the exec() function will return a non-empty result, and the code will output "IP is active". Otherwise, it will output "IP is not active". 2. Using fsockopen() function to check if a port is open: $ip = '192.168.1.1'; $port = 80; if($fp = fsockopen($ip,$port,$errCode,$errStr,1)){ echo "IP is active"; fclose($fp); } else { echo "IP is not active"; } This code checks if port 80 is open on the IP address using the fsockopen() function. If the connection is successful, the code w