【问题标题】:Yii2 form input field counts with value from dbYii2 表单输入字段计数与来自 d​​b 的值
【发布时间】:2017-01-28 17:11:08
【问题描述】:

我在 yii2 中做我的项目。 我有从 db 获取数据到下拉列表的表单(使用 kartik depdtop 小部件)。 第一个字段是“type_of_goods”,取决于“type_of_goods”客户收到“货物”。 之后是“goods_amount”和“total_cost”的两个文本输入字段,商品取决于“goods_amount”(goods_amount * price)。

客户输入他想购买的商品数量,或者他想购买的金额,js脚本在这两种情况下都会在另一个字段中显示他的价值。

价格值在数据库中的商品表中。 我可以从“商品”字段中获取商品 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;
}

【问题讨论】:

    标签: forms yii2


    【解决方案1】:

    每次用户输入新值时,您都需要创建一个 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方法名称)。

    【讨论】:

    • 非常感谢,但不知道它是否有效,我输入了所有值,我的依赖下拉列表停止工作。不知道为什么
    • 我无法确定,因为它缺少一点信息。我写了近似的解决方案。如果依赖下拉菜单停止工作,请检查控制台是否有任何错误。如果没有,那么可能在“网络”部分?
    • 你能解释一下吗,控制台在参数列表后写“SyntaxError: missing )”,但我不知道该放在哪里。如果我没记错的话,缺少两个括号
    • 确实不见了。现已编辑。
    • 谢谢,但还是同样的错误,读到可以出现关于拼接developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多