1

the question says it all, yesterday I asked How to add fields to activeform with js/jQuery in Yii2?

well now I need to add a dropdown menu and is is not the same thing...

I have this:

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\User;
use app\models\ContactType;

$cntcttp = new ContactType;

$this->registerJs('$("#btnadd").on("click",function(){'
  . '$("#dynamicInput").append(\''
  .  Html::textInput("contacto","",['placeholder'=>"contacto"])
  . '\');'
  . '})');

?>

<?php $form = ActiveForm::begin(); ?>

    <div id="dynamicInput"></div>

    <?= Html::activeDropDownList($cntcttp, 'ContactTypeID',
    ArrayHelper::map($ctypes, 'ContactTypeID', 'Name') , ['prompt'=>'--contacto--']) ?>


    <input type='button' id='btnadd' value="add contact">
    <div class="form-group">
        <?= Html::submitInput('Submit', ['class' => 'btn-primary']) ?>
    </div>
<?php  ActiveForm::end(); ?>

now I need to make the dorpdown appear when the user clicks the #btnadd instead of what I have now :P

Thank you in advance :)

Community
  • 1
  • 1
Fernando Andrade
  • 795
  • 1
  • 7
  • 19

1 Answers1

3

It's almost the same thing. Just do like below:

$this->registerJs('$("#btnadd").on("click",function(){'
. '$("#dynamicInput").append(\''
.   Html::activeDropDownList($cntcttp, 'ContactTypeID',
    ArrayHelper::map($ctypes, 'ContactTypeID', 'Name') , ['prompt'=>'--contacto--'])
. '\');'
. '})');

That's all.


You probably get syntax error in javascript, since, Yii puts \n in dropdown. The most dirty way to evade that syntax error is to do like below:

$this->registerJs('$("#btnadd").on("click",function(){'
. '$("#dynamicInput").append(\''
.   str_replace("\n", "", Html::activeDropDownList($cntcttp, 'ContactTypeID',
    ArrayHelper::map($ctypes, 'ContactTypeID', 'Name') , ['prompt'=>'--contacto--']))
. '\');'
. '})');

Special thanks to Soju who mentioned in comment to use json_encode()

However, I wrote that the most dirty way would be using str_replace(), but, you can use json_encode() instead which is a more elegant solution:

$this->registerJs('$("#btnadd").on("click",function(){'
. '$("#dynamicInput").append('
.   json_encode(Html::activeDropDownList($cntcttp, 'ContactTypeID',
    ArrayHelper::map($ctypes, 'ContactTypeID', 'Name') , ['prompt'=>'--contacto--']))
. ');'
. '})');
Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
  • I tryed that, it guives me a syntax error :unterminated string literal – Fernando Andrade Dec 18 '14 at 18:35
  • @FernandoAndrade Can you post the HTML that `Html::activeDropDownList()` produces (edit your question and add it)? Its just a matter of escaping things properly... You may be able to do it directly or you may need to overload `HTML` with a custom implementation of `activeDropDownList` but either way it should be doable. – prodigitalson Dec 18 '14 at 18:37
  • I as about to put the code when you edited the comment it worked :) once again you saved the day many thanks :) – Fernando Andrade Dec 18 '14 at 18:50
  • 1
    You should simply use json encode... http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines – soju Dec 18 '14 at 20:05
  • @soju Hey buddy, Thank you for mentioning that. I have updated the answer. Thanks again – Ali MasudianPour Dec 18 '14 at 20:11