2016-08-13 10 views
0

yii2にwbragancaの動的フォームを追加しました。このフォームの中でproductnameとbatchnoの2つのドロップダウンが選択されているときにitemidにテキストボックスを設定しようとしています。私は値を得ていますが、テキストボックスにそれを渡すことはできません。助けてください。wbraganca内のテキストボックスに自動的に値を入力するyii2の動的フォーム

私のフォームは次のようになります -

<div class="bills-form"> 

    <?php $form = ActiveForm::begin([ 
     'id' => 'dynamic-form', 
     'type' => ActiveForm::TYPE_HORIZONTAL, 
     'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL] 
    ]); ?> 

    <?= $form->field($model, 'bills_partyname')->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(Parties::find()->orderBy(['parties_partyname' => SORT_ASC,])->all(),'parties_partyname','partyDetails'), 
    'language' => 'en', 
    'options' => ['placeholder' => 'Select Party Name', 'id' => 'partyid'], 
    'pluginOptions' => [ 
     'allowClear' => true 
    ], 
    ]); ?> 

    <?= $form->field($model, 'transport')->textInput(['maxlength' => true, 'readOnly'=>true]) ?> 

    <?= $form->field($model, 'bills_ebillid')->textInput() ?> 

    <?= $form->field($model, 'bills_year')->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(Year::find()->orderBy(['yid' => SORT_DESC,])->all(),'year_year','year_year'), 
    'language' => 'en', 
    'options' => ['placeholder' => 'Select Year', 'id' => 'yearid'], 
    'pluginOptions' => [ 
     'allowClear' => true 
    ], 
    ]); ?> 

    <?= $form->field($model, 'console')->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(Console::find()->orderBy(['consoleid' => SORT_ASC,])->all(),'console','console'), 
    'language' => 'en', 
    'options' => ['placeholder' => 'Select Console','id' => 'consoleid'], 
    'pluginOptions' => [ 
     'allowClear' => true 
    ], 
    ]); ?> 

    <?= $form->field($model, 'num')->textInput() ?> 

    <?= $form->field($model, 'billno')->textInput(['maxlength' => true]) ?> 

    <?= $form->field($model, 'billdate')->widget(
        DatePicker::className(), [ 
        // inline too, not bad 
        'inline' => false, 
        // modify template for custom rendering 
        //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>', 
        'clientOptions' => [ 
         'autoclose' => true, 
         'todayHighlight' => true, 
         'format' => 'yyyy-mm-dd' 

        ] 

        ]);?> 

    <div class="row"> 
     <div class="panel panel-default"> 
     <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Products</h4></div> 
     <div class="panel-body"> 
      <?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' => 20, // the maximum times, an element can be cloned (default 999) 
       'min' => 1, // 0 or 1 (default 1) 
       'insertButton' => '.add-item', // css class 
       'deleteButton' => '.remove-item', // css class 
       'model' => $modelsProductsales[0], 
       'formId' => 'dynamic-form', 
       'formFields' => [ 
        'productname', 
        //'batchno', 
        'qty', 
        'free', 
        'total', 

       ], 
      ]); ?> 

      <div class="container-items"><!-- widgetContainer --> 
      <?php foreach ($modelsProductsales as $i => $modelsProductsales): ?> 
       <div class="item panel panel-default"><!-- widgetBody --> 
        <div class="panel-heading"> 
         <h3 class="panel-title pull-left">Products</h3> 
         <div class="pull-right"> 
          <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button> 
          <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button> 
         </div> 
         <div class="clearfix"></div> 
        </div> 
        <div class="panel-body"> 
         <?php 
          // necessary for update action. 
          if (! $modelsProductsales->isNewRecord) { 
           echo Html::activeHiddenInput($modelsProductsales, "[{$i}]id"); 
          } 
         ?> 
         <div class="row"> 
          <div class="col-xs-3 col-sm-3 col-lg-3"> 

           <?= $form->field($modelsProductsales, "[{$i}]productname")->widget(Select2::classname(), [ 
           'data' => ArrayHelper::map(Productbatch::find()->orderBy(['productname' => SORT_ASC,])->all(),'productname','productname'), 
           'language' => 'en', 
           'options' => ['placeholder' => 'Select Productname', 'id' => 'productid'], 
           'pluginOptions' => [ 
            'allowClear' => true 
           ], 
           ]); ?> 
          </div> 
          <div class="col-xs-3 col-sm-3 col-lg-3"> 
           <?= $form->field($modelsProductsales, "[{$i}]qty")->textInput(['maxlength' => true]) ?> 
          </div> 
          <div class="col-xs-3 col-sm-3 col-lg-3"> 
           <?= $form->field($modelsProductsales, "[{$i}]free")->textInput(['maxlength' => true]) ?> 
          </div> 
          <div class="col-xs-3 col-sm-3 col-lg-3"> 
           <?= $form->field($modelsProductsales, "[{$i}]total")->textInput(['maxlength' => true]) ?> 
          </div> 
          <?= $form->field($modelsProductsales, "[{$i}]itemid")->textInput(['maxlength' => true]) ?> 
          <?= $form->field($modelsProductsales, "[{$i}]batchno")->widget(DepDrop::classname(), [ 
          'options'=>['id'=>'batchid'], 
          'pluginOptions'=>[ 
           'depends'=>['productid'], 
           'placeholder'=>'BatchNo', 
           'url'=>Url::to(['/invoice/bills/subcat']) 
          ] 
          ]); ?> 
          <?= $form->field($modelsProductsales, "[{$i}]expdate")->textInput(['maxlength' => true]) ?> 
          <?= $form->field($modelsProductsales, "[{$i}]mrp")->textInput(['maxlength' => true]) ?> 
          <?= $form->field($modelsProductsales, "[{$i}]rate")->textInput(['maxlength' => true]) ?> 
          <?= $form->field($modelsProductsales, "[{$i}]discount")->textInput(['maxlength' => true]) ?> 
         </div><!-- .row --> 

        </div> 
       </div> 
      <?php endforeach; ?> 
     </div> 
      <?php DynamicFormWidget::end(); ?> 
     </div> 
     </div> 
    </div> 

    <?= $form->field($model, 'billamount')->textInput() ?> 

    <?= $form->field($model, 'transport')->textInput(['maxlength' => true]) ?> 

    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

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

</div> 

そして、私のjavascriptのコードがある -

<?php 
/* start getting the itemid and expdate */ 
$script = <<< JS 
$(function(){ 
    $('#productid').change(function(){ 
     getItemid(); 
    }); 
    $('#batchid').change(function(){ 
     getItemid(); 
    }); 

    var productid = $(this).val(); 
    var batchid = $(this).val(); 

    var getItemid = function(){ 
     var productid = String($('#productid').val()); 
     var batchid = String($('#batchid').val()); 
     $.get('index.php?r=invoice/bills/get-for-itemid',{ productid : productid, batchid : batchid }, function(data){ 
     //alert(data); 
     var data = $.parseJSON(data); 
     var getItemid = data; 
     $('#dynamic-[{$i}]itemid').val(getItemid["itemid"]); 
     }); 

    } ; 

}); 


JS; 
$this->registerJs($script); 
/* end getting the itemid and expdate */ 
?> 

以下のスクリーンショットを参照してください。私が何をしたいです -

<div class="col-xs-3 col-sm-3 col-lg-3"> 

            <?= $form->field($modelsProductsales, "[{$i}]productname")->label(false)->textInput(['onchange' => 'getItemdetails($(this))', 'onkeyup' => 'getItemdetails($(this))'])->widget(Select2::classname(), [ 
            'data' => ArrayHelper::map(Productbatch::find()->orderBy('productname')->all(),'productname','productname'), 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select Product','id' => 'prodname'], 
            'pluginOptions' => [ 
             'allowClear' => true 
            ], 
            ]); 
           ?>         
           </div> 
           <div class="col-xs-2 col-sm-2 col-lg-2 nopadding"> 
            <?= $form->field($modelsProductsales, 'batchno')->label(false)->textInput(['onchange' => 'getItemdetails($(this))', 'onkeyup' => 'getItemdetails($(this))'])->widget(DepDrop::classname(), [ 
            'options'=>['id'=>'batchno'], 
            'pluginOptions'=>[ 
             'depends'=>['prodname'], 
             'placeholder'=>'Batch No', 
             'url'=>Url::to(['/invoice/bills/subcat']) 
            ] 
            ]); ?> 

しかし、誰のアイデアについて - 私はYii2-dynamicforms and javascriptをチェックしないと、他には以下のように依存している上の2つのフォームフィールド(PRODUCTNAME、batchno)を更新しました enter image description here

更新JavaScriptの部分。現時点では、Javascriptを以下のようになります -

<? 
/* start getting the itemid */ 
$script = <<< JS 
$(function(){ 
    $('#productsales-{$i}-prodname').change(function(){ 
     getItemid(); 
    }); 
    $('#productsales-{$i}-batchno').change(function(){ 
     getItemid(); 
    }); 

    var prodname = $('#productsales-{$i}-batchno').val(); 
    var batchno = $('#productsales-{$i}-batchno').val(); 

    var getItemid = function(){ 
     var prodname = String($('#productsales-{$i}-prodname').val()); 
     var batchno = String($('#productsales-{$i}-batchno').val()); 
     $.get('index.php?r=invoice/bills/get-for-itemid',{ prodname : productsales-{$i}-prodname, batchno : productsales-{$i}-batchno }, function(data){ 
     alert(data); 
     //var data = $.parseJSON(data); 
     //var getItemid = data; 
     //$('#productsales-{$i}-itemid').val(getItemid["itemid"]); 
     }); 

    } ; 

}); 
JS; 
$this->registerJs($script); 
/* end getting the itemid */ 
?> 

アップデート:ただの答えからコピー:Insaneskull

フォームの実装答えを。

<div class="col-xs-3 col-sm-3 col-lg-3"> 
            <?= $form->field($modelsProductsales, "[{$i}]productname")->widget(Select2::classname(), [ 
            'data' => ArrayHelper::map(Productbatch::find()->orderBy('productname')->all(),'productname','productname'), 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select Product', 'onchange' => 'getItemID($(this))'], 
            'pluginOptions' => [ 
             'allowClear' => true 
            ], 
            ])->label(false); 
           ?> 
           </div> 
           <div class="col-xs-2 col-sm-2 col-lg-2 nopadding"> 
            <?= $form->field($modelsProductsales, 'batchno')->widget(DepDrop::classname(), [ 
            'options' => ['onchange' => 'getItemID($(this))'], 
            'pluginOptions'=>[ 
             'depends'=> [Html::getInputID($modelsProductsales, "[{$index}]productname")], 
             'placeholder'=>'Batch No', 
             'url'=>Url::to(['/invoice/bills/subcat']) 
            ] 
            ])->label(false); ?> 
           </div> 

、フォームの一番下に登録されたJS機能 -

<?php 
/* start getting the itemid */ 
$script = <<< JS 
function getItemID(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var batch = product = 0; 
    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    if (myString == "productname") { 
     fetch = index.concat("-batchno"); 
     product = item.val(); 
     batch = $("#productsales-"+fetch+"").val(); 
    } else { 
     fetch = index.concat("-productname"); 
     batch = item.val(); 
     product = $("#productsales-"+fetch+"").val(); 
    } 
    $.get('index.php?r=invoice/bills/get-for-itemid',{ prodname : product, batchno : batch }, function(data){ 
     var data = $.parseJSON(data); 
     var getItemid = data; 
     itemID = "productsales-".concat(index).concat("-itemid"); 
     $("#"+itemID+"").val(getItemid["itemid"]); 
    }); 
} 
JS; 
$this->registerJs($script); 
/* end getting the itemid */ 
?> 

そして、私は取得していますエラー - enter image description here データは、Selectセレクトウィジェットで選択取得されていない enter image description here 現在のJavascript

<?php 
/* start getting the itemid */ 
$script = <<< JS 
function getItemID(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var batch = product = 0; 
    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    if (myString == "productname") { 
     fetch = index.concat("-batchno"); 
     product = item.val(); 
     batch = $("#productsales-"+fetch+"").val(); 
    } else { 
     fetch = index.concat("-productname"); 
     batch = item.val(); 
     product = $("#productsales-"+fetch+"").val(); 
    } 
    $.get('index.php?r=invoice/bills/get-for-itemid',{ prodname : product, batchno : batch }, function(data){ 
     var data = $.parseJSON(data); 
     var getItemid = data; 
     itemID = "productsales-".concat(index).concat("-itemid"); 
     $("#"+itemID+"").val(getItemid["itemid"]); 
    }); 
} 
JS; 
$this->registerJs($script, View::POS_END); 
/* end getting the itemid */ 
?> 

エラー enter image description here

+0

'$( '#の動的 - [{$ iは}]アイテムID')のval(getItemid ["itemid"]); 'は動作しません。 –

+0

どうすればいいか教えてください。 – Tanmay

+0

@InsaneSkull、これを得るための最良の方法は何か教えてください。私の最後からもう一度入力が必要な場合は、私にもお知らせください。 – Tanmay

答えて

1

_form.php

<div class="col-xs-3 col-sm-3 col-lg-3"> 
    <?= $form->field($modelsProductsales, "[{$i}]productname")->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(Productbatch::find()->orderBy('productname')->all(),'productname','productname'), 
    'language' => 'en', 
    'options' => ['placeholder' => 'Select Product', 'onchange' => 'getItemID($(this))'], 
    'pluginOptions' => [ 
     'allowClear' => true 
    ], 
    ])->label(false); 
?> 
</div> 
<div class="col-xs-2 col-sm-2 col-lg-2 nopadding"> 
    <?= $form->field($modelsProductsales, '[{$i}]batchno')->widget(DepDrop::classname(), [ 
    'options' => ['onchange' => 'getItemID($(this))'], 
    'pluginOptions'=>[ 
     'depends'=> [Html::getInputID($modelsProductsales, "[{$i}]productname")], 
     'placeholder'=>'Batch No', 
     'url'=>Url::to(['/invoice/bills/subcat']) 
    ] 
    ])->label(false); ?> 
</div> 

JS

function getItemID(item) { 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var batch = product = 0; 
    var id = item.attr("id"); 
    var myString = id.split("-").pop(); 

    if (myString == "productname") { 
     fetch = index.concat("-batchno"); 
     product = item.val(); 
     batch = $("#productsales-"+fetch+"").val(); 
    } else { 
     fetch = index.concat("-productname"); 
     batch = item.val(); 
     product = $("#productsales-"+fetch+"").val(); 
    } 
    $.get('index.php?r=invoice/bills/get-for-itemid',{ prodname : product, batchno : batch }, function(data){ 
     var data = $.parseJSON(data); 
     var getItemid = data; 
     itemID = "productsales-".concat(index).concat("-itemid"); 
     $("#"+itemID+"").val(getItemid["itemid"]); 
    }); 
} 
+0

Insane Skull、私はあなたのようにフォームを追加し、js関数を登録しました。質問に両方のコードを追加しました。今エラーを取得する - 未定義の変数:インデックス。質問にスクリーン印刷を追加しました。私が何か間違っていると私に知らせてください。答えには多くの感謝があります。 – Tanmay

+0

@Tanmay。 '$ index'の代わりに' $ i'を使います。更新された回答をご覧ください。 –

+0

select2からデータを選択しているときに選択されていません。 Firebugのエラーは "ReferenceError:getItemIDが定義されていません"です。私は質問にスクリーン印刷を追加しました。 – Tanmay

関連する問題