doufenpaiyu63706 2016-07-17 09:02
浏览 54
已采纳

如何使用自定义数组在yii中创建网格?

I have created an array in php.

Array
(
    [0] => Array
        (
            [ip_addr] => 152.12.40.205
            [Management_col-2] => Success
            [Management_col-3] => Success
        )

[1] => Array
    (
        [ip_addr] => 152.12.40.206
        [Management_col-2] => Success
        [Management_col-3] => Error
    )

[2] => Array
    (
        [ip_addr] => 152.12.40.207
        [Management_col-2] => NA
        [Management_col-3] => Success
    )

[3] => Array
    (
        [ip_addr] => 152.12.40.209
        [Management_col-2] => Success
        [Management_col-3] => Success
    )

);

I will have dynamic columns like ip_addr, Management_col-2, ....and so on with there respective dynamic values.

I want to use it in Grid view of YII.

$nc3DataProvider = new CArrayDataProvider($alldata);


$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'nc3-audit-grid',
'dataProvider' => $nc3DataProvider,

));

I also want to use filter on the table.

Plz help me for this.

Thank you so much in advance.

  • 写回答

1条回答 默认 最新

  • douxian9060 2016-07-17 11:02
    关注

    You can use CArrayDataProvider this a sample

    Controller

    public function actionGridViewArray() {
     $rawData=array(
             array('id'=>1, 'username'=>'from', 'email'=>'array'),
             array('id'=>2, 'username'=>'test 2', 'email'=>'hello@example.com'),
         );
    
         // or using: $rawData=User::model()->findAll();
         $arrayDataProvider=new CArrayDataProvider($rawData, array(
             'id'=>'id',
             /* 'sort'=>array(
                 'attributes'=>array(
                     'username', 'email',
                 ),
             ), */
             'pagination'=>array(
                 'pageSize'=>10,
             ),
         ));
         $params =array(
             'arrayDataProvider'=>$arrayDataProvider,
         );
         if(!isset($_GET['ajax'])) $this->render('grid_view_array', $params);
         else  $this->renderPartial('grid_view_array', $params);
     }
    

    View (gridview)

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $arrayDataProvider,
        'columns' => array(
            array(
                'name' => 'username',
                'type' => 'raw',
                'value' => 'CHtml::encode($data["username"])'
            ),
            array(
                'name' => 'email',
                'type' => 'raw',
               'value' => 'CHtml::link(CHtml::encode($data["email"]), "mailto:".CHtml::encode($data["email"]))',
            ),
        ),
    ));
    

    And buildin the column dinamically

    foreach ($header as $cols){ 
       $column[] = array( 'name'=> $cols, 
       'value'=>'isset($data[\''.$cols.'\'])?$data[\''.$cols.'\']:""', ); 
    }
    
    
    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $arrayDataProvider,
        'columns' => $column
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?