开发者

Calling a Controller action from an onChange event

开发者 https://www.devze.com 2023-04-01 22:24 出处:网络
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 othe

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.


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()
}


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'
});
0

精彩评论

暂无评论...
验证码 换一张
取 消