MKD
MKD

Reputation: 383

How to create a new input field which would offer values stored in different database table? Yii2

It's pretty hard to formulate the question correctly, but I'll try to explain it more here. I have an Item, Category and Sale models. In category and Item models I manually stored data. Now I've created a form, which has some input fields and dependent drop-down, which is:

According to the selected category, items are loaded.

Now I need to create a new input field, which would be price and it has to be taken from the database as a offer (f.e if the user selects Item1 -> Item1 price has to be loaded up as a offer in the input field, which I could edit it and store in different database table.

How should I do that?

Here is my SaleController action:

    /**
 * Creates a new sale
 * 
 * @return string
 */
public function actionCreate()
{   
    $model = new Sale();
    $model->scenario = Sale::SCENARIO_CREATE;
    $category = new Category();
    $category->scenario = Category::SCENARIO_CREATE;
    $categoryList = ArrayHelper::map(Category::find()->all(), 'id', 'name');

    if ($model->load(Yii::$app->request->post())) {
        if ($model->save()) {
            return $this->redirect(['index']);
        }
    }
    return $this->render('create', [
        'model' => $model,
        'category' => $category,
        'categoryList' => $categoryList,
    ]);
}

And here is my view:

        <?= $form->field($category, 'id')->dropDownList($categoryList, [
        'id' => 'category-id',
        'prompt' => 'Choose a category',
    ]); ?>

    <?= $form->field($model, 'item_id')->widget(DepDrop::classname(), [
        'options'=>['id'=>'item-id'],
        'pluginOptions'=>[
            'depends'=>['category-id'],
            'placeholder'=> 'Choose an item',
            'url'=>Url::to(['/sale/subcat'])
        ]
    ]); ?>

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

Thank you for any help. I hope you understand the question

Upvotes: 0

Views: 75

Answers (1)

Vladimir
Vladimir

Reputation: 445

First you must add an input field in your view from model:

<?= $form->field($model, 'item_price')->textInput([
       'maxlength' => true,
       'id' => 'input-price-id'
   ]) ?>

Then you need to add some javascript:

<?php
$item_price=<<<JS
$('#item-id').on('change', function(event) {
        var id = $(this).attr('id');
        var val = $(this).val();        
        .get(
            // you must code function actionRefreshPrice wtih parameter named item_id in 
            // your controller with fetch from any table you want
            'refresh-price',         
            {                
                item_id: val // this is id from your item
            },
            function (data) {
                // here you set a value returned from ajax call
                // you must have an input element with id input-price-is (change as you like)
                $('#input-price-id').val(data);            
            }  
        );
});
JS;
$this->registerJs($item_price);
?>

Then you must add a controller action something like this:

public function actionRefreshPrice($item_id) {
        // I asume you have table with Price for items prices
        $price = Price::findOne(['item_id'=>$item_id]);
        return ($price?0:$price->price_field);
}

I hope I have given you enough guidelines. And a comment learn more about models and relations. I think you overthought problem a little bit. Happy learning and coding.

Upvotes: 1

Related Questions