1

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.

Rafay Zia Mir
  • 2,116
  • 6
  • 23
  • 49
Ljudotina
  • 269
  • 1
  • 3
  • 14
  • Have you hard coded the url in button or getting through model attribute? – Rafay Zia Mir Feb 11 '14 at 16:06
  • Button url is passed from controler. When there is no filtering, url is passed and everything works fine. But once i filter, i can not change that url, as filtering only refreshes (via AJAX) TbExtendedGridView part of page and is not affecting TbButton part. – Ljudotina Feb 11 '14 at 16:12
  • If there is a way to force full page reload when filtering i would be happy too. – Ljudotina Feb 11 '14 at 16:17
  • ok, lease your action code in which you are filtering dataProvider, also post the code in view file where you are passing data to action for ajax call – Rafay Zia Mir Feb 11 '14 at 17:32
  • Here you go m8. Thanks for your time. – Ljudotina Feb 11 '14 at 23:07

1 Answers1

0

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.

Rafay Zia Mir
  • 2,116
  • 6
  • 23
  • 49
  • OMG dude! That's it! THAT'S FREAKING IT! AFTER 3 FREAKING DAYS OF DAMN TORTURE!!!! All i had to do is disable it and puf! IT WORKS! – Ljudotina Feb 12 '14 at 08:42
  • Now that i coold down...really thanks. You did not only solve my problem but from now on i know how to look deeper inside Yii and it's extensions / components. – Ljudotina Feb 12 '14 at 08:43
  • feeling glad if my answer helps. – Rafay Zia Mir Feb 12 '14 at 08:46
  • Is there PM system on stackoverflow? I need to ask you something private. – Ljudotina Feb 12 '14 at 08:46
  • no i think there is no PM system but you can email me. – Rafay Zia Mir Feb 12 '14 at 08:47
  • rafay_07@yahoo.com . The one on my profile is old, i dont use that – Rafay Zia Mir Feb 12 '14 at 08:49
  • Just adidional data for Yii noobs like myself. You dont even have to disable ajax, all i had to do is: 1) give every widget in this view it's unique id by like: 'id'=>'user-grid' and than in widget that does filtering you just tell ajax which widgets need to be refreshed by (in my case): 'ajaxUpdate' => 'user-grid, PDF-button', And there you go..you dont loose any ajax magic. Once again thanks to Rafay – Ljudotina Feb 12 '14 at 09:05