Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dependent tinymce textarea according to the selection of email template name?

I am using SugarCRM 6.5.x CE version. I want to make a dependent functionality where I have a dropdown field with list of email template name. And according to the selection of email template, textarea should be filled with email template body text. So, I have achieved the result.

Now, instead of normal textarea, I want to show this body text in tinymce editor textarea. Now, I have "convert" my textarea into tinymce editor textarea by going through this url How to convert textarea into tinymce editor

Now, when I chose email template from dropdown field, this tinymce textarea doesn't fill up with respective body text.

This is my javascript for normal textarea, and its working

function display_text(){
    if(typeof(document.getElementsByName('email_template_c')[0].value) != "undefined"){
        var custom_data = document.getElementsByName('email_template_c')[0].value;
        if(custom_data != ''){
            $.ajax({
                url:'index.php?entryPoint=check_email_template_subject',
                data:{new_custom_data: custom_data},
                success: function(data){
                    if(data!= ''){
                        document.getElementsByName("email_template_body_c")[0].value = data;
                        SUGAR.util.callOnChangeListers(document.getElementsByName("email_template_body_c")[0]);
                    }else{
                        document.getElementsByName("email_template_body_c")[0].value = '';
                        SUGAR.util.callOnChangeListers(document.getElementsByName("email_template_body_c")[0]);
                    }
                }
            });
        }       
    }
}

This is my code for tinymce editor textarea which is not working

function display_text(){
    if(typeof(document.getElementsByName('email_template_c')[0].value) != "undefined"){
        var custom_data = document.getElementsByName('email_template_c')[0].value;
        if(custom_data != ''){
            $.ajax({
                url:'index.php?entryPoint=check_email_template_subject',
                data:{new_custom_data: custom_data},
                success: function(data){
                    if(data!= ''){
                        $("p").parent(".mceContentBody").val(data);
                        SUGAR.util.callOnChangeListers($("p").parent(".mceContentBody").val(data));
                    }else{
                        $("p").parent(".mceContentBody").val();
                        SUGAR.util.callOnChangeListers($("p").parent(".mceContentBody").val());
                    }
                }
            });
        }       
    }
}

where this ajax gets data from this check_email_template_subject.php file

<?php
    global $db; 

    if($_REQUEST['new_custom_data'] != null){
        $template_id = urldecode($_REQUEST['new_custom_data']);
        $query1 = "SELECT body FROM email_templates WHERE id = '$template_id'";
        $result1 = $db->query($query1);
        $row1 = $db->fetchByAssoc($result1);
        echo $row1['body'];
    }       
?>

And this is html code of tinymce editor texarea,

<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="X-UA-COMPATIBLE" content="IE=7">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body dir="ltr" id="tinymce" class="mceContentBody" contenteditable="true">
<p>//Here should come email template body text
<br>
</p>
</body>
</html>
like image 653
santa banta Avatar asked Feb 20 '15 08:02

santa banta


1 Answers

Use setContent method of active Editor in tinyMCE

suppose you have value as

var txtVal = $(".dropdown").val(); // dropdown value 

//set value as in your active tinyMCE's textarea editor

tinyMCE.activeEditor.setContent(txtVal );

Hope it will work :)

like image 185
Mazzu Avatar answered Oct 08 '22 15:10

Mazzu