在文本编辑器中显示值,点击组合框:Yii 1.1.

Display value in texteditor tinymce on click of combo box:Yii 1.1

本文关键字:组合 Yii 文本编辑 文本 编辑器 显示      更新时间:2023-09-26

我必须使用文本编辑器,即在我的表单中使用tinymce。我必须在选择组合框的文本编辑器中显示数据。简单地使用文本区域没有问题,我可以在选择组合时在我的文本区域获得价值。现在我必须使用文本编辑器而不是简单的文本区域。我的视图代码是:

<div class="row col2">
<?php echo $form->labelEx($model,'template'); ?>
<?php 
        $records = CHtml::listData(EmailTemplate::model()->findAll(array('order' => 'email_template_id','condition'=>"status= '1'")), 'email_template_id', 'subject');
        // echo $form->dropDownList($model,'template',$records,array('id'=>'myDropDown','empty' => 'Select'));
            echo $form->dropDownList($model,'template',$records,array(
                                  'id' => 'myDropDown',
                                  'empty' => 'Select Template',
                                  'ajax' => array(
                                  'type'=>'POST',
                                  'url'=>CController::createUrl('reply/description'),//your controller and action name
                                  'update'=>'#myTextArea', 
                                  'success'=> 'function(data) {
                                    $("#myTextArea").empty(); 
                                    $("#myTextArea").val(data); 
                                  } '
                                  ))); 
            ?>
<?php echo $form->error($model,'template'); ?>
</div>
<div class="row col2">
          <?php echo $form->labelEx($model,'message'); ?>
          <?php echo                $form->textArea($model,'message',array('id'=>'myTextArea','style'=>'width: 680px; height: 300px;')); ?>
          <?php echo $form->error($model,'message'); ?>
      </div>

我的js代码如下:

   <script type="text/javascript">
    tinymce.init({
    selector: "textarea#myTextArea",
    theme: "modern",
    preformatted:true,
    width: 700,
    height: 300,
   plugins: [
         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
         "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
        "save table contextmenu directionality emoticons template paste textcolor"
   ],
   content_css: "css/content.css",
   menubar:false,
    toolbar: "bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent| forecolor backcolor",
 }); 
 </script>
<script>
$('#myDropDown').on('change', function() {
    $.ajax({
        url: "<?php echo $this->createUrl('reply/description'); ?>",
        dataType: 'html',
        type: 'POST',
        data: {dropDownValue: $(this).val()},
        success: function(template, textStatus, jqXHR) {
            alert(data);
            $('#myTextArea').val(data);
        }‌
    });
});
 </script>

我的控制器是:

 public function actionDescription()
{   
    $cvId=0;
    $cvId= $_POST['Reply']['template'];
      if (Yii::app()->request->isAjaxRequest) {
          if($cvId>0){
        $templateModel=EmailTemplate::model()->findByPk($cvId);
        echo $templateModel->description;
       }
    }
}

一旦TinyMCE出现在页面上,只需更改底层文本区域的内容(您是否正在AJAX调用中执行(不会导致TinyMCE重新加载内容。

您需要使用 TinyMCE 的 API 在 AJAX 调用的成功方法中设置编辑器的内容。

例如:

tinyMCE.activeEditor.setContent(data);

。其中数据是包含 AJAX 调用返回的内容的字符串。

好的,

我想出了问题的答案。下拉列表代码进行了简单的更改。它现在运行良好。

<?php 
$records = CHtml::listData(EmailTemplate::model()->findAll(array('order' => 'email_template_id','condition'=>"status= '1'")), 'email_template_id', 'subject');
echo $form->dropDownList($model,'template',$records,array(
                                  'id' => 'myDropDown',
                                  'empty' => 'Select Template',
                                  'ajax' => array(
                                  'type'=>'POST',
                                  'url'=>CController::createUrl('marketingEmail/description'),
                                  'update'=>'#myTextArea', 
                                  'success'=> 'function(data) {
                                    $("#myTextArea").empty(); 
                                    tinyMCE.activeEditor.setContent(data); 
                                  } '
                                  ))); 
            ?>