One-Click Copy to Clipboard
Users frequently need to copy values from APEX pages: order numbers, tracking codes, API keys, URLs. Adding a “copy to clipboard” button next to these values saves time and eliminates copy errors.
Copy Button Implementation
// Generic copy function
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
apex.message.showPageSuccess("Copied to clipboard");
}).catch(function() {
// Fallback for older browsers
var $temp = $("<textarea>").val(text).appendTo("body").select();
document.execCommand("copy");
$temp.remove();
apex.message.showPageSuccess("Copied to clipboard");
});
}
// Attach to a button next to an item
$("#copy_order_id").on("click", function() {
copyToClipboard(apex.item("P10_ORDER_ID").getValue());
});
Copy Button as Column Link in Reports
-- SQL column for a copy button
SELECT order_id,
'<button class="t-Button t-Button--tiny js-copy-btn" '
|| 'data-copy="' || tracking_number || '">'
|| '<span class="fa fa-copy"></span></button>' AS copy_btn,
tracking_number, status
FROM shipments;
// Page-level JavaScript (event delegation)
$(document).on("click", ".js-copy-btn", function() {
copyToClipboard($(this).data("copy"));
});
Paste From Clipboard Into Multiple Fields
// Parse pasted tab-separated data into multiple items
// Useful for pasting from Excel
document.addEventListener("paste", function(e) {
var activeEl = document.activeElement;
if (activeEl.id === "P10_BULK_PASTE") {
e.preventDefault();
var text = e.clipboardData.getData("text/plain");
var lines = text.split("\n");
lines.forEach(function(line, i) {
var cols = line.split("\t");
if (cols.length >= 3) {
// Add to a collection or staging table via AJAX
apex.server.process("ADD_PASTE_ROW", {
x01: cols[0], x02: cols[1], x03: cols[2]
});
}
});
apex.region("stagingGrid").refresh();
}
});
Copy Formatted Table Data
// Copy an IR or IG's visible data as tab-separated text (for Excel paste)
function copyReportData(regionId) {
var rows = [];
$("#" + regionId + " table tbody tr").each(function() {
var cols = [];
$(this).find("td").each(function() { cols.push($(this).text().trim()); });
rows.push(cols.join("\t"));
});
copyToClipboard(rows.join("\n"));
}