Filtering grid view issue using CArrayDataProvider


(Attila Naghi) #1

Hi! This is my first post, I hope it is in the right category. For some reason it doesn’t work. It looks like the grid is refreshed after I hit the enter, but i got the same result. It does not work. I’m staying on this during the last 2 days. Can someone help me ? Thx

This is my model :


public function getAllUsers(){

		

		$command = $this->connection_invetory->createCommand(" 

			SELECT id, `name`, active, username, `function`, email ,active FROM users order by id desc;

		")->queryAll();

		$dataProvider = new CArrayDataProvider($command, array(

		    'id'=>'user',

		    'sort'=>array(

           		'defaultOrder'=>'id DESC',

	        ),

		    'pagination'=>array(

		        'pageSize'=>15,

		    ),

		));

		return $dataProvider;

		

	}

This is my controller:


public function actionIndex() {

    	

		$getApplicationNames = Common::model()->getApplications();

		$getAllRoleNames = Common::model()->getAllRoles();

		


		$getAllUsers = Common::model()->getAllUsers();

		

		$filtersForm = new FiltersForm;

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

		

		    $filtersForm->filters = $_GET['FiltersForm'];

		}

		$resultData = $filtersForm->filter($getAllUsers);

		

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

		    'users' => $getAllUsers,

		    'filtersForm' => $filtersForm

		));

}

This is the filterform class from model:


<?php

class FiltersForm extends CFormModel

{

    public $filters = array();

 	public $username;

    public $email;




    /**

     * Override magic getter for filters

     */

    public function __get($name)

    {

    	

        if(!array_key_exists($name, $this->filters))

            $this->filters[$name] = null;

        return $this->filters[$name];

    }

	public function rules()

    {

        return array(

				array('username', 'required'),

		);

    }

	


    /**

     * Filter input array by key value pairs

     * @param array $data rawData

     * @return array filtered data array

     */

    public function filter( $data )

    {

    	

        foreach($data->rawData as $rowIndex => $row) {

            foreach($this->filters AS $key => $value) {

                // unset if filter is set, but doesn't match

                if(array_key_exists($key, $row) AND !empty($value)) {

                    if(stripos($row[$key], $value) === false)

                        unset($data[$rowIndex]);

                }

            }

        }

		//echo "<pre>"; var_dump($data); echo "</pre>";

        return $data;

    }

}

and this is my view :


$this->widget('GridView', array(

		'dataProvider' => $users,

		'id' => 'my_grid_view',

		'filter' => $filtersForm,

		'itemsCssClass'=>'table',

		'pager' => array(

				'class' 				=> 'CLinkPager',

				'prevPageLabel' 		=> 'Previous',

	            'nextPageLabel' 		=> 'Next',

	            'header'				=> '',

				'previousPageCssClass'	=> 'btn btn-info btn-sm',

				'selectedPageCssClass' 	=> 'btn btn-warning btn-sm',

				'internalPageCssClass' 	=> 'btn btn-info btn-sm',

				'firstPageCssClass' 	=> 'btn btn-info btn-sm',

				'nextPageCssClass'		=> 'btn btn-info btn-sm',

		),

		'columns' => array(

			array(

				'name' => 'User ID',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["id"])'

			),

			array(

				'name' => 'Username',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["username"])'

			),

			array(

				'name' => 'Name',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["name"])'

			),

			array(

				'name' => 'Email',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["email"])'

			),

			array(

				'name' => 'Role',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["function"])'

			),

			array(

				'name' => 'Status',

				'type' => 'raw',

				'value' => 'CHtml::encode($data["active"])'

			),

			array(

				'name'  => 'Action',

				'type'  => 'raw',

				'value' => 'CHtml::link("<button  type=\'button\'  class= \'btn btn-info user_ajax\' data-toggle=\'modal\' data-target=\'#myModal\'>Info</button>")',

			),

		),	

		

	));


(Saurabhdhariwal737) #2

You have to make following changes in your code:

For this issue you have to change "name" attribute of CGridView columns. Here you need to use field name of your table as below:

View File:


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

        'id' => 'users-grid',

        'dataProvider' => $users,                

        'filter' => $filtersForm,

        'itemsCssClass'=>'table',

        

        'columns' => array(

                array(

                        'name' => 'user_id', // field name of your table

                        'type' => 'raw',

                        'value' => 'CHtml::encode($data["user_id"])'

                ),

                array(

                        'name' => 'username',

                        'type' => 'raw',

                        'value' => 'CHtml::encode($data["username"])'

                ),

                array(

                        'name' => 'name',

                        'type' => 'raw',

                        'value' => 'CHtml::encode($data["name"])'

                ),

                array(

                        'name' => 'email',

                        'type' => 'raw',

                        'value' => 'CHtml::encode($data["email"])'

                ),

                array(

                        'name' => 'status',

                        'type' => 'raw',

                        'value' => 'CHtml::encode($data["active"])'

                ),

        ),      

                

));

FilterForm class from Model:


public function filter( $data ) {

                

        $arrNewRawData = array();

                

        foreach($data->rawData as $rowIndex => $row) {                        

                foreach($this->filters AS $key => $value) {                

                // unset if filter is set, but doesn't match

                if(array_key_exists($key, $row) AND !empty($value)) {                    

                        if(stripos($row[$key], $value) !== false) {

                                array_push($arrNewRawData, $row);

                        }

                }

            }

        }


        if (!empty($arrNewRawData)) {

                $data->rawData = $arrNewRawData;

        }


        //echo "<pre>"; var_dump($data); echo "</pre>";

        return $data;

}

Hope this helps.

Thanks!