YII CGridView error - php

controller:
public function actionItem($textdata){
$dataProvider = new CActiveDataProvider('Mytest', array(
'criteria' => array(
'condition' => 'type="'.$textdata.'"',
),
));
$bleble = $textdata;
$this->render('Item', array(
'dataProvider' => $dataProvider,
'bleble' => $bleble,
));
}
view:
$bleble = $bleble;
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'sample_id',
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value' =>'$data->id',
'type' =>'raw',
"value" => function($data){
echo "text".$data->id;
},
),
NEED !! :
$bleble = $bleble;
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'sample_id',
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value' =>'$data->id',
'type' =>'raw',
"value" => function($data){
echo $bleble.$data->id;
},
),
error :
PHP notice
Undefined variable: bleble
need to download the text of the link that is how I controller ($ text) $ text is the one I have to write out the table but does not work when it is a variable, normal text works for example echo "text".$data->id;

You can do it like this:
"value" => function($data) use ($bleble){
return $bleble.$data->id;
}
Hope this works for you

Related

Cannot use object of type TbGridView as array Yiibooster

I'm trying to set up a new grid view in yii booster while passing a variable through at the start of the view to sort the formatting.
I presume I am not passing the variable properly by this line
$gridColumns = $this->widget('booster.widgets.TbGridView', array(
How would I go about using this variable? I can create a new form array okay not using yiibooster but with the widget activated it no longer likes the variable name
The issue is I'm getting the error in the title of the post.
$gridColumns = $this->widget('booster.widgets.TbGridView', array(
'id' => 'delegate-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
// 'id',
array(
'name' => 'forename',
'type' => 'raw',
'value' => 'CHtml::link($data->forename, array("user/view", "id" => $data->id))',
),
'surname',
// 'facilities',
// 'telephone',
// 'address_id',
/*
'logo_path',
*/
array(
'class' => 'booster.widgets.TbButtonColumn',
),
),
));
$groupGridColumns = $gridColumns;
$groupGridColumns[] = array(
'name' => 'firstLetter',
'value' => 'substr($data->surname, 0, 1)',
'headerHtmlOptions' => array('style' => 'display:none'),
'htmlOptions' => array('style' => 'display:none')
);
$this->widget('booster.widgets.TbGroupGridView', array(
'id' => 'user-grid',
'type' => 'striped bordered condensed highlight',
//'template' => "{items}",
'dataProvider' => $model->search(),
'filter' => $model,
'extraRowColumns' => array('firstLetter'),
'extraRowExpression' => '"<b style=\"font-size: 3em; color: #333;\">".substr($data->surname, 0, 1)."</b>"',
'extraRowHtmlOptions' => array('style' => 'padding:10px;text-align: center;'),
'columns' => $groupGridColumns,
));
It is because you are giving an object of type CGridView to TbGroupGridView as parameter.
$groupGridColumns = $gridColumns;
You are putting an object of type TbGridView($gridColumns) in $groupGridColumns and then give it to your TbGroupGridView, but TbGroupGridView columns property expects its value be an array of arrays(definition of columns) and so throws new exception when in the first cell of $groupGridColumns find an object.
You do not need the first part and with some changes, your code should work fine with filtering enabled.
$this->widget('booster.widgets.TbGroupGridView', array(
'id' => 'user-grid',
'type' => 'striped bordered condensed highlight',
'dataProvider' => $model->search(),
'filter' => $model,
'extraRowColumns' => array('firstLetter'),
'extraRowExpression' => '"<b style=\"font-size: 3em; color: #333;\">".substr($data->surname, 0, 1)."</b>"',
'extraRowHtmlOptions' => array('style' => 'padding:10px;text-align: center;'),
'columns' => array(
array(
'name' => 'forename',
'type' => 'raw',
'value' => 'CHtml::link($data->forename, array("user/view", "id" => $data->id))',
),
'surname',
array(
'name' => 'firstLetter',
'value' => 'substr($data->surname, 0, 1)',
'headerHtmlOptions' => array('style' => 'display:none'),
'htmlOptions' => array('style' => 'display:none')
)
array(
'class' => 'booster.widgets.TbButtonColumn',
),
)
));

Showing blank rows for filters in Yii2.0 with GridView

I have set up GridView to crfeate my table in Yii2.0 as follows:
<?= \yii\grid\GridView::widget([
'dataProvider' => $model->dataProvider,
'filterModel' => $model->searchModel,
'columns' => [
[
'label' => Yii::t( $cat, 'Id' ),
'value' => 'id',
],
[
'label' => Yii::t( $cat, 'Title' ),
'format' => 'raw',
'value' => function ( $data ) {
if ( $data['status_code'] != 5 )
{
return Html::a( $data['title'], '/signer/view/' . $data['id'] );
}
else
{
return $data['title'];
}
},
],
[
'label' => Yii::t( $cat, 'Description' ),
'value' => 'description',
],
[
'label' => Yii::t( $cat, 'Filename' ),
'value' => 'filename',
],
[
'label' => Yii::t( $cat, 'Status' ),
'value' => 'status',
'contentOptions' => function ( $data ) {
$statuses = [
1 => 'text-primary', # New
2 => 'text-warning', # Unsigned
3 => 'text-warning', # Partially signed
4 => 'text-success', # Signed
5 => 'text-danger', # Deleted
];
return [ 'class' => $statuses[$data['status_code']] ];
}
],
[
'label' => Yii::t( $cat, 'Created' ),
'value' => 'created',
],
//[ 'class' => 'yii\grid\ActionColumn' ],
],
]);
?>
I get all the correct data, but instead of filter inputs, I get empty rows.
Why is that? What am I missing?
PS: The search model itself works fine, meaning, when I add to the url ?title=asd it actually get the search results!
According to the documentation of the $filterModel property:
Note that in order to show an input field for filtering, a column must have its yii\grid\DataColumn::$attribute property set or have yii\grid\DataColumn::$filter set as the HTML code for the input field.
So you need to set yii\grid\DataColumn::$attribute property on your columns and in most of the cases this makes the value unnecessary:
<?= \yii\grid\GridView::widget([
'dataProvider' => $model->dataProvider,
'filterModel' => $model->searchModel,
'columns' => [
[
'label' => Yii::t( $cat, 'Id' ),
'attribute' => 'id',
],
[
'label' => Yii::t( $cat, 'Title' ),
'format' => 'raw',
'attribute' => 'title',
'value' => function ( $data ) {
if ( $data['status_code'] != 5 )
{
return Html::a( $data['title'], '/signer/view/' . $data['id'] );
}
else
{
return $data['title'];
}
},
],
[
'label' => Yii::t( $cat, 'Description' ),
'attribute' => 'description',
],
[
'label' => Yii::t( $cat, 'Filename' ),
'attribute' => 'filename',
],
[
'label' => Yii::t( $cat, 'Status' ),
'attribute' => 'status',
'contentOptions' => function ( $data ) {
$statuses = [
1 => 'text-primary', # New
2 => 'text-warning', # Unsigned
3 => 'text-warning', # Partially signed
4 => 'text-success', # Signed
5 => 'text-danger', # Deleted
];
return [ 'class' => $statuses[$data['status_code']] ];
}
],
[
'label' => Yii::t( $cat, 'Created' ),
'attribute' => 'created',
],
//[ 'class' => 'yii\grid\ActionColumn' ],
],
]);
?>
Another possible reason for the blank line: (not in posters exact case)
Missing/incorrect declaration of the public function rules() in the search model. In Yii 1 you could concatenate the string, in Yii2 they need to be actual array elements.
return [
[['authorId, title, publishFrom'], 'safe'], //WRONG
[['authorId', 'title', 'publishFrom'], 'safe'], //CORRECT
];

Yii view not work, EColumnsDialog

picture shows what I have and what they want to have
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value'=>'$data->id',
),
I have to do so that the "value => id" instead display an NO.ID Started to display an TXT_ ID NO.ID
By using createCommand
Yii::app()->db->createCommand('SELECT CONCAT('TXT ',id),other_fields.. FROM table WHERE 1');
This will definitely help you
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value' =>'$data->id',
'type' =>'raw',
"value" => function($data){
echo "TXT ".$data->id;
},
),

Pagination not working in Yii Framework

I am using Yii Rights modules for ACL where I want pagination for Permissions page.
Following is my code:
In AuthItemController.php
public function actionPermissions() {
$dataProvider = new RPermissionDataProvider('permissions', array(
'pagination' => array(
'pageSize' => 10,
//'class' => 'CPagination', //showing an error
'itemCount' => 32
)));
// Get the roles from the data provider
$roles = $dataProvider->getRoles();
$roleColumnWidth = $roles !== array() ? 75 / count($roles) : 0;
// Initialize the columns
$columns = array(
array(
'name' => 'description',
'header' => Rights::t('core', 'Item'),
'type' => 'raw',
'htmlOptions' => array(
'class' => 'permission-column',
'style' => 'width:25%',
),
),
);
// Add a column for each role
foreach ($roles as $roleName => $role) {
$columns[] = array(
'name' => strtolower($roleName),
'header' => $role->getNameText(),
'type' => 'raw',
'htmlOptions' => array(
'class' => 'role-column',
'style' => 'width:' . $roleColumnWidth . '%',
),
);
}
$view = 'permissions';
$params = array(
'dataProvider' => $dataProvider,
'columns' => $columns,
);
// Render the view
isset($_POST['ajax']) === true ? $this->renderPartial($view, $params) : $this->render($view, $params);
}
And in View
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'bordered',
'dataProvider' => $dataProvider,
'template' => '{pager}{items}',
'emptyText' => Rights::t('core', 'No authorization items found.'),
'htmlOptions' => array('class' => 'grid-view permission-table'),
'columns' => $columns,
'pager' => array(
'header' => '',
'hiddenPageCssClass' => 'disabled',
'maxButtonCount' => 3,
'cssFile' => false,
'class' => 'CLinkPager',
'prevPageLabel' => '<i class="icon-chevron-left"></i>',
'nextPageLabel' => '<i class="icon-chevron-right"></i>',
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
),
'pagerCssClass' => 'pagination',
));
I have already implemented pagination for other pages by using same method but here in rights module its not working. Its not showing any error but also not displaying pagination links/buttons.
I had the same problem when implementing multiple grids in one page, the solution that worked for me was , I checked the url the grid ajax call,and before update I manipulated the url, and set the correct url and parameters before ajax request!
like:
$this->widget('zii.grid.GridView', array(
'id' => 'group-grid-customers-list-not-scheduled',
'dataProvider' => $notScheduledVisitedDataProvider ,
'beforeAjaxUpdate' => '
function(id , options)
{
options.url = options.url.split("&test=test&");
options.url = options.url[0] + "&test=test&" + $(".search-form form").serialize();
}',
'columns' => array(
'col1',
'col2',
'col3',
),
));

How to format date in listing page

I am new to Yii framework and just started to work on an existing website. I have a listing page and my requirement was to add a new field 'review_date_time' and I could managed to display it in listing. Now my question is how to change the format of the date and how to show a white space if date is not there in table field.Right now it is displaying 0000-00-00 00:00:00 if no date is there.
My code for listing
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'series-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array('header' => 'Category', 'name' => 'category.title'),
'exam_year',
'title',
'review_date_time',
array(
'class' => 'CButtonColumn',
),
),
));
If it is showing 0000-00-00 00:00:00 then it means that, that value is the default value in the db table, hence you'll have to use the value property of CDataColumn:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'series-grid',
'dataProvider' => $model->search(),
'filter' => $model,
// 'nullDisplay'=>'',
'columns' => array(
array('header' => 'Category', 'name' => 'category.title'),
'exam_year',
'title',
// 'review_date_time',
array(
'name'=>'review_date_time',
'value'=>'$data->review_date_time=="0000-00-00 00:00:00"?"":$data->review_date_time'
)
array(
'class' => 'CButtonColumn',
),
),
));
Or try the nullDisplay property of CGridView (if you are storing null and overriding afterFind to format null as 0000-00-00 00:00:00):
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'series-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'nullDisplay'=>'',
'columns' => array(
array('header' => 'Category', 'name' => 'category.title'),
'exam_year',
'title',
'review_date_time',
array(
'class' => 'CButtonColumn',
),
),
));

Categories