2012-03-08 36 views
5

我有關於分頁和Ajax形式的問題。Yii CListview - >分頁和AjaxLink/ajaxButton

這裏是我的Controller代碼:

$dataProvider = new CActiveDataProvider('User', array(
     'pagination'=>array(
       'pageSize'=>10, 
     ), 
)); 

$this->render('users',array(
    'dataProvider'=>$dataProvider, 
)); 

對於視圖 - >用戶:

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider, 
    'itemView'=>'_user', 

);

對於渲染_users:

echo CHtml::ajaxLink($text, $this->createUrl('admin/deleteuser',array('id'=>$data->iduser)), array('success'=>"js:function(html){ alert('remove') }"), array('confirm'=>_t('Are you sure you want to delete this user?'), 'class'=>'delete-icon','id'=>'x'.$viewid)); 

如果我有在數據庫中,將僅示出10 15行和將生成用於下一個5.前10行分頁(ajaxUpdate =真)具有沒有問題ajaxLink是因爲生成了clientscript,但問題在於當我轉到下一頁時,ajaxLink無法正常工作,因爲它不是由分頁生成的。

有什麼想法嗎?謝謝

+2

你必須**解釋**什麼是錯誤的,而不是隻是把代碼和期望的答案 – 2012-03-08 11:29:17

+0

哪裏是你正在面臨的描述? – 2012-03-08 20:27:41

+1

對不起,我以爲我已經把問題..再次對不起 – butching 2012-03-09 07:25:49

回答

0

不完全確定,但考慮到過去的經驗,我認爲問題出在listview小部件本身。

如果小部件的所有者是控制器,則它使用renderPartial來渲染項目視圖。 Renderpartial擁有或者不知道「processOutput」參數,對於大多數AJAX魔術(默認爲FALSE),該參數需要設置爲TRUE。

因此,也許你可以嘗試只是派生一個listview的類,並在「renderItems()」的那裏添加一個副本。在那裏您必須更改它,以便使用正確的參數調用renderPartial。

+0

當我點擊下一頁,因爲它是ajax我看到在控制檯(螢火蟲)的結果,我看到ajaxLink jscode已生成,但它仍然不能在母文件。 – butching 2012-03-12 05:15:25

+0

大多數Ajax鏈接的東西需要一些「在文檔就緒」的代碼來綁定到事件等,這是不會被添加到processOutput == FALSE返回的結果。 – Blizz 2012-03-12 07:04:00

1

另一種方法,請檢查post in the yii forum。所以,你的代碼會變成這樣:

echo CHtml::link($text, 
    $this->createUrl('admin/deleteuser',array('id'=>$data->iduser)), 
    array(// for htmlOptions 
     'onclick'=>' {'.CHtml::ajax(array(
      'beforeSend'=>'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}', 
      'success'=>"js:function(html){ alert('removed'); }")). 
     'return false;}',// returning false prevents the default navigation to another url on a new page 
    'class'=>'delete-icon', 
    'id'=>'x'.$viewid) 
); 

你確認被移動到jQuery的ajax功能的beforeSend回調。如果我們從beforeSend返回false,則不會發生ajax調用。

另一項建議,你應該使用post變量代替get,而且如果可以的話,移動到索引視圖function Ajax調用,並且只包括從所有鏈接onclick事件的函數調用。

希望這會有所幫助。

+0

是的,我只是讀了,我想我沒有其他的選擇,所以我創造了一些想法..只是把代碼afterAjaxUpdate – butching 2012-03-12 09:27:58

+0

行..那是另一種方式來做到這一點 – 2012-03-12 09:41:39

0

在控件CListView中,我加入afterAjaxUpdate

$jsfunction = <<< EOS 
js:function(){ 
    $('.delete-icon').live('click',function(){ 
     if(confirm('Are you sure you want to delete?')) 
     { 
     deleteUrl = $(this).attr('data-href'); 
     jQuery.ajax({'success':function(html){ },'url':deleteUrl,'cache':false}); 
     return false;  
     } 
     else 
     return false; 
    }); 
    } 
EOS; 

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider, 
    'id'=>'subscriptionDiv', 
    'itemView'=>'_subscription', 
    'afterAjaxUpdate'=>$jsfunction, 
    'viewData'=>array('is_user'=>$is_user,'all'=>$all), 
    'htmlOptions'=>($dataProvider->getData()) ? array('class'=>'table') : array('class'=>'table center'), 
) 
); 

,並在_user剛剛添加的屬性數據的href

<?php echo CHtml::ajaxLink($text, $this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription)), 
      array('success'=>"js:function(html){ $('#tr{$viewid}').remove(); }"), 
      array('confirm'=>_t('Are you sure you want to unsubscribe?'), 
       'class'=>'delete-icon', 
       'id'=>'x'.$viewid, 
       'data-href'=>$this->createUrl('admin/deletesubscription',array('id'=>$data->idsubscription))     
       )); ?> 
0

嘗試

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider, 
    'itemView'=>'_user', 
    'enablePagination' => true, 
); 

如果不解決,請嘗試在數據提供者選項中包含記錄的總數 -

'itemCount' => .....,