Yii2 更改其他字段的属性
我需要您更改产品字段(dropdownlist)以更改最小和最大属性kartik\SLIDER组件 ----形式---- 已编辑 每个产品都有一个最大值和最小佣金金额。我不知道如何捕捉这些值并将其转换为滑块的属性max和min 编辑2 我的控制器actionListx返回: 无效参数–yii\base\InvalidParamException 响应内容不能是数组 我的表格:Yii2 更改其他字段的属性,yii2,Yii2,我需要您更改产品字段(dropdownlist)以更改最小和最大属性kartik\SLIDER组件 ----形式---- 已编辑 每个产品都有一个最大值和最小佣金金额。我不知道如何捕捉这些值并将其转换为滑块的属性max和min 编辑2 我的控制器actionListx返回: 无效参数–yii\base\InvalidParamException 响应内容不能是数组 我的表格: <?php $productId = Html::getInputId($model, 'product_id')
<?php
$productId = Html::getInputId($model, 'product_id');
$comissionId = Html::getInputId($model, 'commission_percent');
$url = \Yii::$app->getUrlManager()->createUrl('/dailyproductivity/listx');
$js = <<<JS
$('#{$productId}').on('change', function () {
var form = $('#dailyproductivity-form');
$.ajax({
url: '{$url}',
type: 'post',
data: form.serialize(),
success: function(data) {
var min = data.min;
var max = data.max;
$("#{$comissionId}").data('slider').options.min = min;
$("#{$comissionId}").data('slider').options.max = max;
$("#{$comissionId}").slider('setValue', min);
}
});
});
JS;
$this->registerJs($js);
?>
印刷品
如果您不在数据库中的某个地方保存这个最小值和最大值以及相应的ID(我认为您应该这样做),那么您可以使用纯js进行保存。以下是一个例子:
<?php
$productId = Html::getInputId($model, 'product_id');
$comissionId = Html::getInputId($model, 'commission_percent');
$js = <<<JS
$('#{$productId}').on('change', function () {
var id = $(this).val();
if (id == 14) {
var min = 30;
var max = 40;
} else {
var min = 0;
var max = 100;
}
$("#{$comissionId}").data('slider').options.min = min;
$("#{$comissionId}").data('slider').options.max = max;
$("#{$comissionId}").slider('setValue', min);
});
JS;
$this->registerJs($js);
和您的控制器:
$productId = Html::getInputId($model, 'product_id');
$comissionId = Html::getInputId($model, 'commission_percent');
$url = Url::to(['controller/action-with-ajax']);
$js = <<<JS
$('#{$productId}').on('change', function () {
var form = $('#my-form-id');
$.ajax({
url: '{$url}',
type: 'post',
data: form.serialize(),
success: function(data) {
var min = data.min;
var max = data.max;
$("#{$comissionId}").data('slider').options.min = min;
$("#{$comissionId}").data('slider').options.max = max;
$("#{$comissionId}").slider('setValue', min);
}
});
});
JS;
$this->registerJs($js);
public function actionWithAjax()
{
$model = new Model(); // the model you are using in this form
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
if ($product = Product::findOne($model->product_id)) !== null) {
return [
'min' => $product->min_value,
'max' => $product->max_value,
];
}
}
return [
'min' => '0',
'max' => '100'
];
}
如果您没有将这个最小值和最大值以及相应的ID保存在db中的某个位置(我认为您应该这样做),那么您可以使用纯js进行保存。以下是一个例子:
<?php
$productId = Html::getInputId($model, 'product_id');
$comissionId = Html::getInputId($model, 'commission_percent');
$js = <<<JS
$('#{$productId}').on('change', function () {
var id = $(this).val();
if (id == 14) {
var min = 30;
var max = 40;
} else {
var min = 0;
var max = 100;
}
$("#{$comissionId}").data('slider').options.min = min;
$("#{$comissionId}").data('slider').options.max = max;
$("#{$comissionId}").slider('setValue', min);
});
JS;
$this->registerJs($js);
和您的控制器:
$productId = Html::getInputId($model, 'product_id');
$comissionId = Html::getInputId($model, 'commission_percent');
$url = Url::to(['controller/action-with-ajax']);
$js = <<<JS
$('#{$productId}').on('change', function () {
var form = $('#my-form-id');
$.ajax({
url: '{$url}',
type: 'post',
data: form.serialize(),
success: function(data) {
var min = data.min;
var max = data.max;
$("#{$comissionId}").data('slider').options.min = min;
$("#{$comissionId}").data('slider').options.max = max;
$("#{$comissionId}").slider('setValue', min);
}
});
});
JS;
$this->registerJs($js);
public function actionWithAjax()
{
$model = new Model(); // the model you are using in this form
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
if ($product = Product::findOne($model->product_id)) !== null) {
return [
'min' => $product->min_value,
'max' => $product->max_value,
];
}
}
return [
'min' => '0',
'max' => '100'
];
}
有什么问题吗?.Hi@RumesShyaman我不知道怎么回事。有什么问题吗?.Hi@RumesShyaman我不知道怎么回事。酷。我觉得你的想法很有趣。对于每个产品id,我都会有一个最大和最小列?是的,我建议您这样做。也许可以在产品表中添加这两个额外的列。因此,在您的
js
和规则中,您将检查该值并将其赋给max
和min
变量,而不是执行if语句
,在js
中,只需用ajax
调用替换if
,在stackoverflow中有很多关于如何实现这一点的示例。您调用的操作将尝试查找此产品,并返回它或仅返回这两个值。在您的规则中也有同样的内容,请尝试查找产品,并获取这两个属性。如果您在尝试应用时发现任何问题,请告诉我。:)可以告诉我如何通过ajax获得最小值和最大值,并将这些值分配给变量最小和最大滑块组件吗?酷。我觉得你的想法很有趣。对于每个产品id,我都会有一个最大和最小列?是的,我建议您这样做。也许可以在产品表中添加这两个额外的列。因此,在您的js
和规则中,您将检查该值并将其赋给max
和min
变量,而不是执行if语句
,在js
中,只需用ajax
调用替换if
,在stackoverflow中有很多关于如何实现这一点的示例。您调用的操作将尝试查找此产品,并返回它或仅返回这两个值。在您的规则中也有同样的内容,请尝试查找产品,并获取这两个属性。如果您在尝试应用时发现任何问题,请告诉我。:)可以告诉我如何通过ajax获取最小值和最大值,并将这些值分配给变量“最小值”和“最大值”滑块组件吗?
public function actionWithAjax()
{
$model = new Model(); // the model you are using in this form
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
if ($product = Product::findOne($model->product_id)) !== null) {
return [
'min' => $product->min_value,
'max' => $product->max_value,
];
}
}
return [
'min' => '0',
'max' => '100'
];
}