Yii Framework Forum: wbraganca/yii2-dynamicform change textfield value based on button - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

wbraganca/yii2-dynamicform change textfield value based on button Rate Topic: -----

#1 User is offline   Najpox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-November 17

Posted 17 December 2017 - 11:50 AM

Hi,

I have problem with change value in textfield by button setDefault.

After I clicked on this button I cant access and change value in the name textfield above the button on the "Martin".

I set id "idN2" for this, but nothing happend.

PLEASE , HELP ME.

VIEW:

 <?php DynamicFormWidget::begin([

        'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]

        'widgetBody' => '.container-items', // required: css class selector

        'widgetItem' => '.item', // required: css class

        'limit' => 4, // the maximum times, an element can be cloned (default 999)

        'min' => 0, // 0 or 1 (default 1)

        'insertButton' => '.add-item', // css class

        'deleteButton' => '.remove-item', // css class

        'model' => $modelsAddress[0],

        'formId' => 'dynamic-form',

        'formFields' => [

            'name',

        ],

    ]); ?>

    <div class="panel panel-default">

        <div class="panel-heading">

            <i class="fa fa-envelope"></i> Test

            <button type="button" class="pull-right add-item btn btn-success btn-xs"><i class="fa fa-plus"></i> Add name</button>

             

            <div class="clearfix"></div>

        </div>

        <div class="panel-body container-items"><!-- widgetContainer -->

            <?php foreach ($modelsAddress as $index => $modelAddress): ?>

                <div class="item panel panel-default"><!-- widgetBody -->

                    <div class="panel-heading">

                        <span class="panel-title-address">Names: <?= ($index + 1) ?></span>

                        <button type="button" class="pull-right remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>

                        <div class="clearfix"></div>

                    </div>

                    <div class="panel-body">

                        <?php
                       $testr=5;
                            // necessary for update action.

                            if (!$modelAddress->isNewRecord) {

                                echo Html::activeHiddenInput($modelAddress, "[{$index}]id");

                            }

                        ?>

                       
                        <?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>"idN2"]) ?>
                         

                         <button type="button" onclick="setDefaultF($(this))" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>
                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    </div>

    <?php DynamicFormWidget::end(); ?>



$this->registerJs('
        function setDefaultF(obj){

  $(\'#idN2\').val("Martin");

        }
        
', \yii\web\View::POS_HEAD) ?>

Attached File(s)


0

#2 User is offline   Owais Ali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-December 17
  • Location:Karachi

Posted 18 December 2017 - 12:18 AM

You are trying to assign id='idN2' for all your text fields in the form. You cannot. You need to assign unique different ids. Try this:

<?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>$index]) ?>
//$index is your variable from for loop
<button type="button" onclick="setDefaultF($index)" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>



and in jquery do:

$this->registerJs("

function setDefaultF(id){
    $('#'.id).val(\"Martin\");
}
", \yii\web\View::POS_HEAD) ?>


0

#3 User is offline   Najpox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-November 17

Posted 18 December 2017 - 04:59 AM

View PostOwais Ali, on 18 December 2017 - 12:18 AM, said:

You are trying to assign id='idN2' for all your text fields in the form. You cannot. You need to assign unique different ids. Try this:

<?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>$index]) ?>
//$index is your variable from for loop
<button type="button" onclick="setDefaultF($index)" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>



and in jquery do:

$this->registerJs("

function setDefaultF(id){
    $('#'.id).val(\"Martin\");
}
", \yii\web\View::POS_HEAD) ?>





Thank you for your answer, yes, I am tried assign one id='idN2', because, I wanted only testing access to textfield in dynamic form. I can change value in textfield, which is out of dynamicform, but i Cant change value in textfield, which is in dynamicform.

I tried your advice, but nothing happened. Also I found similar problem on the forum and I tried different access to textfield in dynamicform, but also, nothing happened, when I tried in JQuery this:
$('#modelAddress-' + id+ '-name').val("Martin");

, where 'id'=>$index, how you wrote.
0

#4 User is offline   Owais Ali 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-December 17
  • Location:Karachi

Posted 21 December 2017 - 03:39 AM

View PostNajpox, on 18 December 2017 - 04:59 AM, said:

Thank you for your answer, yes, I am tried assign one id='idN2', because, I wanted only testing access to textfield in dynamic form. I can change value in textfield, which is out of dynamicform, but i Cant change value in textfield, which is in dynamicform.

I tried your advice, but nothing happened. Also I found similar problem on the forum and I tried different access to textfield in dynamicform, but also, nothing happened, when I tried in JQuery this:
$('#modelAddress-' + id+ '-name').val("Martin");

, where 'id'=>$index, how you wrote.


You cannot assign one id to multiple text fields (first learn about ids in HTML). You need to assign different id to text fields, which means:
'id'=>$index

so your text field will have id as
'id'=>0
'id'=>1
.
.
so on

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users