簡體   English   中英

Yii2表單輸入字段計數與db的值

[英]Yii2 form input field counts with value from db

我在yii2做我的項目。 我有從db到下拉列表獲取數據的表單(使用kartik depdtop小部件)。 第一個字段是“type_of_goods”,取決於“type_of_goods”客戶收到“商品”。 在它們是兩個文本輸入字段后,“goods_amount”和“total_cost”的商品取決於“goods_amount”(goods_amount * price)。

客戶輸入他想要購買的商品金額,或者他想要購買的金額,js腳本在兩種情況下都顯示他在另一個領域的價值。

價格值在DB的貨物表中。 我可以從“貨物”字段中獲取goods_id或一些有關商品的信息(執行數據庫查詢並獲取價格並將其放入js函數中),或者甚至可以將價格放入js腳本中,這樣做我上面寫的內容。

我怎么才能意識到這一點? 這是正確的方法嗎?

代碼:查看

<?php $form = ActiveForm::begin([
            'options' => [
            'class' => 'form-horizontal col-lg-11',
            'enctype' => 'multipart/form-data'
            ],
        ]); ?>
    <?= $form->field($type_of_goods, 'id')
        ->dropDownList(
            ArrayHelper::map(Type_of_goods::find()->all(), 'id', 'name'),      
            ['id'=>'type_of_goods_id']
        );
    ?>
    <?= $form->field($goods, 'id')->widget(DepDrop::classname(), [
            'options' => ['id' => 'id'],
            'pluginOptions' => [
                    'depends' => ['type_of_goods_id'],
                    'placeholder' => 'Choose your goods',
                    'url' => \yii\helpers\Url::to(['goods/goods-dep-type'])
            ]
        ]);
    ?>

    <?= $form->field($goods, 'price')->textInput();

    ?>

    <?= $form->field($order, 'amount')->textInput();

    ?>

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

控制器:

public function actionGoodsDepType()
{
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];

        if ($parents != null) {
            $game_id = $parents[0];
            $out = \app\models\Goods::gelGoodsList($goods_type_id);
            echo Json::encode(['output' => $out, 'selected' => '']);
            return;
        }
    }
    echo Json::encode(['output' => '', 'selected' => '']);
}

模型:

public static function gelGoodsList($type_id)
{
    $query = self::find()
        ->where(['type_id' => $type_id])
        ->select(['id', 'name'])->asArray()->all();

    return $query;
}
 public static function getPrice($id)
{
    $query = self::find()
        ->where(['id' => $id])
        ->select(['price'])->asArray()->one();

    return $query;
}

每次用戶輸入新值時,您都需要創建一個AJAX請求。 這可能很好用(放在視圖文件的底部):

$this->registerJs("
    $('#Type_of_goods-price').on('change', function() {    // Should be ID of a field where user writes something
        $.post('index.php?r=controller/get-price', {       // Controller name and action
            input_price: $('#Type_of_goods-price').val()   // We need to pass parameter 'input_price'
        }, function(data) {
            total_price = $.parseJSON(data)['result'];     // Let's say, controller returns: json_encode(['result' => $totalPrice]);
            $('#Type_of_goods-total').value = total_price; // Assign returned value to a different field that displays total amount of price
        }
    });
");

您只需要設置正確的元素的正確ID並在控制器中編寫一個方法(使用此示例,它將是controller控制器名稱和actionGetPrice方法名稱)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM