How to display data from two tables in TbGridView using DataProvider?

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

In this wiki I will show how to display data from two tables in Bootstrap TbGridView using dataProvider.

In your controller/action

public function actionIndex()
 {
	 
    $rawData=Yii::app()->db->createCommand("SELECT a.id as ID, a.first_name as FName, a.last_name as LName, a.status as Status, b.country_name as Country FROM user a LEFT JOIN country_list b ON a.country_id = b.id")->queryAll();  //Also can use where condition for particular use.
	
	// or using: $rawData=User::model()->findAll();
	
	$dataProvider=new CArrayDataProvider($rawData, array(
		'id'=>'user',
		'sort'=>array(
		'attributes'=>array(
			'ID', 'FName', 'LName', 'Status', 'Country'
			),
		 ),
			'pagination'=>array(
			'pageSize'=>10,			//records display
		  ),
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

In your view file

$this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'user-info-grid',
	'dataProvider'=>$dataProvider,
        'type'=>'striped bordered condensed',
        'template'=>'{summary}{pager}{items}{pager}',
		'columns'=>array(
		'ID',
		'FName',
		'LName',
		'Country',				
		array(
		'name'=>'Status',
		'type'=>'raw',
		'value' =>'($data[Status]==1)? "<span class=\"label label-success\">Active</span>": "<span class=\"label label-important\">Inactive</span>"',
		),
	),
)); 

Working fine... for more help please leave a comment.