Search code examples
phpyiicgridviewedatatables

How to pass custom data to EDataTables in Yii


I am using Yii EDataTables Extension : http://www.yiiframework.com/extension/edatatables/

I have Widget of EDataTables as follow

$this->createWidget(
    'ext.EDataTables.EDataTables', array(
        'id'           => 'items',
        'dataProvider' => $dataProvider,
        'ajaxUrl'      => '/ajax-update',
        'columns'      => array(array(
                                'user_id',
                                'email',
                                'name',
                                'created_at:date:Updated',
                                'updated_at:date:Updated',
                                )
        ),
        'options' => array(
            'bStateSave'    => false,
            'bPaginate'     => true,
        ),
    )
);

I want to pass custom ( value,array or object to the widget so i can use it in the row value )

I have looked around many places but i couldn't find way to do it. Could you please advice how to do that ?


Solution

  • Assuming you have custom column Value for name for example :

    $nameExp = '$this->grid->options["nameArray"][$data->user_id]';

    And Array like this

    $namesInArray = array(
                    '1'=>'FirstName LastName 1',
                    '2'=>'FirstName LastName 2',
                    '3'=>'FirstName LastName 3',
                    '4'=>'FirstName LastName 4',
                );
    

    The possible way to pass extra data not from data provide is to send it with options or htmloptions array ( because all other params for the widget has to have an attribute in the class referring to )

    So You can pass the object or the array in the options array and use it as below

    $nameExp = '$this->grid->options["nameArray"][$data->user_id]';
    $namesInArray = array(
        '1'=>'FirstName LastName 1',
        '2'=>'FirstName LastName 2',
        '3'=>'FirstName LastName 3',
        '4'=>'FirstName LastName 4',
    );
    $this->createWidget(
        'ext.EDataTables.EDataTables', array(
            'id'           => 'items',
            'dataProvider' => $dataProvider,
            'ajaxUrl'      => '/ajax-update',
            'columns'      => array(array(
                                        'user_id',
                                        'email',
                                        'name',
                                        array('class' => 'CDataColumn', 'name' => 'Name from array options', 'sortable' => false, 'value' => $nameExp, 'type' => 'raw'),
                                        'created_at:date:Updated',
                                        'updated_at:date:Updated',
                                    )
            ),
            'options' => array(
                'bStateSave'    => false,
                'bPaginate'     => true,
                'nameArray'     => $namesInArray
            ),
        )
    );
    

    You will be able to use/access the Array, value or the object with that row value expression and do the logic you need there !