displaying Cgridview in yii

2019-09-08 11:17发布

问题:

how can i display a record in Cgridview?

tbl_book:
id
title
author


tbl_in_out:
id
book_id
date_out
date_in

I have created a relationship that the book_id in tbl_in_out belongs to id in tbl_book. what i want to do is to query a record in the tbl_in_out with the corresponding data in tbl_book and display it in the CGridview(sorry for the bad english). Please help!

回答1:

Basic grid view:

// the following code goes in your view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
    'book.title', // assuming the name of the relation is "book" in model of tbl_in_out
    'book.author',
    'date_out',
    'date_in'
)
));

You'll need to pass the data provider from the controller:

$dataProvider=new CActiveDataProvider('InOut'); // assuming the name of your model for tbl_in_out is InOut
$this->render('gridviewname',array('dataProvider'=>$dataProvider));