如何仅在 yii2 中选择后才呈现视图

2023-10-16php开发问题
0

本文介绍了如何仅在 yii2 中选择后才呈现视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在单个视图中渲染两个视图.

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..生成的 URLhttp://localhost/mdc/backend/web/mdctariff/_slabBased

在我的浏览器中粘贴此 URL 时,我遇到了同样的错误.我一定遗漏了一些我不知道的东西

如何仅在我选择一个选项时呈现我的视图?

任何帮助将不胜感激.

解决方案

在你的 URL 中

$url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);

第一个参数应该是正确的现有路由.但是你已经以目录的形式编写了它,即 mdctafiff 文件夹,然后是 _slabBased 文件.

这里你需要做的是,你需要在控制器中创建一个action方法,以便你可以通过路由访问它.像MdctariffControllerpartialAction,然后在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 中选择后才呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用

PHP实现DeepL翻译API调用

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误

Laravel Gmail 配置错误

Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题

将 PHPMailer 用于 SMTP 的问题

Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17

热门文章

1nohup:忽略输入并将输出附加到“nohup.out" 2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET 3如何将 LDAP 时间戳转换为 Unix 时间戳 4不推荐使用常量 FILTER_SANITIZE_STRING 5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动 6PHP通过phpspreadsheet导入Excel日期数据处理方法 7Analytics API 返回:错误请求 - invalid_grant 8“tlsv1 警报内部错误"握手时

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序