开发者

yii relational datagrid widget

Using Yii's CGridView widget I want to show data from two or more DB tables in a data grid like

display orders and customers开发者_如何转开发 information in dataGrid using widget

Any Idea?

Thanks in advance.


You can display related data by using the relations attributes of the model and the 'value'/'filter' attributes of CGridView's columns. For instance, assume that each order has a 'customer' relation defined in the model's relations read-only attribute. You could easily display information from both tables as such, assuming that $model is a search instance of your order model.

$this->widget('zii.widgets.grid.CGridView',array(
    'id' => 'order-grid',
    'itemsCssClass' => 'dataGrid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'order_number',
        array(
            'name' => 'customer_id',
            'value' => '$data->customer->first_name . " " . $data->customer->last_name',
            'filter' => Html::listData(Customer::model()->findAll(),'id','name'),
        ),
    ...
));

This assumes that you want to use a drop-down as the filter. There are other filters you can use, or you can hack in custom filtering to your dataProvider method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜