CGridView & Shopping Cart

Hello,

I’m faced to a problem. I want to build a page which will display my shopping cart items and then delete or update quantity of the items.

I thought the best way is to use the CGridView. So far no problem I can display all my items. My problem is the quantity. I want an input box and when it changed the CGridView and the total cart price will be updated.

Can anyone give me some tips how to realise that?

Many thanks,

Luc

You should be able to do this by simply defining a column that spits out a chtml text box withe the current quantity as well as an ‘update’ button to update any changes. You could use javascript to enable a CHtml::ajaxButton ones the user has changed the quantity which will use ajax to update the database / session.

Hello,

I have already a problem for deleting an item in my gridview.

I have the following function in my controller which will delete the item in the database. I want to call this function per ajax.


public function actionCartdeleteitem()

        {

                echo "success";

                if (isset($_POST['Item']))

                {

                        $sessionid = app()->session->sessionID;

                        $id = $_POST['id'];

                        $sql = "DELETE FROM tbcart WHERE sessionid = :sessionid AND id=:id;";

                        $cmd = app()->getDb()->createCommand($sql);

                        $cmd->bindParam(':sessionid', $sessionid);

                        $cmd->bindParam(':id', $id);                        

                        $cmd->execute();

                }

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


        }

Here the definition of the column in the gridview


array(

                        'class'         => 'CButtonColumn',

                        'template' => '{deleteitem}',

                        'buttons' => array(

                                'deleteitem' => array(

                                        'label' => 'Delete',

                                        'url'   => 'Yii::app()->controller->createUrl("checkout/cartdeleteitem",array("id"=>$data[\'id\']))',

                                        'click' => 'js:function() {deleteitem_onclick( $(this).attr("href"), 2 ); return false;}',

                                ),

                        ),

                ),

An here the javascript function which is called. But the function will redirect me do another page and I receive never a success from the ajax call. I want to refresh the entire grid on success.


function deleteitem_onclick(url, id) {

        

        var data2post = {

                'Item' : {

                        'id' : id

                }

        };

        

        $.fn.yiiGridView.update('cart-grid', {

                type :'POST',

                url  : url,

                data : data2post,

                success:function(data,status) {

                        $.fn.yiiGridView.update('cart-grid');

                },

                error: function(XMLHttpRequest, textStatus, errorThrown) {

                     alert(XMLHttpRequest.responseText + textStatus + errorThrown);

                 }

        });

}


/*]]>*/

What is wrong with my code?

Where does it redirect you? If your action is not called then the problem is in the url parameter…

I found the error. There was an issue in my javascript


function deleteitem_onclick(url, id) {


        var data2post = {

                'Item' : {

                        'id' : id

                }

        };


        $.ajax({

            url: url,

            cache: false,

            success: function(response, status){

                $.fn.yiiGridView.update('cart-grid');

            }

        });

        


}

So now you are not sending the data2post variable… so you can delete that part…

Yes that’s right