Page 1 of 1

How to add and Alert Message on "Save as Copy"

Posted: 2021-06-17 23:23
by rpierce
Is it possible to have an alert pop up when someone clicks the Save as Copy button? I would like to have them accept the alert dialog before the duplication occurs.

Thank you...

Re: How to add and Alert Message on "Save as Copy"

Posted: 2021-06-20 07:43
by jsetzer
Hi,

what about this javascript code, which shows a browser-built-in confirmation prompt before copying the record.

Code

(1) For re-usability, the following javascript-function should be put in a common, shared file, for example in hooks/header-extras.php.

Code: Select all

// file: hooks/header-extras.php
<script>
  function confirmCopy(prompt = "Are your sure?") {
    let tn = AppGini.currentTableName();
    let pk = jQuery(`input[name="SelectedID"]`).val();
    let btn = jQuery("#insert").attr("data-prompt", prompt);
    let ico = btn.find("i.glyphicon").clone();
    if (tn && btn.length && pk) btn.attr("onclick", `return ${tn}_validateData() && confirm(this.getAttribute('data-prompt'))`).text(btn.text() + "...").prepend(ico);
  }
</script>
(2) Put the following line into the javascript-hooks-file for your table's detail view:

Code: Select all

// file: hooks/TABLENAME-dv.js
confirmCopy("Do you really want to insert a copy?");
That's it. You can customize the prompt text in the first parameter.

Result

Note the "..." text which has been appended to the button-text. This is a widely used method for indicating to the user that there is a prompt before execution.

chrome_WBlTQEuB7b.png
chrome_WBlTQEuB7b.png (12.5 KiB) Viewed 2322 times

Prompt

chrome_m913qyVNSI.png
chrome_m913qyVNSI.png (5.87 KiB) Viewed 2322 times

The record will be inserted (as copy) only if you click OK.

The code keeps the icon, appends "...", keeps form-validation and only applies to "copy" action (for existing records), not to "insert" action (for new records).

Re: How to add and Alert Message on "Save as Copy"

Posted: 2021-08-13 05:03
by zibrahim
Thanks Jan. I was looking for this for a while and found it here.