gpt4 book ai didi

javascript - 如何在 yii2 中的单个 Controller 中使用多个场景?

转载 作者:行者123 更新时间:2023-11-28 06:13:16 26 4
gpt4 key购买 nike

大家好,如何在 yii2 中的单个 Controller 操作中使用多个场景。i 有一个 onchange 下拉列表,依赖于 onchange 表单字段将发生变化。所以我使用了场景,但 Controller 只接受一种场景。是否可以在一个场景中使用多个场景单一操作。请帮忙..

Model:

public function rules()
{
return [
//[['discount_type', 'discount_name', 'discount_percentage', 'discount_from_quantity','discount_to_quantity' ,'discount_start_date', 'discount_end_date', 'discount_status', 'access_code', 'code_discount', 'no_of_tickets', 'status','ticket_name'], 'required'],
[['discount_type','ticket_name'],'required'],
[['event_id', 'ticket_id', 'discount_from_quantity','discount_to_quantity', 'no_of_tickets', 'status'], 'integer'],
[['discount_type', 'discount_status','ticket_name'], 'string'],
[['discount_start_date', 'discount_end_date', 'timestamp'], 'safe'],
[['discount_name'], 'string', 'max' => 255],
[['discount_percentage', 'code_discount','access_code'], 'string', 'max' => 100,],
[['discount_start_date', 'discount_end_date','access_code','no_of_tickets'],'required','on' => 'access'],
[['discount_start_date', 'discount_end_date','code_discount','no_of_tickets','discount_percentage'],'required','on' => 'code'],
[['discount_name','discount_percentage','discount_start_date', 'discount_end_date','no_of_tickets','discount_from_quantity','discount_to_quantity'],'required','on' => 'group'],
];

}
Controller:

public function actionCreate($id)
{
//echo $id;
$model_discount= new EventticketDiscount();
$model_discount->scenario = 'code';
$model_ticketid = new EventTicket_ex();

$ticketid=$model_ticketid->find()->where(['event_id'=>$id])->one();

$ticketname=ArrayHelper::map($model_ticketid->find()->where(['event_id'=>$id])->all(),'ticket_id','ticket_name');


if ($model_discount->load(Yii::$app->request->post())) {

$model_discount->attributes=$_POST['EventticketDiscount'];

if(isset($_POST['EventticketDiscount']['ticket_name']) && $_POST['EventticketDiscount']['ticket_name']!==array())
$model_discount->ticket_name=implode(',',$_POST['EventticketDiscount']['ticket_name']);


$model_discount->event_id = $id;
$model_discount->ticket_id = $ticketid->ticket_id;
$model_discount->save();


return $this->redirect(['view', 'id' => $model_discount->discount_id]);
} else {
return $this->renderAjax('create', [
'model_discount' => $model_discount,'ticketname'=>$ticketname,
]);
}
}
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model_discount, 'discount_type')
->dropDownList(ZHtml::enumItem($model_discount, 'discount_type'),['prompt'=>'Select Type','onchange'=>'js:discountShow(this.value)'])
->label(FALSE)
?>

<?= $form->field($model_discount, 'event_id')->hiddenInput(['value'=>""])->label(false); ?>

<?= $form->field($model_discount, 'ticket_id')->hiddenInput(['value'=>""])->label(false); ?>


<div id="discountname" style="display:none">


<?= $form->field($model_discount, 'discount_name')->textInput(['maxlength' => true]) ?>
</div>

<div id="discountpercentage" style="display:none">
<?= $form->field($model_discount, 'discount_percentage')->textInput(['maxlength' => true]) ?>
</div>

<div id="discountdate" style="display:none">

<?= $form->field($model_discount, 'discount_start_date')->textInput() ?>

<?= $form->field($model_discount, 'discount_end_date')->textInput() ?>
</div>


<div id="accesscode" style="display:none">
<?= $form->field($model_discount, 'access_code')->textInput(['maxlength' => true]) ?>
</div>

<div id="discountcode" style="display:none">

<?= $form->field($model_discount, 'code_discount')->textInput(['maxlength' => true]) ?>
</div>

<div id="discountcount" style="display:none">
<?= $form->field($model_discount, 'no_of_tickets')->textInput() ?>
</div>

<div id="discountquantity" style="display:none">
<?= $form->field($model_discount, 'discount_from_quantity')->textInput() ?>

<?= $form->field($model_discount, 'discount_to_quantity')->textInput() ?>
</div>

<div id="ticketname" style="display:none">
<?= $form ->field($model_discount, 'ticket_name[]')
->checkboxList($ticketname)->label('Select Tickets');?>
</div>


<div id="discountstatus" style="display:none">
<!-- $form->field($model_discount, 'discount_status')->dropDownList(ZHtml::enumItem($model_discount, 'discount_status'),['prompt' => 'Select Status'])-->

<?= $form->field($model_discount, 'status')->hiddenInput(['value'=>1])->label(false); ?>

<?= $form->field($model_discount, 'timestamp')->hiddenInput()->label(false);?>
</div>

<div class="form-group">
<?= Html::submitButton($model_discount->isNewRecord ? 'Create' : 'Update', ['class' => $model_discount->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>

</div>



<script>
function discountShow(type)
{
alert(type);


if(type=="Group Discount"){
document.getElementById('discountname').style.display="block";
document.getElementById('ticketname').style.display="block";
document.getElementById('discountpercentage').style.display="block";
document.getElementById('discountdate').style.display="block";
document.getElementById('discountcount').style.display="block";
document.getElementById('discountquantity').style.display="block";
document.getElementById('discountstatus').style.display="block";
document.getElementById('accesscode').style.display="none";
document.getElementById('discountcode').style.display="none";


}
else if(type=="Access Code Discount"){

document.getElementById('discountname').style.display="none";
document.getElementById('discountpercentage').style.display="none";
document.getElementById('discountquantity').style.display="none";
document.getElementById('discountcode').style.display="none";
document.getElementById('discountdate').style.display="block";
document.getElementById('accesscode').style.display="block";
document.getElementById('discountcount').style.display="block";
document.getElementById('discountstatus').style.display="block";
document.getElementById('ticketname').style.display="block";
//return false;
}

else if(type=="Code Discount"){
document.getElementById('discountpercentage').style.display="block";
document.getElementById('discountdate').style.display="block";
document.getElementById('discountcode').style.display="block";
document.getElementById('discountcount').style.display="block";
document.getElementById('discountstatus').style.display="block";
document.getElementById('discountname').style.display="none";
document.getElementById('accesscode').style.display="none";
document.getElementById('discountquantity').style.display="none";
document.getElementById('ticketname').style.display="block";
}


最佳答案

您必须在事件表单的选项中设置场景选项。如果你愿意的话,你可以用 jquery 来改变它。还要为表单添加一个 ID,以便客户端验证也能正常工作。

关于javascript - 如何在 yii2 中的单个 Controller 中使用多个场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com