Help With Dataprovider And Renderpartial

Hi all,

I am trying to show a renderPartial view that contains data from another model and controller but am hitting a few problems that I hope someone can point me in the right direction with?

I have a view that when navigated to shows a CGridView of data.

Firstly this is the view -


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

    'id' => 'properties-grid',

    'dataProvider'=> $dataProvider,

        array(

            'class' => 'CButtonColumn',

            'buttons' => array(

                'update' => array(

                    'label' => '<i class="icon-user"></i> <span>View Property</span>',

                    'options' => array('class' => 'btn btn-small'),

                    'url' => 'Yii::app()->createUrl("properties/update", array("id"=>$data->pr_id))',

                ),

            ),

        ),

    ),

));

The controller for this view is -


      public function actionshowProperties() {


            $criteria = new CDbCriteria(array(

                'condition' => 'pr_id=14',

            ));


            $dataProvider = new CActiveDataProvider('Properties', array(

                'criteria' => $criteria,

            ));


            $this->render('showProperties', array(

                'dataProvider' => $dataProvider,

            ));

      }

Navigating to this page shows the correct data but I need to do something else with this. I now need to be able to show this view in another model.

I call the view using renderPartial, like this -


<?php $this->renderPartial('application.views.properties.showPropertiesInAuction', array('dataProvider' => $dataProvider)); ?>

The partial view does appear in the other view and it shows data but not the data that I filtered using the criteria variable in the original controller.

Does anyone know why this is and how to show the correct data?

Many thanks

Hi

u can just change the line in your controller


 $dataProvider = new CActiveDataProvider($criteria, array('id'=>'id',

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

Hope it will be help

Hi and thanks for the reply.

This does not work unfortunately, I just get the error -

Undefined variable: dataProvider

EDIT - The error is only given when the view is displayed through the renderPartial view and not when directly viewed if this helps.

Please post the other controller/actions and the other views code

Hi

please also change the


$this->render('showProperties',

                'dataProvider' => $dataProvider,

            );

hi, I did not work out this. Just try this code




public function actionshowProperties() {

         $model=new Properties;

         if(isset($_GET['Properties'])){

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

         }

         $criteria = new CDbCriteria(array(

                'condition' => 'pr_id=14',

         ));


         $dataProvider = new CActiveDataProvider($model, array(

                'criteria' => $criteria,

          ));


         $this->render('showProperties', array(

                'dataProvider' => $dataProvider,

            ));

      }



My other code is -

View -


<?php

/* @var $this AuctionsController */

/* @var $model Auctions */

?>


<?php

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id' => 'auctions-form',

    'enableAjaxValidation' => false,

    'htmlOptions' => array(

        'class' => 'form-horizontal',

    ),

    ));

?>


<?php $this->renderPartial('//auctions/buttons/update_buttons', array('model' => $model)); ?>


<?php $this->setPageTitle('Auction - ' . $model->au_name); ?>


<div class="alpha span5">


      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_name', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->textField($model, 'au_name'); ?>

                  <?php echo $form->error($model, 'au_name'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_venue', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->dropDownList($model, 'au_venue', GxHtml::listDataEx(Venues::model()->findAll(), 'v_id', 'v_name')); ?>

                  <?php echo $form->error($model, 'au_venue'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_date_auction', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->datepickerRow($model, 'au_date_auction', array('prepend' => '<i class="icon-calendar"></i>', 'labelOptions' => array('label' => false)));

                  ?>

                  <?php echo $form->error($model, 'au_date_auction'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_date_close', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->datepickerRow($model, 'au_date_close', array('prepend' => '<i class="icon-calendar"></i>', 'labelOptions' => array('label' => false)));

                  ?>

                  <?php echo $form->error($model, 'au_date_close'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_opening_times', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->textField($model, 'au_opening_times'); ?>

                  <?php echo $form->error($model, 'au_opening_times'); ?>

            </div>

      </div>

</div>


<div class="alpha span5">


      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_information', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->textField($model, 'au_information'); ?>

                  <?php echo $form->error($model, 'au_information'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_status', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->toggleButtonRow($model, 'au_status', array('labelOptions' => array('label' => false))); ?>

                  <?php echo $form->error($model, 'au_status'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_website', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->toggleButtonRow($model, 'au_website', array('labelOptions' => array('label' => false))); ?>

                  <?php echo $form->error($model, 'au_website'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($model, 'au_pdf_download_url', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->textField($model, 'au_pdf_download_url'); ?>

                  <?php echo $form->error($model, 'au_pdf_download_url'); ?>

            </div>

      </div>


      <?php $this->endWidget(); ?>


</div>


<div class="alpha span12">

      <?php $this->renderPartial('application.views.properties.showPropertiesInAuction', array('dataProvider' => $dataProvider)); ?>

</div>

and controller is -


      public function actionUpdate($id) {

            $model = $this->loadModel($id, 'Auctions');




            if (isset($_POST['Auctions'])) {

                  $model->setAttributes($_POST['Auctions']);


                  if ($model->save()) {

                        

                  }


                  if (isset($_POST['save'])) {

                        $this->redirect(array('update', 'id' => $model->au_id));

                  }


                  if (isset($_POST['saveclose'])) {

                        $this->redirect(array('index'));

                  }

            }


            $this->render('update', array(

                'model' => $model,

            ));

      }

Sorry guys but the code provided above did not work.

When you access the view directly the data works ok but it seems that when I use renderPartial I have to send a new dataProvider when I call it but I cannot re-use the dataProvider that I created in the action in the controller.

Hi auren,

You can render (or renderPartial) the view that belongs to another controller, but it doesn’t mean that you can reuse the code in that controller by doing so. You have to create the data for the view in the current controller.

Ok, I understand.

I have been able to get it working in another way though.

I have added a new method to the properties model with the following code -




      public function showByAuctionID() {

            $criteria = new CDbCriteria(array(

                'condition' => 'pr_fk_auction_id=14',

            ));


            return new CActiveDataProvider($this, array(

                'criteria' => $criteria,

            ));

      }

I then called in the renderPartial view like this -


<?php $this->renderPartial('application.views.properties.showPropertiesInAuction', array('dataProvider' => CActiveRecord::model('Properties')->showByAuctionID())); ?>

The content of the renderPartial view now looks like this -


<?php


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

    'id' => 'properties-grid',

    'dataProvider' => CActiveRecord::model('Properties')->showByAuctionID(),

    'columns' => array(

        'pr_id',

        array(

            'header' => 'Address',

            'value' => '$data->addressCommaDelim',

        ),

        'pr_status',

        'pr_administrator',

        'pr_fk_auction_id',

        array(

            'class' => 'CButtonColumn',

            'buttons' => array(

                'update' => array(

                    'label' => '<i class="icon-user"></i> <span>View Property</span>',

                    'options' => array('class' => 'btn btn-small'),

                    'url' => 'Yii::app()->createUrl("properties/update", array("id"=>$data->pr_id))',

                ),

            ),

        ),

    ),

));

?>

Is a old threat, but I think you have to set "ajaxUrl" in the correct url (the proper controller which handle the cgridview).


'ajaxUrl'=>$this->createUrl('controller/view')