Question

I have view with 3 widgets. Two TbButton widgets and one TbExtendedGridView widget. Buttons are used for "insert new user" and "create pdf". TbExtendedGridView is used to list current users and their attributes. TbExtendedGridView has filtering turned on and is working fine.

Controller is simple. It detects if there is an AjaxRequest. If there is, it filters data according to GET variables. If not, it renders a default view that shows all users.

What i need is the ability to create a PDF of that table.

I installed PDF extensions and created PDFReport controller. When user clicks on "create pdf" button, he goes to that controller which than creates PDF. All is fine until I want to create PDF out of filtered data.

First thing that came across my mind was to pass variables from filters to "Create pdf" button's link so that once it's clicked i will send relevant data to PDFReport controller which would than be able to filter data and create filtered PDF.

Problem is that filtering is done via AJAX request and it refreshes only table. Button widgets are not refreshed and i don't know how to send new link to "create PDF" button.

I have no idea how to make this work.

Controller action:

public function actionIndex()
{
    // if submited by filters, get only filtered data
    if( Yii::app()->request->isAjaxRequest && isset($_GET['users']))
    {
        $criteria=new CDbCriteria;
        foreach ($_GET['users'] as $key => $value) {
            if ($value != "") {
                if (preg_match('/^(["\']).*\1$/m', $value)) {
                    $criteria->addInCondition($key,array($value),$operator='AND');
                }
                else
                {
                    $criteria->addsearchCondition($key,$value,$like='LIKE');
                }
            }
        }
        $data=new CActiveDataProvider(
            'users', 
            array(
                'criteria'=>$criteria,
                'pagination' => array('pageSize' => 30)
            )
        );
        $createPDFurl = "test2";
        $this->render('index',array('model'=>$data, 'createPDFurl' => $createPDFurl));
    }
    //else full table
    else
    {
        $data=new CActiveDataProvider(
            'users', 
            array('pagination' => array('pageSize' => 30))
        );
        $createPDFurl = "test1";
        $this->render('index',array('model'=>$data, 'createPDFurl' => $createPDFurl));
    }
}

and view:

<?php

    $this->widget(
        'bootstrap.widgets.TbButton',
        array(
            'label' => 'Unos novog zaposlenika',
            'type' => 'primary',
            'url' => $createPDFurl,
        )
    );

    $this->widget(
        'bootstrap.widgets.TbButton',
        array(
            'label' => 'Kreiraj PDF',
            'type' => 'primary',
            'url' => $createPDFurl,
        )
    );

    $godisnji_width = "100px";
    $this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'fixedHeader' => true,
    'headerOffset' => 40, // 40px is the height of the main navigation at bootstrap
    'type' => 'striped bordered condensed',
    'dataProvider' => $model,
    'filter' => $model->model,
    'responsiveTable' => true,
    'template' => "{pager}{items}{summary}{pager}\n{extendedSummary}",
    'ajaxUrl'=> $this->createUrl('/site/index'),
    'pager' => array(
        'nextPageLabel' => 'Sljedeća',
        'prevPageLabel' => 'Prijašnja',
        'firstPageLabel' => 'Prva',
        'lastPageLabel' => 'Posljednja'
    ),
    'summaryText' => 'Prikazano {start}-{end} od {count} unosa.',
    'columns' => array(
        'prezime',
        'ime',
        'radno_mjesto',
        'odjel',
        'broj_dana',
        array('name'=>'godisnji', 'htmlOptions'=>array('width' => $godisnji_width), 'headerHtmlOptions'=>array('width' => $godisnji_width), 'filterHtmlOptions'=>array('width' => $godisnji_width)),
        'stari_godisnji',
        'bolovanje',
        array(
            'htmlOptions' => array('nowrap'=>'nowrap'),
            'class'=>'bootstrap.widgets.TbButtonColumn',
        ),
    ),

    ));

echo $createPDFurl;

When you load index page 1st, you get 2 nice buttons and 1 nice table full of data and echo of that test variable: "test1". When i enter something into filter, my table changes but test variable stays "test1". I'm pretty much sure i know why, because Ajax changes only table, which is good thing as that's reason Ajax is there, but how to force it refresh other widgets or how to push that new data to rest of the page i have no idea.

Was it helpful?

Solution

As my answer rather suggestion is long thats y i am adding answer instead of adding comment below the question.
First thing for your code, You are using a property responsiveTable=true. this property will not let the gridView to be responsive rather it will reverse the effect as you can see when you reduce the width of your browser manually it will show TbGridView but with reverse effects.It will distort the orientation of the columns and rows.try it yourself.
For your question As TbExtendedGridView is extended from TbGridView. And TbGridView is extended from CgridView and TbDataColumn. Now CgridView has some public properties regarding ajax. listed as

  • public $ajaxUpdate;
  • public $ajaxUpdateError;
  • public $ajaxVar='ajax';
  • public $ajaxUrl;
  • public $beforeAjaxUpdate;
  • public $afterAjaxUpdate;

As you have used $ajaxUrl in your code. Now I think $ajaxUpdate is the property your are looking for.

@var mixed the ID of the container whose content may be updated with an AJAX response.
     * Defaults to null, meaning the container for this grid view instance.
     * If it is set false, it means sorting and pagination will be performed in normal page requests
     * instead of AJAX requests. If the sorting and pagination should trigger the update of multiple
     * containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).

Above lines are about $ajaxUpdate .I have taken above lines from the official documentation of yii. I hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top