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 :)

1 Answer 1

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--']))
. ');'
. '})');
Sign up to request clarification or add additional context in comments.

5 Comments

I tryed that, it guives me a syntax error :unterminated string literal
@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.
I as about to put the code when you edited the comment it worked :) once again you saved the day many thanks :)
You should simply use json encode... stackoverflow.com/questions/168214/…
@soju Hey buddy, Thank you for mentioning that. I have updated the answer. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.