Trying to add a Clone button to View

227 views
Skip to first unread message

Tony Anytime

unread,
Feb 22, 2021, 6:53:33 PM2/22/21
to Jam.py Users Mailing List
As you can see below I add a CLONE button to form in Index.html following same method  the other buttons use. The button would simply create a duplicate of the record in focus .
The question is what is the best way to make it work, also how can I make it disappear and appear on just the view need. 

Thanks in advance





Drazen D. Babic

unread,
Feb 22, 2021, 7:56:52 PM2/22/21
to Jam.py Users Mailing List
No pic...

Tony Anytime

unread,
Feb 23, 2021, 11:42:28 AM2/23/21
to Jam.py Users Mailing List
Screenshot from 2021-02-22 18-43-50.png

Andrew Yushev

unread,
Feb 23, 2021, 3:27:06 PM2/23/21
to Tony Anytime, Jam.py Users Mailing List
Hi, Tony

You can clone a record as follows:

var clone_fields;

function clone_record(item) {
    // task.customers_report.customers.value = item.selections;
    // task.customers_report.print(false);
    clone_fields = {};
    item.each_field(function(f) {
        clone_fields[f.field_name] = f.value;
    });
    item.insert_record();
}

function on_edit_form_created(item) {
    if (clone_fields) {
        try {
            for (let field_name in clone_fields) {
                let field = item.field_by_name(field_name);
                if (!field.system_field()) {
                    field.value = clone_fields[field_name];
                }
            }
        }
        finally {
            clone_fields = undefined;
        }
    }
}

You can write on_after_scroll to disable or enable a button.
In the code below the buttons are disabled when the paid field value is true.

function on_after_scroll(item) {
    clearTimeout(scroll_timeout);
    scroll_timeout = setTimeout(
        function() {
            if (item.view_form && item.rec_count) {
                item.view_form.find("#delete-btn, #paid-btn").prop("disabled", item.paid.value);
            }
        }, 50
    );
}

Regards,
Andrew Yushev

вт, 23 февр. 2021 г. в 19:42, Tony Anytime <tonyany...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Jam.py Users Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jam-py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jam-py/dfbfe120-cd7e-48c0-9c6f-bab29eec4bffn%40googlegroups.com.

Tony Anytime

unread,
Feb 23, 2021, 4:20:47 PM2/23/21
to Jam.py Users Mailing List
Thank You Again

Tony Anytime

unread,
Feb 24, 2021, 3:22:08 PM2/24/21
to Jam.py Users Mailing List
    item.view_form.find("#new-btn").prop("disabled", true);    This works to disable button... is there property I can us to make it not show at all. I want button invisible.  Thanks

Andrew Yushev

unread,
Feb 25, 2021, 3:01:47 PM2/25/21
to Tony Anytime, Jam.py Users Mailing List
Of course 

let btns = item.view_form.find("#delete-btn, #paid-btn");
if (item.paid.value) {
    btns.hide();
} else {
    btns.show();
}

ср, 24 февр. 2021 г. в 23:22, Tony Anytime <tonyany...@gmail.com>:
Message has been deleted

Andrew Yushev

unread,
Mar 3, 2021, 11:16:03 AM3/3/21
to Tony Anytime, Jam.py Users Mailing List
The code is needed to understand why your buttons are ignored.
The code in the task client module and in index.html.

вт, 2 мар. 2021 г. в 18:04, Tony Anytime <tonyany...@gmail.com>:
This works great, but so far I have to put it in every function on_view_form_created(item)  or it will still show the default settings which is display.
I tried putting it in  Task client module or group client modules and it seem to ignore it.

Just to remind you, I am defining buttons in Index.html and then using them in each view as needed. So by default they seem to always be on first.

thanks

tony

Danijel Kaurin

unread,
Jul 1, 2021, 10:09:26 AM7/1/21
to Jam.py Users Mailing List
Hi Andrew.

Regarding to "clone record" functionality, if I want to clone record "x" times based on value in some filed that contains desirednumber of clones, how to write that code?

Regards

Danijel Kaurin

Andrew Yushev

unread,
Jul 4, 2021, 3:09:45 PM7/4/21
to Danijel Kaurin, Jam.py Users Mailing List
Hi, Danijel

You can use this function, it inserts 5 records:

function print(item) {
    let clone_fields = {};

    item.each_field(function(f) {
        clone_fields[f.field_name] = f.value;
    });
    for (let i = 1; i <= 5; i++) {
        item.insert();

        for (let field_name in clone_fields) {
            let field = item.field_by_name(field_name);
            if (!field.system_field()) {
                field.value = clone_fields[field_name];
            }
        }
        item.post();
    }
    item.appy();
}

Regards,
Andrew Yushev

чт, 1 июл. 2021 г. в 17:09, Danijel Kaurin <yonika...@gmail.com>:

Danijel Kaurin

unread,
Jul 16, 2021, 8:37:10 AM7/16/21
to Jam.py Users Mailing List
Thank you Andrew :)

Andreas Schneider

unread,
Dec 16, 2021, 11:04:59 AM12/16/21
to Jam.py Users Mailing List
the code works for text fields and dates ... but not for lookups

(sorry for hijack)

Andrew Yushev

unread,
Dec 19, 2021, 2:31:46 AM12/19/21
to Andreas Schneider, Jam.py Users Mailing List
Hi,

The function below should work for lookup_fields as well:

function insert_copys(item) {
    let clone_fields = {};

    item.each_field(function(f) {
        clone_fields[f.field_name] = [f.value, f.lookup_value];

    });
    for (let i = 1; i <= 5; i++) {
        item.insert();

        for (let field_name in clone_fields) {
            let field = item.field_by_name(field_name);
            if (!field.system_field()) {
                field.value = clone_fields[field_name][0];
                field.lookup_value = clone_fields[field_name][1];
            }
        }
        item.post();
    }
    item.appy();
}

Best regards

чт, 16 дек. 2021 г. в 19:05, Andreas Schneider <schnei...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages