$(document).ready(function () { $('#paid-lots-tab').on('shown.bs.tab', function () { // Check if DataTable for paid lots already exists if (!$.fn.DataTable.isDataTable('#tblPaidLots')) { loadPaidLotsDataTable(); } }); }); function loadPaidLotsDataTable() { $('#tblPaidLots').DataTable({ ajax: { url: '/api/lots/won/paid', dataSrc: '', }, columns: [ { data: 'name', render: function (data, type, row) { return '' + data + ''; }, }, { data: 'highest_bid_amount' }, { data: 'payment_method' }, { data: 'shipping_status', render: function (data) { var status = ''; if (data === 'pending') { status = 'Pending'; } else if (data === 'in_transit') { status = 'In Transit'; } else if (data === 'delivered') { status = 'Delivered'; } return status; }, }, { data: 'id', render: function (data, type, row) { var elem = ''; if (row.shipping_status === 'delivered') { elem = `
`; } else { elem = `
Mark as Delivered
`; } return elem; }, width: '15%', }, ], responsive: true, searching: false, paging: false, info: false, language: { emptyTable: 'No payment has been made till yet...', }, }); $('#tblPaidLots').on('click', '#markedAsDeliveredBtn', function () { const csrftoken = Cookies.get('csrftoken'); var shipmentUpdateUrl = '/api/lots/ship/update/'; var shipmentId = $(this).data('shipment-id'); fetch(shipmentUpdateUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrftoken, }, body: JSON.stringify({ shipment_id: shipmentId, shipment_date: '', shipment_status: 'delivered', }), }) .then((response) => { return response.json(); }) .then((data) => { toastr.success(data['message']); // Reload table after successful update reloadPaidLotsDataTable(); console.log('Shipment updated successfully:'); }) .catch((error) => { toastr.error(data['error']); console.error('Error updating shipment:', error); }); }); // reloading table function reloadPaidLotsDataTable() { if ($.fn.DataTable.isDataTable('#tblPaidLots')) { $('#tblPaidLots').DataTable().ajax.reload(); } } }