我正在使用CSqlDataProvider来构造CGridview我不能使用CActiveRecord,因为结果集很大并且引发了内存错误。列需要是可排序的。我该怎么做到这一点?
示例sql
$orders_query_raw = 'select o.order_id, o.customer_name, o.customer_email, o.customer_advertiser, o.payment_method, o.created, o.last_updated, o.currency, o.currency_value, o.status, o.blinking, s.name, ot.text order_total, o.customer_id, op.product_id, o.phonebooking
from `order` o, `order_total` ot, `order_status` s , order_product op
where o.order_id = op.order_id and o.status = s.order_status_id and ot.order_id = o.order_id and s.language_id = '1' and ot.class = 'ot_total' group by o.order_id'
sql dataprovider
$dataProvider = new CSqlDataProvider($orders_query_raw, array(
'totalItemCount'=>$count, // get from a count query
'pagination'=>array(
'pageSize'=>50,
),
));
和gridview
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'id'=>'order-grid',
'columns' => array(
array(
'header'=>'Order ID',
'value'=>array($this, 'gridOrderId'),
'type'=>'raw',
),
array(
'header'=>Yii::t('order', 'Customers'),
'name'=>'customer.fullName',
'value'=>'CHtml::link($data[\'customer_name\'], \'mailto:\'.$data[\'customer_email\'])',
'type'=>'raw',
),
array(
'header'=>Yii::t('order', 'Order total'),
'value'=>'strip_tags($data[\'order_total\'])',
'type'=>'raw',
'htmlOptions'=>array(
'style'=>'text-align:right;',
),
),
array(
'header' => Yii::t('order', 'Date Purchased'),
'name' => 'created',
),
array(
'header'=> Yii::t('order', 'Last modify date'),
'value'=>array($this, 'gridLastModified'),
),
array(
'header' => Yii::t('order', 'Status changed by'),
'value' => array($this, 'gridLastModifiedUserFirstName'),
),
array(
'header' => Yii::t('provider', 'Provider\'s code'),
'value' => array($this, 'gridProviderCode'),
'type' => 'raw',
'htmlOptions'=>array(
'class'=>'nobr',
),
),
array(
'header' => Yii::t('order', 'Follow up'),
'value' => array($this, 'gridFollowUp'),
'type' => 'raw',
),
array(
'header' => Yii::t('order', 'Order status'),
'value' => '$data[\'name\']',
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
'header'=>'Action',
'buttons'=>array(
'update'=>array(
'url'=>'Yii::app()->createUrl(\'order/update\', array(\'order_id\'=>$data[\'order_id\']))',
),
),
),
),
));
由于
答案 0 :(得分:6)
要在数据提供程序为CSqlDataProvider
的网格视图中启用排序(通过单击列标题),您至少需要2件事情:
attributes
that would be sortable为数据提供者定义CSort
对象。name
of the column ,但仅在大小写的情况下指定网格视图的columns
属性,否则如果columns属性保留为空,则无论属性如何在CSort对象中提到的将是可排序的。那说,另一个答案应该适用于sql很简单的情况,来自1个表,但在你的情况下,sql有点复杂,即数据来自多个表,解决方案会略有改变。
在这种情况下,您必须考虑冲突的列名(如果有)以及CSort的attributes
数组的正确规范。
示例:
任何表中都没有冲突的列名(与其他答案相同):
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'order_id, order_total' // csv of sortable column names
)
)
));
然后在你的网格中:
array(
'header'=>Yii::t('order', 'Order total'),
'name'=>'order_total',// to make header clickable to sort
'value'=>'strip_tags($data[\'order_total\'])',
'type'=>'raw',
'htmlOptions'=>array(
'style'=>'text-align:right;',
),
),
列名冲突:
其次,将这些别名指定为attributes
对象中的可排序CSort
:
'attributes'=>array(
'some_alias, some_other_alias'
)
为name
中的列指定columns
:
array(
'header'=>'Foo',
'name'=>'some_alias',
'value'=>'$data[\'some_alias\']' // this is actually redundant in this
// case, because the name will itself pick up the value, and we don't
// need to specify value explicitly if we are not applying any function to it
)
请注意,只需指定排序对象即可启用通过url调用进行排序,除非您希望单击以对标题进行排序,否则无需使用name
。
答案 1 :(得分:1)
尝试以下
$sort = new CSort();
$sort->defaultOrder = 'order_id'; // for initial order
$sort->attributes = array(
'created'
);
$dataProvider = new CSqlDataProvider($orders_query_raw, array(
'totalItemCount'=>$count, // get from a count query
'pagination'=>array(
'pageSize'=>50,
),
'sort'=>$sort
));