Cgridview Custom Data Provider Issue

Greetings,

Newbie here. I have a form with 2 textfields with datepicker to choose a begining and ending dates of events, a bootsrap tabular to show 2 CGridViews (there are 2 types of events both have different models, so each tab has one model) and a button to search.

(and yes, I must have those datepickers as the standard CGridView filtering is not to be used on this page)

The thing is in my action I get the begining and ending dates from the $_POST, perform Active Record query and then send the model back.

However, I apparently lost pagination in the process and am a bit lost on what to do.

My first CGridView (the other is the same but from a different model so I will ignore it):


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'event-grid',

    'dataProvider'=>$searching,

    'columns'=>array(

	array(

	  'name'=>'main_sponsor',

	  'htmlOptions'=>array('style' => 'text-align: center;')

	),

	array(

	  'name'=>'starting_date',

	  'htmlOptions'=>array('style' => 'text-align: center;')

	),

	array(

	  'name'=>'ending_date',

	  'htmlOptions'=>array('style' => 'text-align: center;')

	),

        .....

    ),

));

As you can see, im using this ‘dataProvider’=>$searching, which is probably the issue as it is a regular model. Anyway, I’m getting it by doing this on my controller:


           

$searching = new CActiveDataProvider('Event', array(

    'criteria'=>array(

    'condition'=>'starting_date >= :converted_start_date AND starting_date <= :converted_ending_date',

    'params'=>array(':converted_start_date'=>$converted_start_date , ':converted_ending_date'=>$converted_ending_date),

));

Then when I visit the page, I can see the total number of results, and the pagination, but when I click to the next page, the hole CGridView disappears. Any tips?

Thanks for you input,

Regards :)

Hi

Have u pass this on Serching array?


'pagination'=>array('pageSize'=>10),

Hi, thank you for your input.

And yes, I’m doing:


$searching = new CActiveDataProvider('Event', array(

    'criteria'=>array(

    'condition'=>'starting_date >= :converted_start_date AND starting_date <= :converted_ending_date',

    'params'=>array(':converted_start_date'=>$converted_start_date , ':converted_ending_date'=>$converted_ending_date),

    'pagination'=>array('pageSize'=>10),

));

and the outcome is the same :unsure:

Hi SilverPT,

CGridView (or CListView) and CActiveDataProvider works more easily with GET than with POST, because CGridView (or CListView) uses get method to implement its pagination, sorting and filtering features.

You could have made your search function using the "Advanced Search Form" in the gii-generated admin page as the skeleton, where the form is using get method.

I hope this wiki will help you understand how they are expeted to be configured. :)

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider

Hi softark and thank you for your reply.

Hum I see what you mean. However using the "Advanced Search Form" may not be what I am looking for because of how I am displaying the data.

I have a a form with those 2 datepickers and a Search button. Then, (also inside the form) I have got the Tabular with 2 tabs, each with a different model.

For what I know the "Advanced Search Form" performs the search on its own model, and what I am trying to do is search the 2 models and display both results already filtered in their respectave tabs by the dates inserted inside the datepickers.

Can this still be done with the "Advanced Search Form"?

Or perhaps I should go another path?

I would appreciate just this last question to have feedback so I could stop banging my head agains the wall ;D

Regards,

SilverPT

So, there are 2 CActiveRecord models involved …

Event and Event2 (or something I don’t know), and both of them can be filtered using the starting date and the ending date … right?

So my proposal will be to create a CFormModel that has 2 attributes of starting date and ending date. Let us call it EventSearch model. We can use it to construct the data providers for Event and Event2, and also we can use it as the model for the search form.

controller:




$model = new EventSearch('search');

$model->unsetAttributes();

if (isset($_GET['EventSearch'])

    $model->attributes = $_GET['EventSearch'];

$this->render('view', array('model' => $model));



view:




...

$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'event-grid',

    'dataProvider'=>$model->searchEvent(),

    ...


...

$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'event-2-grid',

    'dataProvider'=>$model->searchEvent2(),

    ...

...



EventSearch model:




public function searchEvent()

{

    ...

    return new CActiveDataProvider('Event', $criteria);

}

public function searchEvent2()

{

    ...

    return new CActiveDataProvider('Event2', $criteria);

}



Thank you very much softark, I will definatly try your suggestion ^_^

Best regards,

SilverPT