Cgridview Open Without Records

Hello,

I have a question for CGridView.

Is it possible in a CGridview only records indicate when a keyword is entered in a column.

That means when opening the page should the grid be empty and only display records which was found.

Thanks for your help.

Carsten

Did you mean the CGridView filter ? (As search on the top of each column)

Give us an example

Yes, i mean the filter on each column.

I have a CGridview which show all records from the table, but i will not show all records.

If the User enter an Filter in the grid then it shows the records matched.

And if all Filter are empty, the grid must be empty to.

Thanks

Are Filters Displayed in your CGridView ?

Please post your CGridview code and model attributes to help you

model:




<?php


/**

 * This is the model class for table "device".

 *

 * The followings are the available columns in table 'device':

 * @property integer $deviceID

 * @property integer $productID

 * @property string $devicename

 * @property string $serialnumber

 * @property string $spit_dealer

 * @property integer $spit_dealer_postalcode

 * @property string $spit_dealer_city

 * @property string $invoicenumber

 * @property string $invoicedate

 * @property integer $companyID

 * @property string $registration_email

 * @property string $date_waranty

 * @property string $date_fos

 * @property string $state_fos

 * @property string $state_customer

 * @property string $registration_date

 */

class Device extends CActiveRecord

{

	public function getProducts()

    { 

		return CHtml::listData(Product::model()->findAll(), 'productID', 'title');

    }	


	public function getStateCustomer()

    { 

		return CHtml::listData(DeviceStateCustomer::model()->findAll(), 'id', 'title');

    }	


	public function getStateFos()

    { 

		return CHtml::listData(DeviceStateFos::model()->findAll(), 'id', 'title');

    }	


	// Return Product_Title.

	public function getProductTitle(){

		$product = Product::model()->findByPk($this->productID);

		return $product->title;

	}

	// Return Productarticlenumber.

	public function getProductarticlenumber(){

		$product = Product::model()->findByPk($this->productID);

		return $product->articlenumber;

	}

	// Return Product_Warrantytime.

	public function getProductWarrantytime(){

		$product = Product::model()->findByPk($this->productID);

		return $product->warrantytime;

	}

	// Return Product_Repaircosts.

	public function getProductRepaircosts(){

		$product = Product::model()->findByPk($this->productID);

		return $product->repaircosts;

	}


	

	// Return StateCustomer_Title.

	public function getStateCustomerTitle(){

		$statecustomer = DeviceStateCustomer::model()->findByPk($this->state_customer);

		return $statecustomer->title;

	}

	

	// Return StateFos_Title.

	public function getStateFosTitle(){

		$statefos = DeviceStateFos::model()->findByPk($this->state_fos);

		return $statefos->title;

	}

	

	// Return CompanyFirma.

	public function getCompanyFirma(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->company;

	}


	// Return CompanyStrasse.

	public function getCompanyStrasse(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->street;

	}


	// Return CompanyPLZ.

	public function getCompanyPLZ(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->postalcode;

	}


	// Return CompanyOrt.

	public function getCompanyOrt(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->city;

	}


	// Return CompanyLand.

	public function getCompanyLand(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->country;

	}


	// Return CompanyAnsprechpartner.

	public function getCompanyAnsprechpartner(){

		$company = Company::model()->findByPk($this->companyID);

		return $company->contact;

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'device';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('productID, serialnumber, spit_dealer, spit_dealer_postalcode, spit_dealer_city, invoicedate, invoicenumber', 'required'),

			array('serialnumber', 'unique', 'message' => Yii::t("Device","This serialnumber already exists.")),

			array('productID, spit_dealer_postalcode, companyID', 'numerical', 'integerOnly'=>true),

			array('devicename, serialnumber, spit_dealer, spit_dealer_city, invoicenumber, registration_email, state_fos, state_customer', 'length', 'max'=>255),

			array('invoicedate, date_waranty, date_fos', 'safe'),

            array('invoicedate, date_waranty, date_fos', 'default', 'value' => date('dd.mm.yyyy H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('deviceID, productID, devicename, serialnumber, spit_dealer, spit_dealer_postalcode, spit_dealer_city, invoicenumber, invoicedate, companyID, registration_email, date_waranty, date_fos, state_fos, state_customer', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'DeviceStateFos'=>array(self::BELONGS_TO, 'DeviceStateFos', 'state_fos'),

			'Product'=>array(self::BELONGS_TO, 'Product', 'productID'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'deviceID' => Yii::t('Device','Device'),

			'productID' => Yii::t('Device','Product'),

			'devicename' => Yii::t('Device','Devicename'),

			'serialnumber' => Yii::t('Device','Serialnumber'),

			'spit_dealer' => Yii::t('Device','Spit Dealer'),

			'spit_dealer_postalcode' => Yii::t('Device','Spit Dealer Postalcode'),

			'spit_dealer_city' => Yii::t('Device','Spit Dealer City'),

			'invoicenumber' => Yii::t('Device','Invoice No.'),

			'invoicedate' => Yii::t('Device','Invoice Date (yy-mm-dd)'),

			'companyID' => Yii::t('Device','Company'),

			'registration_email' => Yii::t('Device','Registration Email'),

			'date_waranty' => Yii::t('Device','Date Waranty'),

			'date_fos' => Yii::t('Device','Date Fos'),

			'state_fos' => Yii::t('Device','State Fos'),

			'state_customer' => Yii::t('Device','State Customer'),

			'registration_date' => Yii::t('Device','Registration Date'),

		);

	}


	

	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 *

	 * Typical usecase:

	 * - Initialize the model fields with values from filter form.

	 * - Execute this method to get CActiveDataProvider instance which will filter

	 * models according to data in model fields.

	 * - Pass data provider to CGridView, CListView or any similar widget.

	 *

	 * @return CActiveDataProvider the data provider that can return the models

	 * based on the search/filter conditions.

	 */

	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;

        If (Yii::app()->user->isAdmin())

		{

            //$criteria->condition='companyID=0';

		}

		else

		{

			If (Yii::app()->user->isGuest)

			{

            $criteria->condition='companyID like \'%\'';

			}

			else

			{

			

            $criteria->condition='companyID='.Yii::app()->user->created_by;

			}

		}

		


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

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

		$criteria->compare('devicename',$this->devicename,true);

		$criteria->compare('serialnumber',$this->serialnumber,true);

		$criteria->compare('spit_dealer',$this->spit_dealer,true);

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

		$criteria->compare('spit_dealer_city',$this->spit_dealer_city,true);

		$criteria->compare('invoicenumber',$this->invoicenumber,true);

		$criteria->compare('invoicedate',$this->invoicedate,true);

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

		$criteria->compare('registration_email',$this->registration_email,true);

		$criteria->compare('date_waranty',$this->date_waranty,true);

		$criteria->compare('date_fos',$this->date_fos,true);

		$criteria->compare('state_fos',$this->state_fos,true);

		$criteria->compare('state_customer',$this->state_customer,true);

		$criteria->compare('registration_date',$this->registration_date,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}


	/**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return Device the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

}



CGridView:




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

	'id'=>'device-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

			'name'=>'devicename',

			'value' => 'CHtml::link($data->devicename, Yii::app()->createUrl("product/viewsmall",array("id"=>$data->productID)), array("target"=>"_blank","title"=>"Seriennummer finden"))',

			'type'  => 'raw',

		),

		'serialnumber',

		'invoicenumber',

		array(

			'name'=>'invoicedate',

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

			'filter'=>false,

		),

		array(

			'name'=>'date_waranty',

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

			'filter'=>false,

		),

		array(

			'name'=>'date_fos',

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

			'filter'=>false,

		),

		array(

			'header'=>Yii::t('Device','State Fos'),

            'value'=>'$data->DeviceStateFos->title',

		),

		array(

			'header'=>Yii::t('Product','Repair Costs in €'),

            'value'=>'$data->Product->repaircosts',

		),

	),

)); ?>



You set filter to false for a lot of columns, so for these columns the search field on the header columns not displayed.

Is there specific reason to did that?

Yes, I need the filter only on serialnumber and invoicenumber.

maybe in your controller do something like


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

   	$parts->attributes=$_GET['ModelName'];

   	$yourdataprovider = $model->search();

 }

else {

   $yourdataprovider =$model-> some_empty_value;

//it would still need to know what model it was searching.

  }


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

		'model'=>$model,

		'yourdataprovider'=>$yourdataprovider

	));



i’d add an empty value message too since it will always be empty when first viewing

ie in your gridview widget




<?php

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

'id'=>'product-parts-list',

'dataProvider'=>$yourdataprovider,

'emptyText'=>'Please search invoice or serial number to view records',

'columns'=>array(

......

personally I’d always show data and just let them search. The info they are looking for could have been right on top and they wouldn’t have had to search to see it which would create a better user interaction.

Thank you very much for your help.

It works.