【问题标题】:Creating a PDF document from a filtered CGridView - Yii从过滤的 CGridView 创建 PDF 文档 - Yii
【发布时间】:2014-04-15 17:03:37
【问题描述】:

我正在尝试从过滤的 CGridView 创建 PDF。该值将通过高级搜索中的下拉菜单传递,但问题是我无法通过我的 pdf 函数过滤搜索。

控制器

public function actionPrint() {
    $mPDF1 = Yii::app()->ePdf->mpdf('ar','A4','14','dejavusanscondensed');
    $model=new Country('search');

    $model->center_id = 1;// This value will be passed from dropdown
                          //and i want the report to be made on this

    $model->unsetAttributes(); 
    if(isset($_GET['Country']))
        $model->attributes=$_GET['Country'];

    $html = '';
    $html .= $this->renderPartial('candidates', array('model'=>$model, 'enablePagination' => false),true);
    $mPDF1->WriteHTML($html, false);
    $mPDF1->Output('list.pdf','D');
}

查看

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'country-grid',
        'dataProvider'=>$model->search($enablePagination),
        'summaryText' => '',
       // 'enablePagination' => false,
        'filter'=>$model,
        'columns'=>array(
            'name',
            array(
                'header'=>' Total Registered Candidates',
                'value'=>'$data->itemsTotal',
            ),
        ),
    ));
    echo CHtml::link(
        'Save as PDF',
        Yii::app()->createUrl('country/print'),
        array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));

型号

public function search($enablePagination = true)
{
         $criteria->together= true;
        $criteria->with=array('center');
 $criteria->compare('center.name', $this->center_id, true);
..........
        if ($enablePagination)
        {
                $pagination = array(
                        'pageSize' => 30,
                );
        }
        else
        {
                $pagination = false;
        }

        return new CActiveDataProvider($model, array(
                'criteria' => $criteria,
                'pagination' => $pagination,
        ));
}

【问题讨论】:

  • 打印按钮/链接吗?如果是这样,传递的确切链接参数是什么?可能是您没有将过滤器值作为参数传递。
  • @topher 问题不在于传递值,正如您所见,我已经对值进行了硬编码,尽管如此我无法获得过滤后的 PDF 报告。
  • 您正在使用$model->unsetAttributes() 取消设置center_id。将$model->center_id = 1;移动到$model->attributes = $_GET['Country'];下一行
  • @topher 如果我不取消设置属性,则不会在 PDF 中打印任何值。如果我把它放在下面 $model->attributes = $_GET['Country'];不打印任何值。
  • 你搬了吗?如果是这样,那么问题可能出在您的查询上。您能否包括 candidates 视图和您的模型的 search 函数(或您用来生成数据/数据提供者的任何内容)

标签: yii pdf-generation


【解决方案1】:

由于center_id 是外键

$criteria->compare('center.name', $this->center_id, true);

应该阅读

$criteria->compare('center_id', $this->center_id);

您也可以执行以下操作,但这会在连接表上添加一个条件,并可能导致查询速度变慢。

$criteria->compare('center.id', $this->center_id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多