2015-12-08 68 views
1

我只是試圖圍繞yii框架我的頭,我創建了一次刪除多個數據庫記錄的函數。在視圖中我有一個網格的引用,每一個項目和旁邊的複選框Yii框架 - 使用CCheckBoxColumn刪除多個

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'htmlOptions'=>array(
     'class'=>'' 
    ), 
    'columns'=>array(
... 
array(
    'class'=>'CLinkColumn', 
    'header'=>'Handle', 
    'labelExpression'=>'$data->handle', 
    'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))', 
     ), 
array(
    'class'=>'CCheckBoxColumn', 
    'header'=>'Select', 
    'selectableRows'=>'2', 
     ), 
    ), 

再往頁面我想要一個按鈕(刪除選擇),它發送的所有項目的數組刪除功能。我的想法是這將是這一個類似於:

<a href=" 
<?php echo Yii::app()->createUrl('.../item/bulkDelete','array("id" => $data->id)')?>" 
class="btn">Delete Selected</a> 

但我不知道如何獲得每個項目檢查的參考,而不是:

"id" => $data->id 

爲我所調用的傳遞項目提前查看功能。如果任何人都可以幫助它將不勝感激。

編輯:

查看:

$form = $this->beginWidget('CActiveForm', array(
     'id' => 'itemForm', 
     'action' => array('.../item/bulkDelete'), 
     )); 

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'htmlOptions'=>array(
     'class'=>'' 
    ), 
array(
    'class'=>'CLinkColumn', 
    'header'=>'Handle', 
    'labelExpression'=>'$data->handle', 
    'urlExpression'=>'Yii::app()->createUrl(".../item/view", array("id"=>$data->id))', 
), 
array(
    'class'=>'CCheckBoxColumn', 
    'header'=>'Select', 
    'selectableRows'=>'2', 
), 
... //More Columns 
), //End of Grid 
... 
echo CHtml::SubmitButton('Delete Multiple'); 
$this->endWidget(); 

控制器:

public function actionBulkDelete() 
{ 

    if(isset($_POST['id'])&& !empty($_POST['id'])) 
    { 
     Yii::app()->user->setFlash('success', 'Delete Items'); 
      $this->redirect(array('.../item/index')); 
    } 
    else 
    { 
     Yii::app()->user->setFlash('success', 'No Items Selected'); 
      $this->redirect(array('.../item/index')); 
    } 


} 

回答

1

包裹形式的內部網格視圖,並做你的columns陣列以下變化:

$form = $this->beginWidget('CActiveForm', array(
     'id' => 'some-grid-form', 
     'action' => array('myController/myAction'), 
)); 

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'selectableRows' => 2, 
    'columns'=>array(
    array(
      'id' => 'id', 
      'class' => 'CCheckBoxColumn', 
     ), 
    //... rest of your columns 
    ), 
    array(
     'class'=>'CButtonColumn', 
    ), 
), 
)); 

echo CHtml::SubmitButton('Multiple Delete'); 
$this->endWidget(); // end form 

而在你的控制器的myAction

public function actionmyAction() 
    { 
     if(isset($_POST['id']) && !empty($_POST['id'])) { //you'll get all the ids in an array 
      //print_r($_POST['id']); 
      //your delete function here, also add a few validation here to authenticate deletion 
      $ids = $_POST['id']; 
      $criteria = new CDbCriteria; 
      $criteria->addInCondition('id',$ids); 
      MyModel::model()->deleteAll($criteria); 
      //.... render with success flash message etc. 
     } 
    } 
+0

非常感謝你的幫助!這對我來說真的很有用! –

+0

對不起,由於某種原因'isset($ _ POST ['id])'總是返回false –

+0

@AlecGamble您可以發佈您的代碼,只需將其附加到您現有的問題? – Criesto