我在单个视图中渲染两个视图.
= $form->field($model, 't_type')->dropDownList(['' =>'请选择','基于平板' =>'基于板','基于 TOU' =>'基于 TOU']) ?><div class="showSlab";id="slab"样式=显示:无"><?php echo $this->render('_slabBased', ['modelsTariffSlabs' =>$modelsTariffSlabs,]);?>
<div class="showTou";id=头"样式=显示:无"><?php echo $this->render('_touBased', ['modelsTouSlabs' =>$modelsTouSlabs,]);?>
默认情况下,两个 div 都是隐藏的,但它们都在渲染.但我只想在选择选项基于板"或 TOU Based
JS
$('#mdctariff-t_type').on('change', function () {if (this.value === '基于平板') {$(#slab").show();$(#tou").hide();} else if (this.value === 'TOU based') {$(#tou").show();$(#slab").hide();} 别的 {$(#slab").hide();$(#tou").hide();}});
注意:渲染表单后,我也在保存它
更新 1
我试图通过 ajax
$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);doGet('$url')函数 doGet(url, params) {参数 = 参数 ||{};$.get(url, params, function(response) {//请求表单中的 url$('#slab').html(响应);//获取响应并推送到具有 id #response 的元素});}
参考: 如何使用 AJAX 渲染局部?Laravel 5.2
当我选择一个选项时,我无法查看表单.在我的 Network
选项卡中,我收到错误 Not Found (#404): Page not found.
.生成的 URL
是 http://localhost/mdc/backend/web/mdctariff/_slabBased
在我的浏览器中粘贴此 URL 时,我遇到了同样的错误.我一定遗漏了一些我不知道的东西
如何仅在我选择一个选项时呈现我的视图?
任何帮助将不胜感激.
在你的 URL 中
$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);
第一个参数应该是正确的现有路由.但是你已经以目录的形式编写了它,即 mdctafiff
文件夹,然后是 _slabBased
文件.
这里你需要做的是,你需要在控制器中创建一个action方法,以便你可以通过路由访问它.像MdctariffController
和partialAction
,然后在partialAction
方法的主体中,您需要调用_slabBased
视图文件.此外,您还可以参考 此处 了解 Url::toRoute().
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>
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
Update 1
I have tried to render it via 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
});
}
Reference: How to render partial using AJAX? Laravel 5.2
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
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?
Any help would be highly appreciated.
In your URL
$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);
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.
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 中选择后才呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!