2014-02-12 146 views
0

我想让我的CGridView按钮上的打印机打印。
我的视图打印CGridView - Yii

<p align="right"> 
     <?php 

      echo CHtml::link('New Job',array('create'),array('class'=>'btn btn-info')); 
      echo CHtml::link('Print',array('print'),array('class'=>'btnPrint btn btn-info', 'style'=>'margin-left: 10px;')); 
     ?> 
    </p> 
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'job-grid', 
    'dataProvider'=>$jobs->search(), 
    'filter'=>$jobs, 
...)); ?> 

我的控制器

public function actionPrint() { 
     $d = $_SESSION['all']; 
     $this->renderPartial('print',array('d'=>$d),false,true); 
} 

我的模型

public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id,true); 
     $criteria->compare('name',$this->name,true); 
     $criteria->compare('date',$this->date,true); 
     $criteria->compare('department_id',$this->department_id); 
     $criteria->compare('section_id',$this->section_id); 
     $criteria->compare('team_id',$this->team_id); 
     $criteria->compare('created_date',$this->created_date,true); 
     $criteria->compare('updated_date',$this->updated_date,true); 

     $data = new CActiveDataProvider(get_class($this), array(
       'criteria'=>$criteria, 
       'pagination'=>false, 
     )); 
     $_SESSION['all'] = $data; 

     $data = new CActiveDataProvider(get_class($this), array(
       'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize', 
         Yii::app()->params['defaultPageSize']),), 
       'criteria'=>$criteria, 
     )); 
     $_SESSION['limited'] = $data; 

     return $data; 
    } 

我的视图(print.php)

<div id="summary"> 
<table width="100%" border="1"> 
    <tr> 
     <th>No.</th> 
     <th>Job No.</th> 
     <th>Date</th> 
     <th>Department</th> 
     <th>Section</th> 
     <th>Team</th> 
    </tr> 
    <?php 
    $i=1; 
    foreach($d->data as $item) 
    { 
    ?> 
    <tr> 
     <td><?php echo $i;?></td> 
     <td><?php echo $item->name; ?></td> 
     <td><?php echo $item->date; ?></td> 
     <td><?php echo $item->departmentsdep->name; ?></td> 
     <td><?php echo $item->departmentssec->name; ?></td> 
     <td><?php echo $item->departmentstea->name; ?></td> 
    </tr> 
    <?php 
     $i++; 
    } 
    ?> 
</table> 
</div> 

但如果使用上面的代码我收到未定义指数的错误:所有
如何将这项工作。

+0

你宣布$ _SESSION [ '所有']在哪里?您在模型中使用它,但尚未定义它。 –

+1

Yii :: app() - > session ['all']。那么添加这条线呢?你试过这个吗? –

+1

同样,你可以像使用它$ d = Yii :: app() - > session ['var'] –

回答

1

试试这个

Yii::app()->session['all'] 

不是访问$ _SESSION直接

+0

它的工作,但现在我正在尝试获得非对象的属性foreach。 –

+0

这是因为在调用搜索方法之前正在设置控制器中的分配,请尝试在控制器中分配之前调用搜索并查看会发生什么。 –