我需要您更改 PRODUCT 字段(下拉列表)更改MIN和MAX属性 kartik \ SLIDER 组件
---- form ----
<?=
$form->field($model, 'product_id', [
'inputOptions' => [
'class' => 'selectpicker '
]
]
)->dropDownList(app\models\Product::getHierarchy(), ['prompt' => 'Selecione', 'class'=>'form-control required']);
?>
use kartik\slider\Slider;
echo $form->field($model, 'commission_percent')->widget(Slider::classname(), [
'name'=>'commission_percent',
'value'=>7,
'sliderColor'=>Slider::TYPE_GREY,
'handleColor'=>Slider::TYPE_SUCCESS,
'pluginOptions'=>[
'handle'=>'round',
'tooltip'=>'always',
'min'=>10,
'max'=>50,
'step'=>1,
]
]);
----规则----
['commission_percent', 'number','min'=>30,'max'=>40, 'when' => function($model) {
return $model->product_id == 14;
}],
EDITED
每种产品都有最大值和最低佣金金额。我不知道如何捕获这些值并转移到SLIDER的属性max和min。
已编辑2
我的控制器actionListx返回:
无效参数 - yii \ base \ InvalidParamException 响应内容不能是数组。
我的_form:
<?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);
?>
我的ProductivityController:
public function actionListx()
{
$model = new Dailyproductivity();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
$product = Product::findOne($model->product_id);
if ($product !== null) {
return [
'min' => $product->min_value,
'max' => $product->max_value,
];
}
}
return [
'min' => '0',
'max' => '100'
];
}
打印
答案 0 :(得分:3)
如果你没有在db中的某个地方保存这个最小值和最大值以及相应的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);
确保在模型中添加新规则:
public function rules()
{
return [
// the other rules
// ...
['commission_percent', 'number','min'=>0,'max'=>100] // the general values
['commission_percent', 'validate']
];
}
/**
* @param string $attribute
*/
public function validate($attribute)
{
if ($this->product_id == 14) {
$min = 30;
$max = 40;
if ($this->attribute < $min || $this->attribute > $max) {
$this->addError($attribute, 'error message'));
}
}
}
但是如果你有很多产品并且每个产品都有一个特定的min和max值,而不是创建一堆if statements
,你可以创建一个新的动作来检查具有这两个值的模型(最小和最大)保存并返回它们。您可以通过ajax
电话访问它。
以下是此ajax的示例(假设您在min_value
模型中添加了max_value
和Product
属性:
:
$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'
];
}