在Yii中使用Ajax更新值

时间:2022-09-15 09:26:53

This is my first ever post....

这是我的第一篇文章....

I'm working on a shopping website using Yii-Booster but I'm currently stumped! Basically what I'm trying to achieve is that when a user adds a product to the cart, a modal displays showing an acknowledgement message and then the div with the shopping cart is updated to reflect the new total.

我正在使用Yii-Booster在购物网站上工作,但我现在很难过!基本上我想要实现的是,当用户将产品添加到购物车时,显示确认消息的模式显示然后更新购物车的div以反映新的总数。

Really simple, innit?? I've spent the entire day trying to get it to work. Any help you can offer would be greatly appreciated.

真的很简单,没有?我花了一整天的时间试图让它发挥作用。我们非常感谢您提供的任何帮助。

Oh yeah... one more thing, the dropdown menu in the navbar stops working after almost any sort of AJAX action(loading modals, etc). I'm guessing its caused by a conflict somewhere but I'm completely lost in Jquery/Ajax stuff.

哦,是的...还有一件事,导航栏中的下拉菜单在几乎任何类型的AJAX动作(加载模态等)后停止工作。我猜它是由某个地方的冲突引起的,但我完全迷失在Jquery / Ajax的东西中。

Thanks for the replies. Here's my code:

谢谢你的回复。这是我的代码:

StoreController:

// action allowing a product to be added to the shopping cart
public function actionAddToCart($id)
{
    //fetch item
    $item = Item::model()->findByPk($id);

    //if item is in cart
    if(Yii::app()->shoppingCart->contains($id))
    {
        //Yii::app()->clientScript->scriptMap['jquery.js'] = true;
        //$this->renderPartial('_addToCart',array('data'=>$data),false,true);           
        echo CJSON::encode(
            array(
                //'total'=>'Your Cart (N'.number_format(Yii::app()->shoppingCart->getCost()).')',
                'addCart'=>$this->renderPartial('_inCart',array('item'=>$item),true),
            )
        );
        //CHtml::ajax();
    }
    else
    {
        Yii::app()->shoppingCart->put($item);
        //Yii::app()->clientScript->scriptMap['jquery.js'] = true;
        //$this->renderPartial('_addToCart',array('data'=>$data),false,true);
        //$this->renderPartial('_addToCart',array('item'=>$item),false,true);
        echo CJSON::encode(
            array(
                'total'=>'Your Cart (N'.number_format(Yii::app()->shoppingCart->getCost()).')',
                'addCart'=>$this->renderPartial('_addToCart',array('item'=>$item),true),
            )
        );
    }
}

_display (partialview showing the product)

_display(显示产品的partialview)

<div style="float: left; display: inline; margin-right: 30px; margin-top: 20px;">
<img style="border: 1px solid black;" src="<?php echo Yii::app()->baseUrl; ?>/images/products/thumbs/<?php echo $data->image; ?>" />
<div style="font-size: 14px; margin-top: 3px;">
Name: <?php echo CHtml::ajaxLink($data->name, array("view", "id"=>$data->id), array('update'=>'#viewdetailsDiv',)); //array(), array('data-toggle'=>'modal','data-target'=>"#".$data->id)); ?><br/>
Category: <?=$data->category->category?><br/>
Gender: <?=$data->gender?><br/>
Price: <?php echo "N".number_format($data->price);?><br/>
<?php //echo CHtml::ajaxLink("Add to Cart", array("addtocart", "id"=>$data->id), array( 'update'=>'#addtocartDiv',)); ?>
<?php echo CHtml::ajaxLink("Add to Cart", array("addtocart", "id"=>$data->id), array( 'type'=>'POST', 'dataType'=>'json', 
    'success'=>'function(data){
        $("#Cart").html(data.total);            
        }'
    )
); ?>
</div>

Here's the code for the view containing the modal

这是包含模态的视图的代码

<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal', 'autoOpen'=>true, 'htmlOptions'=>array('style'=>'margin-top: 2px;'))); ?>

<div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h4>Your Cart has been updated</h4>
</div>

<div class="modal-body">
    <p>The item, <strong><?php echo $item->name; ?></strong> (<?php echo $item->category->category; ?>),  has been added to your shopping cart.</p>
</div>

    <div class="modal-footer">
    <?php $this->widget('bootstrap.widgets.TbButton', array(
        'type'=>'primary',
        'label'=>'Close',
        'url'=>'#',
        'htmlOptions'=>array('data-dismiss'=>'modal'),
    )); ?>
</div>

<?php $this->endWidget(); ?>

1 个解决方案

#1


0  

I can't tell you the perfect solution but can give you a suggestion regarding Yii that can solve your problem. Always load all the jquery or js files used in first request. Never load again any js file in any ajax request, It will a lot of problems.

我不能告诉你完美的解决方案,但可以给你一个关于Yii的建议,可以解决你的问题。始终加载第一个请求中使用的所有jquery或js文件。永远不要再加载任何ajax请求中的任何js文件,这会产生很多问题。

You can achieve it in simple way

您可以通过简单的方式实现它

if(Yii::app()->request->isAjaxRequest){
    Yii::app()->clientScript->scriptMap['jquery.js'] = true; 
}

#1


0  

I can't tell you the perfect solution but can give you a suggestion regarding Yii that can solve your problem. Always load all the jquery or js files used in first request. Never load again any js file in any ajax request, It will a lot of problems.

我不能告诉你完美的解决方案,但可以给你一个关于Yii的建议,可以解决你的问题。始终加载第一个请求中使用的所有jquery或js文件。永远不要再加载任何ajax请求中的任何js文件,这会产生很多问题。

You can achieve it in simple way

您可以通过简单的方式实现它

if(Yii::app()->request->isAjaxRequest){
    Yii::app()->clientScript->scriptMap['jquery.js'] = true; 
}