1

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.

8
  • Is this Yii or Yii2? Commented Jul 17, 2016 at 9:03
  • Need this in yii only Commented Jul 17, 2016 at 9:05
  • I see. I can't be sure about it then. I know only in Yii2. Commented Jul 17, 2016 at 9:05
  • Can't we try same procedure in yii. Commented Jul 17, 2016 at 9:08
  • @EdvinTenovim Can you plz give answer for yii2, I will try to make it in yii Commented Jul 17, 2016 at 9:37

1 Answer 1

1

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'=>'[email protected]'),
     );

     // 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
));
Sign up to request clarification or add additional context in comments.

6 Comments

I have dynamic columns in grid, it can be change, its name and number columns too with its value. So how can i implement it? Here u have taken only username and email in columns array.
this seems another answer .. anyway you can set the dataProvider based in your dinamic array and also you can use a var for the column and you can build the var with the columns coniguration as you need .. but this i repeart seems another question to me ..
foreach ($header as $cols){ $column[] = array( 'name'=> $cols, 'value'=>'isset($data[\''.$cols.'\'])?$data[\''.$cols.'\']:""', ); } I have used this to set columns in grid: 'columns' => $column, Its makes my work done.. Thanxs for ur support @scaisEdge
I have update the answer with a suggestion based on your comment
Well then is that you are looking for?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.