简体   繁体   English

Yii控制器从视图调用?

[英]Yii Controller call from a View?

I have a view that has a form. 我有一个形式的观点。 I have a button which calls a controller via Ajax. 我有一个按钮,可通过Ajax调用控制器。

    echo CHtml::submitButton('Generate', array(
        'ajax'        => array(
            'type'    => 'POST',
            'url'     => CController::createUrl('Calculator/generateRetailers'),
            'update'  => '#div_retailers'
        )
    ));

The action controller what it does is to gather some data from MySQL and then renderPartial HTML tables into my form. 动作控制器的作用是从MySQL收集一些数据,然后将renderPartial HTML表放入我的表单中。 Exactly in the div #div_retailers . 恰好在div #div_retailers This is the create option. 这是创建选项。 Now I am trying to implement the update action which should render the information provided in the create action and draw the tables. 现在,我正在尝试执行更新操作,该操作应呈现create操作中提供的信息并绘制表格。

I would like to be able to call generateRetailers action controller from my view. 我希望能够从我的视图中调用generateRetailers动作控制器。 Something like this: 像这样:

<div id="div_retailers">
</div>

<script type="text/javascript">
    // I would like to call a url using jQuery?
    $.ajax({
        url: "/Calculator/generateRetailers"
    });
</script>

How can I achieve this? 我该如何实现?

Thanks 谢谢

Do the exact same thing you already do in the widget: 做与小部件中已经做的完全相同的事情:

<script type="text/javascript">
  // I would like to call a url using jQuery?
  $.ajax({
    url: "<?php echo CController::createUrl('Calculator/generateRetailers');?>"
  });
</script>

Interesting, there is no built-in url generator for ajax. 有趣的是,没有用于Ajax的内置URL生成器。 So im calling ajax with helper variable, which is generated by my yii app. 所以我用我的yii应用程序生成的helper变量调用ajax。 I use Yii::app()->clientScript("config.url = ".Yii::app()->createUrl() . "); and then in pure javascript i can use a global variable config where i have generated urls. 我使用Yii::app()->clientScript("config.url = ".Yii::app()->createUrl() . ");然后在纯JavaScript中,我可以使用已生成的全局变量config网址。

$.ajax({
url: config.url
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM