0

I am trying to display the price based on the selection of product from a drop down.

View contains:

<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
                    'id'=>'plan-form',
                    'enableAjaxValidation'=>true,
                )); ?>  

    <?php echo $form->dropDownListRow($model, 'plan_id',
            $planList, array('id'=>'planid', 'prompt'=>'Select Plan',
            'ajax' => array('type'=>'GET',
            'url'=> Yii::app()->createUrl('mbr/plan/ajaxGetPrice'),
            'update'=>'#price'))); ?>

    <div id="price">0.00</div>    
<?php $this->endWidget(); ?>

Action:

public function actionAjaxGetPrice()
{
    Yii::log("Within AjaxGetPrice");

    if (Yii::app()->request->isAjaxRequest)
        Yii::log("ajax request");
    else
        Yii::log("regular request");

    //$plan=Plan::model()->findByPk((int) $_GET['plan_id']);
    //Yii::log(serializ($plan));
    // echo $plan->price;

    echo "10";

    Yii::app()->end();      
}

It is not updating the price. Its not calling the action at all. I looked at the suggestions found in Yiiframework and here and tried those and still no luck.

when I add this to the view

    <?php
        echo CHtml::ajaxLink(
          "Get Price",
          Yii::app()->createUrl('mbr/plan/ajaxgetprice'),
          array( // ajaxOptions
            'type' => 'GET',
            'update' => '#price'),
          array( //htmlOptions
            'href' => Yii::app()->createUrl('mbr/plan/ajaxgetprice')
         )
        );
    ?>

I get the response when I click on the link, but it renders a separate page with value "10". I have URLFormat = Path.

What am I doing wrong? Any pointers?

4

3 回答 3

1

你也可以试试这个如图

<?php echo CHtml::dropDownList('categories','', 
              $category,
              array('ajax'=>array('type'=>'POST','url'=>CController::createUrl('yourController/GetId'),'update' =>'#data'))
            );?>

你的控制器动作

public function actionGetId(){
        echo 10;
    }

和 div

<div id="data">0.00</div>
于 2013-07-13T04:27:52.260 回答
0

我不太确定如何以 Yii 方式执行此操作,但以下 javascript 应该很有用。

$("#yourdropdownidhere").change(function(){
    $.ajax({
       url: 'your url here', //(dynamically generated by product item)
       type: "get",
       success: ajaxSuccessHandler
    });
});

function ajaxSuccessHandler(obj) {
    $('#price').html(obj);
}

我还建议发送一个 JSON 对象作为 AJAX 请求的响应,这样它就有一个实际的结构。一篇很棒的博客文章将是带有 Yii 的完全 ajax 网站

于 2013-07-12T21:44:51.020 回答
0

我遇到了同样的问题,并在呈现视图的控制器上解决了它。如果你使用 renderPartial 确保 processOutput() 选项设置为 TRUE,否则 Yii 不会附加任何 Javascript。

如果您使用 render 而不是 renderPartial 它也可以。

于 2015-07-27T16:16:12.803 回答