如何仅在yii2中选择后呈现视图 [英] How to render view only after selection in yii2
问题描述
我在单个视图中呈现两个视图.
I am rendering two views in my single view.
<?= $form->field($model, 't_type')->dropDownList([
'' => 'Please Select', 'Slab Based' => 'Slab Based',
'TOU Based' => 'TOU Based']) ?>
<div class="showSlab" id="slab" style="display: none">
<?php echo $this->render('_slabBased', [
'modelsTariffSlabs' => $modelsTariffSlabs,
]); ?>
</div>
<div class="showTou" id="tou" style="display: none">
<?php echo $this->render('_touBased', [
'modelsTouSlabs' => $modelsTouSlabs,
]); ?>
</div>
默认情况下,两个div都是隐藏的,但是它们都正在渲染.但我只想在选择选项基于平板"或TOU Based
By default both div's are hidden but both of them are rendering. But I want to render the form only when I select option 'Slab Based' or TOU Based
JS
$('#mdctariff-t_type').on('change', function () {
if (this.value === 'Slab Based') {
$("#slab").show();
$("#tou").hide();
} else if (this.value === 'TOU Based') {
$("#tou").show();
$("#slab").hide();
} else {
$("#slab").hide();
$("#tou").hide();
}
});
注意::呈现表单后,我也将其保存
Note: After rendering the form I am also saving it
更新1
我尝试通过ajax
$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);
doGet('$url')
function doGet(url, params) {
params = params || {};
$.get(url, params, function(response) { // requesting url which in form
$('#slab').html(response); // getting response and pushing to element with id #response
});
}
参考:如何使用AJAX渲染部分图片? Laravel 5.2
当我选择一个选项时,我将无法查看该表格.在我的Network
选项卡中,出现错误Not Found (#404): Page not found.
.生成的URL
是http://localhost/mdc/backend/web/mdctariff/_slabBased
When I selects an option I am not able to view the form. In my Network
tab I am getting error Not Found (#404): Page not found.
. The URL
generated is http://localhost/mdc/backend/web/mdctariff/_slabBased
在我的浏览器中粘贴此URL时,出现了同样的错误.我一定想念我不知道的东西
While pasting this URL at my browser I am getting the same error. I must be missing something that I don't know
仅当我选择一个选项时,如何才能呈现我的视图?
How can render my view only when I select an option?
任何帮助将不胜感激.
推荐答案
在您的URL中
$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);
第一个参数应该是正确的现有路由.但是您是以目录的形式编写的,即mdctafiff
文件夹,然后是_slabBased
文件.
The first argument should be the proper existing route. But you have written it in the form of a directory, i.e. mdctafiff
folder then _slabBased
file.
这里您需要做的是,您需要在控制器中创建一个动作方法,以便您可以通过路由访问它.类似于MdctariffController
和partialAction
,然后在partialAction
方法的主体中,您需要调用_slabBased
视图文件.此外,您也可以在此处以获得Url :: toRoute的引用().
What you need to do here is, you need to create an action method in the controller so that you can access it through route. Like MdctariffController
and partialAction
and then in the body of partialAction
method you need to call the _slabBased
view file. Futher you can also take reference here for Url::toRoute().
这篇关于如何仅在yii2中选择后呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!