2014-01-14 39 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

虽然我解决了这个问题昨天才用同样的解决方案你给了。反正谢谢你的回答和帮助我......干杯:) –

相关问题