2014-01-14 41 views
1

我正在使用yii,並且在產品從中刪除後我想更新我的clistview。 這裏是CListView中部件代碼當在yii中刪除記錄時更新CListView

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider, 
     'viewData'=>array('exhibitorId'=>$exhibitorId), 
     'id'=>'productView', 
    'itemView'=>'_productView', 
     'sortableAttributes'=>array(
     'productName', 
     'productType', 
     'productBrand', 
     'description' 
    ) 
)); ?> 

這裏是_productView

<b><?php echo CHtml::encode(Products::model()->getAttributeLabel('creationDate')); ?>:</b> 
    <?php echo CHtml::encode($data['creationDate']); ?> 
    <br /> 

    <b><?php echo CHtml::encode(Products::model()->getAttributeLabel('updatedBy')); ?>:</b> 
    <?php echo CHtml::encode($data['updatedBy']); ?> 
    <br /> 
<?php echo CHtml::ajaxLink('Delete', array('deleteMyThisProduct', 'product'=>$data['productId'],'exhibitor'=>$exhibitorId), 
       array(
        "beforeSend" => 'js:function(){if(confirm("Are you sure you want to delete?"))return true;}', 
        "success"=>'js:function(data){' 
        . '$.fn.yiiListView.update("productView");' 
        . '}', 
        "type"=>'post', 

        )); ?> 

這裏就是AJAX鏈接進入

public function actionDeleteMyThisProduct($product,$exhibitor) 
     { 
      if(Yii::app()->request->isAjaxRequest) 
      { 
      $productId=(int)$product; 
      $exhibitorId=(int)$exhibitor; 
      if($productId !== 0 && $exhibitorId !== 0) 
      { 
      $deleteProduct= Exhibitorproducts::model()->loadRecordsByexhibitorIdAndProductId($exhibitorId, $productId); 
      ProductPhotos::model()->deleteAllPhotosByExhibitorIdAndProductId($exhibitorId, $productId); 
      if(!empty($deleteProduct)) 
      { 
       $deleteProduct->delete(); 
      } 
      } 
      } 

     } 

問題的行動: - 現在的問題是,我第一次從clistview中刪除產品。它非常好地刪除產品並更新列表。但之後,當我點擊下一個產品的刪除鏈接時,它什麼都不做。那麼我該如何克服這個問題呢?

回答

3

問題是,當整個頁面加載並且ajax刷新後沒有附加任何事件時,您的事件將附加到<a>標記。

在你的產品視圖整個包住內容有<span>標籤:

<span class="product" data-product-id="<?php echo $data['productId']; ?>"> .... </span> 

更改你的腳本是這樣的:

$('#productView').on('click','.product', function(e){ 
    var $target = $(e.target); 
    e.preventDefault(); 
    $.post('<?php echo Yii::app()->createUrl('deleteMyThisProduct') ?>', {'product': $target.data('product-id')}). 
    done(function(){ 
     $.fn.yiiListView.update("productView"); 
    }) 

}) 
+0

雖然我解決了這個問題昨天才用同樣的解決方案你給了。反正謝謝你的回答和幫助我......乾杯:) –

相關問題