$(document).ready(function () { /*BEGIN - Append percentage of funds remaining to acquisitions funds tables * David Green - Carnegie-Stout Library * 2018-03-22 * Koha Version: 17.05, 17.11 */ /* Acquisitions budget table */ /* Wait for the data table to initialize. */ $("#budgeth").on("init.dt", function () { console.log("IntranetUserJS: Acquisitions Table Percentage"); /*On each row of the table: */ $("#budgeth") .DataTable() .rows() .nodes() .toArray() .forEach(function (row) { /* Get the Allocated Funds by: getting the text from column 2, removing commas, and converting to a number. */ var allocated = parseFloat( row.children[2].textContent.trim().replace(",", "") ); /*Get the Remaining Funds from column 8 in the same way. */ var remaining = parseFloat( row.children[8].textContent.trim().replace(",", "") ); /*Calculate and append the percentage of funds remaining to the 8th column. */ var percent = Math.floor((remaining / allocated) * 100); row.children[8].innerHTML += ` (${percent}%)`; percent = Math.max(-100, Math.min(100, percent)); row.children[8].style = `background: linear-gradient(to right, #DFD 0%, #9F9 ${percent}%, #FFF ${percent}%, #FFF ${ percent + 100 }%, #FAA ${percent + 100}%, #FCC 100%);`; }); /*Make numbers in the table black. */ $("#budgeth .total_amount, #budgeth .child_fund_amount").css("color", ""); /*Redraw the table. */ $("#budgeth").DataTable().rows().invalidate().draw(); }); /*END - Append percentage of funds remaining to acquisitions budget table */ /* Acquisitions home */ /* Wait for the data table to initialize. */ $("#accounts").on("init.dt", function () { console.log("IntranetUserJS: Acquisitions Table Percentage"); /*On each row of the table: */ $("#accounts") .DataTable() .rows() .nodes() .toArray() .forEach(function (row) { /*Get the Allocated Funds by: getting the text from column 4, removing commas, and converting to a number. */ var allocated = parseFloat( row.children[4].textContent.trim().replace(",", "") ); /*Get the Remaining Funds from column 7 in the same way. */ var remaining = parseFloat( row.children[7].textContent.trim().replace(",", "") ); /*Calculate and append the percentage of funds remaining to the 8th column. */ var percent = Math.floor((remaining / allocated) * 100); row.children[7].innerHTML += ` (${percent}%)`; percent = Math.max(-100, Math.min(100, percent)); row.children[7].style = `background: linear-gradient(to right, #DFD 0%, #9F9 ${percent}%, #FFF ${percent}%, #FFF ${ percent + 100 }%, #FAA ${percent + 100}%, #FCC 100%);`; }); /*Make numbers in the table black. */ $("#accounts .total_amount, #accounts .child_fund_amount").css("color", ""); /*Redraw the table. */ $("#accounts").DataTable().rows().invalidate().draw(); }); /*END - Append percentage of funds remaining to acquisitions home. */