Howto custom button to copy content from field to another

drashrafsabry

Well-known member
dadabik enterprise 9.1

question: How can i create a custom button to copy content of a single select field and paste it into a textarea field underneath
 

eugenio

Administrator
Staff member
Hello,
you have an example of a copy content button in your prepackaged app Dada sales (it copies the address of the customer to the invoice address). Install the app and insert a new customer to see it in action.

For the code, open from apps/dadasales the files custom_functions.php and custom_functions.js, the relevant function is dadabik_copy_customer_address.

Best,
 

drashrafsabry

Well-known member
I have table Reports
i want to copy the values of single select field Report_Template (populated from another table correctly) into textarea field Report

i added to custom_functions.php

$custom_buttons['Reports'][$cnt]['type'] = 'javascript';
$custom_buttons['Reports'][$cnt]['callback_function'] = 'dadabik_copy_report';
$custom_buttons['Reports'][$cnt]['permission_needed'] = 'insert';
$custom_buttons['Reports'][$cnt]['show_in'][] = 'insert_form';
$custom_buttons['Reports'][$cnt]['show_in'][] = 'edit_form';
$custom_buttons['Reports'][$cnt]['position_form'] = 'Report_Template';
$custom_buttons['Reports'][$cnt]['label_type'] = 'fixed';
$custom_buttons['Reports'][$cnt]['label'] = 'Copy Template To Report Box';
$custom_buttons['Reports'][$cnt]['style'] = 'background:#000;width:250px';
$cnt++;


and added to custom_functions.js
function dadabik_copy_report(field)
{
$('[name="Report"]')[0].value = $('[name="Report_Template"]')[0].value;
}


Button shows correctly but when pressed upon it does not copy anything
 

drashrafsabry

Well-known member
ok i found this two lines omitted when i added it copies
// CUSTOM BUTTONS
$cnt = 0;
/* add custom buttons here (see documentation for details) */

$cnt=0



BUT NOW
if the source field is text area , it works perfect , but if single select it pastes numbers only 1 , or 2 or 3 why?
 

eugenio

Administrator
Staff member
Because that's the value of the field, what you have in the "value" attribute.

If, instead, you want the text showed to the user, instead of:

$('[name="Report"]')[0].value = $('[name="Report_Template"]')[0].value;

you need something like:

selectedindex = $('[name="Report_Template"]')[0].selectedIndex;
$('[name="Report"]')[0].value = $('[name="Report_Template"]')[0][selectedindex].text;
 

drashrafsabry

Well-known member
Ok that worked
Lastly how can i preserve the line breaks ... when it pasted , it pasted as a 1 paragraph without the original line breaks
 

eugenio

Administrator
Staff member
This is strange; try yourself with the dada sales app: starting from a fresh installation just change the field type of both the customer addresses to textarea, introduce a line break in the first and copy it to the second: the line break are preserved.
 

drashrafsabry

Well-known member
Field 1 in REPORTS table is select_single reading values from a textarea field type in table REPORTS_TEMPLATES ...
Custom button is to copy the value from the select single into a text area box


so in reports table it is a select_single value read from another table being copied into field2 texarea
 

drashrafsabry

Well-known member
i cannot change field to txtarea as my main design is that single select reads reports templates from another table , i use button to paste it in empty txt area and adjust the text accordingly then use pdf export to print report
 

eugenio

Administrator
Staff member
It is not clear to me what the select_single items contain. If they get the value from another table post here the content of a record from such table containing an example of line break (open the table with phpmyadmin and copy/paste here without adding anything).

Best,
 

eugenio

Administrator
Staff member
Ok, having line breaks in a select single option is not a good practice, also because HTML doesn't support it: you can't see a line break in a SELECT element.

Having said that, if you really want to copy a text option that contains line breaks, there are at least two different cases:

- if the original content contains actual line breaks, everything should work without any change

- if the original content contains <br> tag instead of line breaks, you should first replace the <br> with line breaks, so instead of:

selectedindex = $('[name="Report_Template"]')[0].selectedIndex;
$('[name="Report"]')[0].value = $('[name="Report_Template"]')[0][selectedindex].text;

you should do something like:

selectedindex = $('[name="Report_Template"]')[0].selectedIndex;
var mytext = $('[name="Report_Template"]')[0][selectedindex].text;
var mytext_2 = mytext.replace(/<br>/gi, "\n");
$('[name="Report"]')[0].value = mytext_2;

As I said, having line breaks in a select option is however something I don't recommend.

Best,
 

eugenio

Administrator
Staff member
Hello,
I did some further tests; as I said, line breaks are not supported in HTML for a select input elements. I thought, however, that at least you could copy (in javascript) the text - including the line breaks - but this is not the case, line breaks are converted in normal spaces.

Best,
 
Top