How to pass custom data to EDataTables in Yii - php

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 ?

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 !

Related

change id checkboxs in CGridView

there are 2 columns check box in cgridViewtable and two bootstrap widgets TbButton in view page.
I can not get value of my checkbox very good. My value in checkboxs transfer into controller but changed id of checkboxs After a period of timeand , and controller don't knew checkbox,
View:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'profile-information-form',
'enableAjaxValidation' => false,
));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'First validation'),));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'End validation'),));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'profile-information-grid',
'dataProvider' => $model->children(),
'filter' => $model,
'columns' => array(
array(
'header' => '',
'value' => '$this->grid->dataProvider->pagination->offset + $row+1', // row is zero based
),
array(
'name' => 'ProfileInformation.user.scope',
'value' => 'CHtml::encode($data->user->scope->name)',
'filter' => Scope::model()->options,
),
array(
'name' => 'id',
'value' => 'CHtml::encode($data->id)',
),
array(
'name' => 'center',
'value' => 'CHtml::encode($data->center)',
),
array(
'name' => 'sendCount',
'value' => 'CHtml::encode($data->sendCount)',
'filter' => '',
),
// Use CCheckbox column with selectableRows = 2 for Select All
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser2(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser1(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
),
));
// action button
$this->endWidget();
Controller:
if (isset($_POST['profile-information-grid_c10']) & isset($_POST['yt0'])) {
////Do action1
}
if (isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])) {
////Do action2
}
}
my problem is in $_POST['profile-information-grid_c10'] and _POST['profile-information-grid_c12'] , before id were $_POST['profile-information-grid_c8'] and $_POST['profile-information-grid_c10'] , and now is $_POST['profile-information-grid_c12'] and $_POST['profile-information-grid_c14'].
my problem is very involved. I cannot explain very good.
I want to have a fix id.
I donto why change id of check box?
Can I assign ids for checkboxs?
Can I assign ids for checkboxs? Sure, you better explicitly set checkbox column id:
'class' => 'CCheckBoxColumn', 'id'=>'column1', ... see these docs.
This way you firmly set it and you'll have no things like these yt0, yt2...
The same you can do for the buttons. See TButton docs.
Also you need to use double && in logical condition:
isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])

YiiBooster. TbEditableColumn. Property "attribute" should be defined

I have a problem with TbEditedableColumn in YiiBooster 4.0.1
View:
$this->widget(
'application.extensions.booster.widgets.TbGridView',
array(
'type' => 'striped bordered',
'dataProvider' => new CActiveDataProvider('Stats'),
'columns' => array(
'pid',
array(
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
//'model' => $model,
//'attribute' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)
)
),
)
);
Controller:
public function actionEditableSaver()
{
Yii::import('application.extensions.booster.components.TbEditableSaver');
$es = new TbEditableSaver('Stats');
$es->update();
}
When I try to save the edited fields, I got this exception: Property "attribute" should be defined.
$es->attributes is empty.
How to fix that? Thanks.
From the source code, TbEditableSaver::update() obtains the attribute from a post or get parameter name:
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
}
In order for this parameter to be sent in the update request it needs to be defined in the editable array. To fix this:
'class' => 'application.extensions.booster.widgets.TbEditableColumn',
'name' => 'login',
'sortable' => false,
'editable' => array(
'name' => 'login',
'url' => $this->createUrl('stats/editableSaver'),
'placement' => 'right',
'inputclass' => 'span3'
)

how to display array to CGridView (yii framework)

I have next variables:
$type_model = ProductTypeModel::model()->findByPk($id);
$prod = $type_model->product;
Now in $prod:
array
(
0 => ProductModel#1
(
[CActiveRecord:_new] => false
[CActiveRecord:_attributes] => array
(
'product_id' => '6'
'product_type_id' => '5'
)
...
)
1 => ProductModel#2
(
'product_id' => '8'
'product_type_id' => '5'
)
...
How i can display my products in CGridView?
Thx.
I Suppose you are using CarrayDataProvider. So in your controller
$dataProvider = new CArrayDataProvider($prod);
Here $product could be any array you want to display in CgridView. Now
In you view write this.
$gridColumns = array(
array(
'header' => 'First Name',
'value' => 'ProductTypeModel::model()->findByPk($data->product_id)->key',
'htmlOptions' => array('style' => 'text-align:center;')
),
$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider,));
As in CarrayDataprovider array is obtained so we cant use relations in it. Thats why u have to write 'ProductTypeModel::model()->findByPk($data->product_id)->key'
Here you can display anything attribute of ProductTypeModel so u can replace above mentioned key with your desired attribute
Try this ...
it automatic convert to array data provider.
$dataProvider=new CArrayDataProvider($type_model->product);
Thanks all. By means of answers "naveen goyal" and "jailed abroad" i did like this:
$dataProvider=new CArrayDataProvider($type_model->product);
$dataProvider->keyField = 'product_id';
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'Title',
'value' => 'CHtml::encode($data["product_title"])',
),
)));
Nice work for me.

Yii CDetailView translate sub-property

<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'htmlOptions' => array('style' => 'width:425px; float:left; margin-right:20px; word-break:break-all', 'class' => 'detail-view table table-striped table-condensed'),
'attributes' => array(
'id',
'name',
'street',
'housenumber',
'zipcode',
'city',
array(
'label' => Yii::t('api', 'Country'),
'name' => Yii::t('api', 'country.name'),
),
),
));
?>
I want to translate the values for the attributes label and name in the array for the columns.
But it only translate the label and not the name.
Can someone tell me, what I am doing wrong?
name is the column name in the database, I think you want to adjust value, try this:
'label' => Yii::t('api', 'Country'),
'name' => 'country.name', //in reality you don't need this since you are setting the value
'value' => Yii::t('api', $model->country->name),

Adding a label to a symfony 2 form field

I'm trying to modify a piece of code that generates an input field using the Symfony 2 framework. The thing he creates the field with a default label equals to the name id of the field, in this case; "amount".
<?php
//++++++++++ descrizione
echo $view['form'] -> row($form["amount"], array(
//widget
"widgetArgs" => array(
"attr" => array(
'class' => 'input-small tooltipRight',
'id' => "gift_amount"
),
"tooltip"=>"gift.tooltip.amount",
"translation_domain" =>"brand"
),
"labelArgs" => array(
"label_attr" => array(
'class' => 'control-label',
)) ,"rowType"=>2
)
);
?>
How can i edit this to make it show a custom label?
You have to add the label properties:
<?php
echo $view['form']->row($form["amount"], array(
'widgetArgs' => array(
"attr" => array(
'class' => 'input-small tooltipRight',
'id' => "gift_amount"
),
'tooltip'=>'gift.tooltip.amount',
'translation_domain' =>'brand'
),
'label' => 'My ammount',
));

Categories