jQuery(document).ready(function() { // Initialize DataTable with empty data if(!jQuery('#jsonDataTable').length || jQuery('#jsonDataTable').is(':empty')) return; var table = jQuery('#jsonDataTable').DataTable(); // Initialize DataTable with empty data // var table = jQuery('#jsonDataTable').DataTable({ // language: { // emptyTable: 'Loading data...' // Optional: Customize empty table message // } // }); jQuery('#party-bill-payment-go').click(function(e) { e.preventDefault(); // Prevent default form submission // Show loader //jQuery('#loader').show(); jQuery('.dataTables_loader').show(); // Clear search input jQuery('.dataTables_filter input[type="search"]').val(''); // Redraw DataTable table.search('').columns().search('').draw(); // Retrieve the value attribute directly using .attr('value') to preserve spaces var partyName = jQuery('#optionsPartyName option:selected').attr('value'); var encodedPartyName = encodeURIComponent(partyName); var apiUrl = ajax_object.ajaxurl + '?action=fetch_data_party_payment&party_payment_name=' + encodedPartyName; //debugger jQuery.ajax({ url: apiUrl, method: 'GET', beforeSend: function() { // Show loader before sending the request //jQuery('#loader').show(); jQuery('.dataTables_loader').show(); }, success: function(response) { // Hide loader on success or error //jQuery('#loader').hide(); jQuery('.dataTables_loader').hide(); if (response.success) { var data = response.data; // Access the nested 'data' array // Clear existing data table.clear(); if (data && data.length > 0) { // Prepare an array of rows to be added var rows = data.map(function(item) { return [ item.SLNO || '', item.PARTY_CODE || '', item.PARTY_NAME || '', item.PARTY_BILL_NO || '', item.PARTY_BILL_DT || '', item.PARTY_BILL_AMT || '', item.BILL_PASS_AMT || '', item.PAID_AMOUNT || '', item.PAYMENT_DATE || '', item.BILL_DESC || '', item.PAY_MODE || '', item.DEPT_NAME || '', item.BILL_CD || '' ]; }); // Add the rows to the table table.rows.add(rows).draw(); } else { table.row.add(['No data found']); } // Redraw table with new data table.draw(); } else { table.clear().draw(); table.row.add(['Error: ' + (response.data || 'No data found'), '', '', '', '', '', '', '', '', '', '', '', '']).draw(); } }, error: function(xhr, status, error) { table.clear().draw(); table.row.add(['Error: ' + (xhr.responseJSON ? xhr.responseJSON.data : status), '', '', '', '', '', '', '', '', '', '', '', '']).draw(); } }); }); });