从 onChange 事件调用控制器操作

发布于 2024-12-02 21:22:57 字数 682 浏览 1 评论 0原文

我需要一些有关 Yii 的帮助。

如何从文本框 onChange 事件调用控制器操作?

更新: 我有一个用 CActiveForm 构建的表单。该表单有许多文本框。当我更改任何值时,必须更新其他值,使用新输入的值计算公式。所以我想做的是:

    <tr>
        <td>Average KW Demand:</td>
        <td><?php echo $form->textField($lfac_model, 'kw_demand', array('size'=>5));?></td>
    </tr>
    <tr>
        <td>Estimated Load Factor:</td>
        <td><?php echo $form->textField($lfac_model, 'loadf', array('size'=> 5));?>/td>
    </tr>

当用户更改“估计负载系数”时,“平均千瓦需求”值应使用公式的结果进行更新。我想到的是将公式存储在表中,然后转到控制器操作,检索特定公式,计算新值并将其发送回视图。我不确定这是否是实现这一点的最佳想法。

I need some help with Yii.

How can I call a Controller action from a textbox onChange event?

Update:
I have a form built with CActiveForm. This form has many text-boxes. When I change any value other must be updated computing a formula using the new entered value. So what I am trying to do is the following:

    <tr>
        <td>Average KW Demand:</td>
        <td><?php echo $form->textField($lfac_model, 'kw_demand', array('size'=>5));?></td>
    </tr>
    <tr>
        <td>Estimated Load Factor:</td>
        <td><?php echo $form->textField($lfac_model, 'loadf', array('size'=> 5));?>/td>
    </tr>

When the user changes "Estimated Load Factor", "Average KW Demand" value should be updated with the result of a formula. What I've figured out is to store the formulas in a table, then go to a Controller action, retrieve the specific formula, calculate the new value and send it back to the view. I'm not sure if this is the best idea to implement this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2024-12-09 21:22:57

您应该能够使用 CJuiAutoComplete Yii 小部件来执行您想要的操作。 此处有一个基本演示,并且还有其他示例Yii 文档。在您的情况下,您需要确保的主要事情是您还指定了“appendTo”选项,请参阅 jQuery 自动完成文档,该小部件使用的。

因此基本上,您将为“估计负载系数”字段设置小部件,并通过appendTo 属性指向“平均功率需求”字段。

如果您不想要自动完成功能而只想替换该值,您可以在“loadf”值上使用 htmlOptions 属性“ajax”,如下所示:

'ajax' => array(
    'type'=>'POST', //request type
    'url'=>$this->createUrl('site/ajaxAction'), //url to call
    'success'=>'function(data) { $(\'#kw_demand\').val(data) }',// function to call onsuccess 
             // "data" is returned data and function can be regular js or jQuery
)

请参阅: CHtml#ajax-detailjQuery.ajax。在这种情况下,您很可能会返回纯文本,因此您的控制器函数将如下所示:

public function actionAjaxAction() {
  [your data functions...]
  echo $kw_demand_string;
  Yii::app()->end()
}

You should be able to use the CJuiAutoComplete Yii widget to do what you want. There is a basic demo here and there are other examples in the Yii docs. The main thing you would need to make sure in your case is that you also specify the "appendTo" option, see the jQuery autocomplete docs, which that widget uses.

So basically, you would set up the widget for your "Estimated Load Factor" field, and point to the "Average KW Demand" field via the appendTo attribute.

And if you don't want the autocomplete stuff and simply want to replace the value, you can use the htmlOptions attribute "ajax" on your "loadf" value like so:

'ajax' => array(
    'type'=>'POST', //request type
    'url'=>$this->createUrl('site/ajaxAction'), //url to call
    'success'=>'function(data) { $(\'#kw_demand\').val(data) }',// function to call onsuccess 
             // "data" is returned data and function can be regular js or jQuery
)

see: CHtml#ajax-detail and jQuery.ajax. In this case you would most likely return plain text so your controller function would look like:

public function actionAjaxAction() {
  [your data functions...]
  echo $kw_demand_string;
  Yii::app()->end()
}
紫竹語嫣☆ 2024-12-09 21:22:57

文本框的 onChange 事件是一个 JavaScript 事件。它存在于应用程序的不同方面。

要在控制器上调用操作,您必须使用一些AJAX(或异步调用)。这里的想法本质上是将一些数据发布到处理该操作的路由。

例如:

  • 假设您有以下路线/页面: /product/details => ProductController::actionDetails()
  • 您希望使用自动完成选项更新搜索字段。
  • 您在搜索字段的 onChange 事件上设置 javascript,以调用将字段值(当前输入的文本)发布到控制器上的操作的方法。
  • 您设置此路线/页面 /product/ajaxautocomplete => ProductController:actionAjaxAutocomplete()
  • 第二个操作从 javascript 异步 HTTP 请求(AJAX 调用)获取发布的值,并根据数据库返回可能结果的列表。
  • 然后,您的 javascript 接受返回值(通常为 XML/JSON,有时为 HTML),并将这些值应用于客户端的 dom。

您可以使用 jQuery 轻松完成此任务:

$.ajax({
  type: 'POST',
  url: '/product/ajaxautocomplete',
  data: { 'searchPhrase' => searchBox.value },
  success: function(data, textStatus, jqXHR) { applyValues(data); },
  dataType: 'json'
});

A Textbox's onChange event is a javascript event. It lives in a different aspect of your application.

To call an action on your controller you're going to have to use some AJAX (Or an asynchronous call). The idea here is to essentially post some data to the route that handles that action.

For instance:

  • Say you have this route/page: /product/details => ProductController::actionDetails()
  • You want to have a search field update with auto-complete options.
  • You set up javascript on the onChange event of your search field to call a method that posts the field value (currently entered text) to an action on your controller.
  • You set up this route/page /product/ajaxautocomplete => ProductController:actionAjaxAutocomplete()
  • This second action takes in the posted value from your javascript Async HTTP Request (AJAX Call) and returns a list of possible results based on the database.
  • Your javascript then accepts the return (usually in XML/JSON, sometimes HTML) and applys the values against the dom on the client side.

You can use jQuery to accomplish this easily:

$.ajax({
  type: 'POST',
  url: '/product/ajaxautocomplete',
  data: { 'searchPhrase' => searchBox.value },
  success: function(data, textStatus, jqXHR) { applyValues(data); },
  dataType: 'json'
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文