当需要构建 GridView的时候;
常常用 CArrayDataProvider 或者 CActiveDataProvider
这是就需要一个CActiveRecord
比如:
857 $sql = "SELECT * FROM zhuanli_applicantCount where kId=:kId order by percentage desc";
858 $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));
859 $IDARRAY = array();
860
861 $paginationTop = new CPagination(sizeof($rawData));
862 $paginationTop->pageVar = 'page';
863 $paginationTop->pageSize= 10;
864 $paginationButtom = clone $paginationTop;
865 $dataProvider = new CArrayDataProvider($rawData, array(
866 'keyField'=>false,
867 ));
868 $this->render('applicantListMore', array(
869 'dataProvider'=>$dataProvider,
870 'paginationTop'=>$paginationTop,
871 'totalItemCount'=>sizeof($rawData),
872 'paginationButtom'=>$paginationButtom,
873 ));
858 $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));
859 $IDARRAY = array();
860
861 $paginationTop = new CPagination(sizeof($rawData));
862 $paginationTop->pageVar = 'page';
863 $paginationTop->pageSize= 10;
864 $paginationButtom = clone $paginationTop;
865 $dataProvider = new CArrayDataProvider($rawData, array(
866 'keyField'=>false,
867 ));
868 $this->render('applicantListMore', array(
869 'dataProvider'=>$dataProvider,
870 'paginationTop'=>$paginationTop,
871 'totalItemCount'=>sizeof($rawData),
872 'paginationButtom'=>$paginationButtom,
873 ));
如果需要展示的数据主题并不在一个数据表中,而是我们用Coreseek得到的一个数组,或者就是一个普通的数组
如何用数组数据来展现 GridView视图呢?
首先可以去看下$dataProvider =
new CArrayDataProvider($rawData, array(
的数据结构
object(CArrayDataProvider)#37 (10) {
["keyField"]=>
bool(false)
["rawData"]=>
array(24) {
[0]=>
array(2) {
["provinceId"]=>
string(2) "11"
["provinceName"]=>
string(9) "江苏省"
}
……
……
[23]=>
array(2) {
["provinceId"]=>
string(2) "15"
["provinceName"]=>
string(9) "江西省"
}
}
["keyField"]=>
bool(false)
["rawData"]=>
array(24) {
[0]=>
array(2) {
["provinceId"]=>
string(2) "11"
["provinceName"]=>
string(9) "江苏省"
}
……
……
[23]=>
array(2) {
["provinceId"]=>
string(2) "15"
["provinceName"]=>
string(9) "江西省"
}
}
["_id":"CDataProvider":private]=>
NULL
["_data":"CDataProvider":private]=>
NULL
["_keys":"CDataProvider":private]=>
NULL
["_totalItemCount":"CDataProvider":private]=>
NULL
["_sort":"CDataProvider":private]=>
NULL
["_pagination":"CDataProvider":private]=>
NULL
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
好,那我们想要的结果是最后能够
$dataProvider = new CArrayDataProvider($IDARRAY, array(
那就来构建这个数组
734 $IDARRAY = array();
735 $total =0;
736 $totalItemCount = 0;
737 if($result['total'])
738 {
739 foreach($result['matches'] AS $k=>$v)
740 {
741 $IDARRAY[] = array('site_name'=>$v['attrs']['site_name'],'count'=>$v['attrs']['@count']);
742 $total+= $v['attrs']['@count'];
743 }
744 $totalItemCount = $result['total'];
745 $this->sourceMoreIDARRAY=$IDARRAY;
746 $this->sourceMoreNum=$total;
747 }
748
749 $paginationTop = new CPagination($totalItemCount);
750 $paginationTop->pageVar = 'page';
751 $paginationTop->pageSize= 10;
752 $paginationButtom = clone $paginationTop;
753 $dataProvider = new CArrayDataProvider($IDARRAY, array(
735 $total =0;
736 $totalItemCount = 0;
737 if($result['total'])
738 {
739 foreach($result['matches'] AS $k=>$v)
740 {
741 $IDARRAY[] = array('site_name'=>$v['attrs']['site_name'],'count'=>$v['attrs']['@count']);
742 $total+= $v['attrs']['@count'];
743 }
744 $totalItemCount = $result['total'];
745 $this->sourceMoreIDARRAY=$IDARRAY;
746 $this->sourceMoreNum=$total;
747 }
748
749 $paginationTop = new CPagination($totalItemCount);
750 $paginationTop->pageVar = 'page';
751 $paginationTop->pageSize= 10;
752 $paginationButtom = clone $paginationTop;
753 $dataProvider = new CArrayDataProvider($IDARRAY, array(
754 'keyField'=>false,
755 ));
756
757 $this->render('sourceListMore', array(
758 'dataProvider'=>$dataProvider,
759 'paginationTop'=>$paginationTop,
760 'totalItemCount'=>$totalItemCount,
761 'paginationButtom'=>$paginationButtom,
762 ));
来看下
$dataProvider = new CArrayDataProvider($IDARRAY, array(
object(CArrayDataProvider)#37 (10) {
["keyField"]=>
bool(false)
["rawData"]=>
array(456) {
[0]=>
array(2) {
["site_name"]=>
string(0) ""
["count"]=>
int(3046)
}
[1]=>
array(2) {
["site_name"]=>
string(9) "易车网"
["count"]=>
int(904)
}
[455]=>
array(2) {
["site_name"]=>
string(9) "科技讯"
["count"]=>
int(1)
}
}
["_id":"CDataProvider":private]=>
NULL
["_data":"CDataProvider":private]=>
NULL
["_keys":"CDataProvider":private]=>
NULL
["_totalItemCount":"CDataProvider":private]=>
NULL
["_sort":"CDataProvider":private]=>
NULL
["_pagination":"CDataProvider":private]=>
NULL
["_e":"CComponent":private]=>
NULL
["_m":"CComponent":private]=>
NULL
}
OK ,成功,其实用着样的方法,比用SQL 是不是要方便很多?
但是应该适用于,GridView中 column没几个的情况。
如果有很多Column,一般都是某个数据表的数据要展示,当然就用CActiveRecord
$rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));