Yii Framework Forum: Filtering grid view issue using CArrayDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Filtering grid view issue using CArrayDataProvider Rate Topic: -----

#1 User is offline   Attila Naghi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 12-January 15

Posted 13 January 2015 - 02:44 AM

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>")',
			),
		),	
		
	));

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users