Important Update: Archer Community Scheduled Maintenance on November 23–24 - New Community Launching Soon! Learn More..
2024-03-06 09:49 AM
Hi all,
Hoping some of you Archer JavaScript Hero's can point me in the right direction here.
We have a Questionnaire that runs Quarterly. Sometimes the answers don't change too much from Quarter to Quarter. To assist the submitter we were first asked to present the previous Quarter's answers on layout to help with the latest submission. Now we've been asked to assist further & enable a copy / paste function in one click (I know!!).
I implemented the below simple function, which almost works.
<script type='text/javascript'>
function copyPrevAnswer(whereToCopy, textToCopy) {
whereToCopy.innerHTML = textToCopy.innerHTML;
}
</script>
When you click in to edit the copy however, it disappears for some reason.
Adding an additional:
whereToCopy.focus();
...before (and/or after) setting the innerHTML, helps a little, but seems to be inconsistent, long clicks, seem to work ok... weird!
Anyone know what's happening any advice?
2024-03-11 04:20 PM
@ShaunMartinLaw2 Archer's HTML is a little complex how fields and associated data is laid out. Here's POC to copy just one text are field to another upon clicking a button to get you started.
<div class="workflow-app-buttons" >
<a title="Copy Field" class="tb-btn-link-left" id="btnCopyField" href="javascript:void(0);">
<div class="tb-btn" data-icon="" data-icon-pos="left"> Copy Field</div>
</a>
</div>
<script>
$('#btnCopyField').click(function(){
copyField("Text Area", "Text Area Two");
});
function copyField(from, to) {
var fromFieldId = lookupFieldId(from);
var toFieldId = lookupFieldId(to);
if (fromFieldId && toFieldId) {
setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());
}
}
function setTextNumericField(fld,text) {
if($CM._fields[fld].type == 1 && $CM._fields[fld].displayControl == 1) {
setTimeout( function(){
$('input[id$="'+ fld +'c"]').focus({preventScroll: true}).val(text).blur();
}, 20);
} else {
$('input[id$="'+ fld +'c"]').val(text);
$('div[id$="'+ fld +'c_text"]').html(text);
}
//Check to see if the field is read-only
if($('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly')) $('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly').html(text);
}
function lookupFieldId(fldName){
var goFindId = null;
try{
$('.FieldLabel').each(function(){
if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
goFindId = $(this).find("span")[0].id;
return false;
}
});
} catch (err) {console.log(err)}
try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsible:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsibleToolTip:findField("' + fldName + '")')[0].id;} catch (err) {}
return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
}
$.expr[':'].findField = function(a, i, m) {
return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
};
</script>
Advisory Consultant
2024-03-15 06:47 AM
Still no joy, I've alerted lots of different variations on this, bit nothing returned unfortunately.
When I hardcode the element & grab the innerHTML, as the second param it works.
setTextNumericField(toFieldId, setTextNumericField(toFieldId, document.getElementById('master_DefaultContent_rts_s5590_f19369c').innerHTML);
2024-03-06 03:28 PM
@ShaunMartinLaw2 it's hard to tell what's going on with just snippets of code. Can you post the complete custom object?
Advisory Consultant
2024-03-07 06:48 AM
Thanks David.
Here's how I am calling the above function, by providing a link to click, which calls the function above on click:
<div class="copy-text" id="btnUIDcopyPrevRiskOutlookJust">
<a title="Copy from previous answer" class="SystemURL" href="javascript:void(0);" onclick="copyPrevAnswer(document.getElementById('master_DefaultContent_rts_s5590_f19375c_text'), document.getElementById('master_DefaultContent_rts_s5590_f20161c'))">
⇐ Copy Previous</a></div>
whereToCopy = Editable Text Area - though shows up as Div when inspect.
TextToCopy = Uneditable textArea - though again copying Div content when inspect.
Both seem ok. Not sure why it clears & why focus() kind of helps.
2024-03-08 09:43 AM
Any insights @DavidPetty ? What am I missing? We've had a few in our team wrecking our brains, it feels simple.
2024-03-11 04:20 PM
@ShaunMartinLaw2 Archer's HTML is a little complex how fields and associated data is laid out. Here's POC to copy just one text are field to another upon clicking a button to get you started.
<div class="workflow-app-buttons" >
<a title="Copy Field" class="tb-btn-link-left" id="btnCopyField" href="javascript:void(0);">
<div class="tb-btn" data-icon="" data-icon-pos="left"> Copy Field</div>
</a>
</div>
<script>
$('#btnCopyField').click(function(){
copyField("Text Area", "Text Area Two");
});
function copyField(from, to) {
var fromFieldId = lookupFieldId(from);
var toFieldId = lookupFieldId(to);
if (fromFieldId && toFieldId) {
setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());
}
}
function setTextNumericField(fld,text) {
if($CM._fields[fld].type == 1 && $CM._fields[fld].displayControl == 1) {
setTimeout( function(){
$('input[id$="'+ fld +'c"]').focus({preventScroll: true}).val(text).blur();
}, 20);
} else {
$('input[id$="'+ fld +'c"]').val(text);
$('div[id$="'+ fld +'c_text"]').html(text);
}
//Check to see if the field is read-only
if($('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly')) $('input[id$="'+ fld +'c"]').parent().parent().find('.readOnly').html(text);
}
function lookupFieldId(fldName){
var goFindId = null;
try{
$('.FieldLabel').each(function(){
if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
goFindId = $(this).find("span")[0].id;
return false;
}
});
} catch (err) {console.log(err)}
try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsible:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabelCollapsibleToolTip:findField("' + fldName + '")')[0].id;} catch (err) {}
return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
}
$.expr[':'].findField = function(a, i, m) {
return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
};
</script>
Advisory Consultant
2024-03-14 12:40 PM
This is very tidy David, thanks so much.
I got it partially working, copying from an editable field, however, my use case is from a read only non editable field, so failing.
Using an alert, the second param seems to be where the issue lies below, as it returns undefined:
setTextNumericField(toFieldId, $('input[id$="' + fromFieldId + 'c"]').val());
2024-03-14 12:48 PM
Anytime : D
Try,
setTextNumericField(toFieldId, $('[id$="' + fromFieldId + 'c"]').text().trim());
Advisory Consultant
2024-03-15 06:47 AM
Still no joy, I've alerted lots of different variations on this, bit nothing returned unfortunately.
When I hardcode the element & grab the innerHTML, as the second param it works.
setTextNumericField(toFieldId, setTextNumericField(toFieldId, document.getElementById('master_DefaultContent_rts_s5590_f19369c').innerHTML);
2024-03-15 12:51 PM
Advisory Consultant