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

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

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

Post by rpierce » 2021-06-17 23:23

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...

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

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

Post by jsetzer » 2021-06-20 07:43

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 1357 times

Prompt

chrome_m913qyVNSI.png
chrome_m913qyVNSI.png (5.87 KiB) Viewed 1357 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).
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

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

Post by zibrahim » 2021-08-13 05:03

Thanks Jan. I was looking for this for a while and found it here.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

Post Reply