Reputation: 503
following code will implement AJAX-Request as intended. Curiously, it's only working with yii2-method textinput(). Using yii2-method textarea() or even widget CKEditor doesn't work- Any ideas how to use AJAX-Request with more comfortable methods but textinput()? Here is Controllercode:
<?php
namespace frontend\controllers;
use Yii;
use yii\helpers\Json;
use backend\modules\app_einstellung\models\Textbaustein;
use yii\web\Controller;
class TextbausteinController extends Controller {
public function actionBaustein($textId) {
$text = Textbaustein::findOne($textId);
echo Json::encode($text);
}
}
Here is JQueryCode:
<?=
Dialog::widget();
$script = <<< JS
$('#bez').change(function(){
var textId=$(this).val();
$.get('textbaustein/baustein',{textId:textId},function(data){
var data=$.parseJSON(data);
krajeeDialog.alert('Fügen Sie eventuell erzeugte Mailvorlagen manuell(Copy&Paste) in Ihren Maileditor ein');
$('#mailausgang-inhalt').attr('value',data.inhalt);
});
});
JS;
$this->registerJS($script);
?>
Following elements don't work with AJAX! Why??
//echo $form->field($model, 'bodytext')->textarea();
/* echo $form->field($model, 'bodytext')->widget(\dosamigos\ckeditor\CKEditor::className(), [
'preset' => 'full', 'clientOptions' => ['height' => 200],
]); */
Only this element works with AJAX! Why only this??
<div class="col-md-6">
<?=
$form->field($modelTextbaustein, 'bezeichnung', ['addon' => [
'prepend' => ['content' => 'Mailvorlagen']]])->widget(\kartik\widgets\Select2::classname(), [
'data' => \yii\helpers\ArrayHelper::map(Textbaustein::find()->orderBy('bezeichnung')->asArray()->all(), 'id', 'bezeichnung'),
'options' => [
'id' => 'bez'
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
</div>
works only with this:
<?php
echo $form->field($model, 'bodytext', ['addon' => [
'prepend' => ['content' => 'Vorlage']]])->widget(TwbsMaxlength::className())
->textInput();
Rephrasing question:
As I think to attach an event handler on keypress event, in which fire the Ajax call only when the key is ENTER I coded like this but didn't succeed with it:
$script = <<< JS
$('#bez').keypress(function(e){
if(e.which == 13) {
var textId=$(this).val();
$.get('textbaustein/baustein',{textId:textId},function(data){
var data=$.parseJSON(data);
$('#mailausgang-inhalt').attr('value',data.inhalt);
});
}
});
Upvotes: 0
Views: 131
Reputation: 750
i don't know if this is the mistake , but i think thay in your AJAX response whe you set the value in the input the "id" is wrong .
When you create a input in a form in yii2 his id by defaut is "nameModel-attribute" , then in you case it is "textbaustein-bodytext", and in you JS you must write like that .
// this set a attribute value , but you dont see difference in you view , only in the code
$('#textbaustein-bodytext').attr('value',data.inhalt);
// this change that you see
$('#textbaustein-bodytext').text(data.inhalt);
//or
$('#textbaustein-bodytext').val(data.inhalt);
if you will like write a different ID , you must write like this
echo $form->field($model, 'bodytext')->textarea(['id'=>'testId']);
Upvotes: 1