Yii2 Call to a member function getImageUrl() on a non-object - php

I have a GridView where it displays data and one of them is an image. Here is my code in my GridView:
echo GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fullName',
[
'header' => '<a style="cursor: pointer;">Attachment</a>',
'format' => 'html',
'value' => function ($data) {
return Html::img($data->getImageUrl(), ['class' => 'reim-attach']);
},
],
'receipt_company',
'description',
'date',
'amount',
[
'attribute' => 'chargeable',
'value' => function ($model) {
return $model['chargeable'] ? 'Chargeable' : 'Non-chargeable';
},
],
'GST_amount',
'date_noted',
[
'attribute' => 'status',
'label' => 'Status',
'content' => function ($model, $key, $index, $column) {
if ($model['status'] == "Pending") {
return Html::button('Pending', ['class' => 'status-pending']);
} elseif ($model['status'] == "Draft") {
return Html::button('Draft', ['class' => 'status-pending']);
} elseif ($model['status'] == "Approved") {
return Html::button('Approved', ['class' => 'status-approved']);
} else {
return Html::button('Rejected', ['class' => 'status-rejected']);
}
}
],
[
'label' => 'Action',
'content' => function ($model, $key, $index, $column) {
if($model['status'] == "Pending") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
} elseif($model['status'] == "Draft") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Save Reimbursement', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'saveReimbursement(value)', 'data-toggle'=>'tooltip','title'=>'Save Reimbursement', 'data' => [ 'confirm' => 'Are you sure you want to save this reimbursement?', 'method' => 'post', ]]);
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
}
}
]
],
]);
I get the following error on the line return Html::img($data->getImageUrl(), ['class' => 'reim-attach']):
Call to a member function getImageUrl() on a non-object
Here's a snippet of my controller:
$model = new Reimbursement();
$reimbursementQuery = new Query;
$reimbursementQuery->select([])->from('reimbursement')->andwhere(['company_id' => new \MongoId($session['company_id'])]);
if (isset(Yii::$app->request->getBodyParams()['period'])) {
if(Yii::$app->request->getBodyParams()['period'] != '') {
echo Yii::$app->request->getBodyParams()['period'];
$refreshData = true;
$selectedPeriodID = Yii::$app->request->getBodyParams()['period'];
$selectedPeriod = Periods::find()->where(['_id' => $selectedPeriodID])->one();
$reimbursementQuery->andWhere(['date_reimbursed' => array('$gte' => date('Y/m/d', strtotime($selectedPeriod->p_start)), '$lte' => date('Y/m/d', strtotime($selectedPeriod->p_end))) ]);
}
}
$reimbursements = $reimbursementQuery->all();
$dataProvider = new ArrayDataProvider([
'allModels' => $reimbursements,
]);
if($refreshData) {
return $this->renderPartial('_reports', [
'dataProvider' => $dataProvider,
]);
}
$dataProvider->pagination->pageSize = 10;
return $this->render('reports', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
'rModel' => $rModel,
'employees' => $employees,
'contacts' => $contacts,
'periods' => $periods,
]); exit;
Here's also my model:
public function getImageUrl()
{
return Url::to('#web/' . $this->attachment, true);
}
Hope somebody could point out where I missed and how to get rid of the error.

Its happen because you access none data in database or you didnt specify your function. . .
[ 'header' => '<a style="cursor: pointer;">Attachment</a>', 'format' => 'html','value' => function ($data) {
$attachment = YourModel::find($model->yourId)->one()->attachment;
return Html::img(Url::to('#web/' . $attachment, true), ['class' => 'reim-attach']);
},
],

Related

Getting unknown property in Yii

under the course content there, I want to make the Active column to show either "Yes" or **"No"**At first it was showing "1" or "0", but that's not what I want to show. These are my attempted codes, and I'm getting this error. Appreciate it if anyone can please guide me.
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\Coursecontent::isActive
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\DetailView;
/* #var $this yii\web\View */
/* #var $model app\models\Course */
$this->title = $model->course_name;
$this->params['breadcrumbs'][] = ['label' => 'Courses', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="course-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('<< Back', ['index'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Update', ['update', 'id' => $model->course_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->course_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'course_name',
'description',
['attribute' => 'active', 'value' => $model->isActive,'contentOptions' => ['style' => $model->active == 1 ? 'color:green' :'color:red']],
'lastupdate',
],
]) ?>
<h2>Course Content</h2>
<?= Html::a('Create Coursecontent', ['coursecontent/create'], ['class' => 'btn btn-success']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'chapter_name',
'description',
'video_link',
['attribute' => 'active',
'value' => function ($model, $key, $index, $column) {return $model->isActive;},
'contentOptions' => function ($model, $key, $index, $column) {
return $model->active == 1 ? ['style' => 'color:green'] : ['style' => 'color:red'];
}],
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['class' => 'text-center'],
'buttons' => [
'view' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>',['coursecontent/view', 'id' => $model->coursecontent_id]);
},
'update' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>',['coursecontent/update', 'id' => $model->coursecontent_id]);
},
'delete' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>',['coursecontent/delete', 'id' => $model->coursecontent_id],
['data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]);
},
]],
],
]); ?>
</div>
Have to make 2 changes to your code.
Change all $model->isActive to $model->active.
Return "Yes" or "No" depending to $model->active value.
Update DetailView:
[
'attribute' => 'active',
'value' => function ($model) {
return $model->active ? "Yes" : "No";
},
'contentOptions' => ['style' => $model->active ? 'color:green' :'color:red']
],
Update GridView:
[
'attribute' => 'active',
'value' => function ($model) {
return $model->active ? "Yes" : "No";
},
'contentOptions' => function ($model, $key, $index, $column) {
return $model->active ? ['style' => 'color:green'] : ['style' => 'color:red'];
}
],

Yii2 - Keep child data in select option from parent dependent dropdown in uclead Tabular Input

I made a tabular input where I would enter several records at once into one table in the database.
I use this extension: https://github.com/unclead/yii2-multiple-input/wiki/Usage#tabular-input
From this extension, I combine it with dependent dropdown.
Here is the code.
public function actionCreateMulti() {
$request = Yii::$app->request;
$models = [new MaterialLocations()];
if ($request->isAjax) {
/**
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
if ($request->isGet) {
return [
'title' => "Create new MaterialLocations",
'content' => $this->renderAjax('_form_create_multi', [
'models' => $models,
]),
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
} else if ($eglData = $request->post('MaterialLocations', [])) {
foreach (array_keys($eglData) as $index) {
$models[$index] = new MaterialLocations();
}
if (Model::loadMultiple($models, $request->post()) && ActiveForm::validateMultiple($models)) { // if failed
return [
'title' => "Create new MaterialLocations",
'content' => $this->renderAjax('_form_create_multi', [
'models' => $models,
]),
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
} else { // success
foreach ($models as $single) {
$single->save(false);
}
return [
'forceReload' => '#crud-datatable-pjax',
'title' => "Create new MaterialLocations",
'content' => '<span class="text-success">Create MaterialLocations success</span>',
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
];
}
} else {
return [
'title' => "Create new MaterialLocations",
'content' => '<pre>' . VarDumper::dumpAsString($request->post()) . '</pre>',
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Order', ['class' => 'btn btn-primary', 'type' => "submit"])
];
}
} else {
/**
* Process for non-ajax request
*/
new ForbiddenHttpException(); // just for test
}
}
And the following is a view of the tabular input.
<?php $form = ActiveForm::begin(); ?>
<?php try {
echo TabularInput::widget([
'id' => 'some-id',
'models' => $models,
'cloneButton' => true,
'addButtonPosition' => MultipleInput::POS_FOOTER,
'sortable' => true,
'columns' => [
[
'name' => 'cupboard_id',
'title' => $models[0]->getAttributeLabel('cupboard_id'),
'enableError' => true,
'type' => Select2::className(),
'options' => [
'data' => $cupBoards,
'options' => [
'prompt' => '== Choose a vendor ==',
'onChange' => $jsOnChange, // this is handle onChange to find list row
'class' => 'form-control cupboard-id',
]
],
],
[
'name' => 'row',
'title' => $models[0]->getAttributeLabel('row'),
'type' => 'dropDownList',
'items' => [],
'value' => function ($data) {
return $data->row;
},
'options' => [
'class' => 'form-control row-cupboard',
],
'columnOptions' => [
'style' => 'width: 100px;',
]
],
],
]);
} catch (Exception $e) {
echo $e->getMessage();
} ?>
<?php ActiveForm::end(); ?>
As you can see, the first input column is: 'cupboard_id',
If there is onChange, it will generate data for the 'row' column.
<?php
$urlOnChangeCupboard = Url::toRoute(['cupboards/find-row-column-available']);
$jsOnChange = <<<JS
var cupboard = jQuery(this);
var row = cupboard.closest('tr');
var rowCupboard = row.find('.row-cupboard');
var columnCupboard = row.find('.column-cupboard');
jQuery.post('$urlOnChangeCupboard', { id : cupboard.val() }, function(response){
rowCupboard.find('option').remove().end();
columnCupboard.find('option').remove().end();
jQuery.each(response.data.rows, function(index, value){
rowCupboard.append('<option value=' + value + '>' + value + '</option>');
});
});
JS;
?>
<?php
function callBack($cupboardID) {
$data = Cupboards::find()->where(['id' => $cupboardID])->one();
$rowsRaw = range($data->rows_start, $data->rows_amount);
$rows = array_combine($rowsRaw, $rowsRaw);
return $rows;
}
?>
The problem is, when POST (Submit) the data, and then it is validated and there are still errors,
data from the column row should still be based on the cupboard selection.
At this time, after posting, the data from 'row' is empty
Please help.
Fixed with columnOptions closure
[
'name' => 'row',
'title' => $models[0]->getAttributeLabel('row'),
'type' => 'dropDownList',
'items' => function ($model) {
$data = [];
if (isset($model->row)) {
$cupBoards = Cupboards::find()->where(['id' => $model->cupboard_id])->one();
$rowsRaw = range($cupBoards->rows_start, $cupBoards->rows_amount);
$data = array_combine($rowsRaw, $rowsRaw);
}
return $data;
},
'value' => function ($data) {
return $data->row;
},
'options' => [
'class' => 'form-control row-cupboard',
],
'columnOptions' => [
'style' => 'width: 100px;',
]
],

Put style tag in php echo

Is it possible to put <style> tag inside and echo? My scenario is that I have a table of reimbursements and there's this "Type" column, where if the type is an "Ad Hoc Reimbursement", the whole table <tr>'s text color and font style will be changed:
And here's what it should look like:
I am using Yii 2.0 php framework. Here's my code:
echo GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'header' => 'Employee ID',
'value' => 'employeeId'
],
[
'header' => 'Identification <br> Number',
'value' => 'IDnumber'
],
[
'header' => 'Employee <br> Name',
'format' => 'html',
'value' => 'fullName'
],
[
'header' => 'Attachment',
'format' => 'html',
'value' => function ($model) {
return !empty($model->attachment) ? Html::img($model->getImageUrl(), ['class' => 'reim-attach']): 'No Attachment';
},
],
[
'attribute' => 'receipt_company',
'header' => 'Merchant',
],
'description',
[
'attribute' => 'date',
'header' => 'Date <br><span style= "color:gray;font-size:8pt;"> (dd-mm-yyyy)</span>',
],
'currency',
[
'attribute' => 'amount',
'format'=>['decimal',2],
'value' => function ($model){
return !empty($model['amount']) ? $model['amount'] : 0.00;
}
],
[
'attribute' => 'exchange_rate',
'header' => 'Exchange <br> Rate',
'format'=>['decimal',2],
'value' => function ($model){
return !empty($model['exchange_rate']) ? $model['exchange_rate'] : 0.00;
}
],
[
'attribute' => 'converted_amount',
'header' => 'Converted <br> Amount',
'format'=>['decimal',2],
'value' => function ($model){
return !empty($model['converted_amount']) ? $model['converted_amount'] : 0.00;
}
],
[
'attribute' => 'chargeable',
'header' => 'Chargeable to <br> Client',
'value' => function ($model) {
return $model['chargeable'] ? 'Chargeable' : 'Non-chargeable';
},
],
[
'attribute' => 'date_noted',
'header' => 'Date Modified <br><span style= "color:gray;font-size:8pt;"> (dd-mm-yyyy)</span>',
],
[
'attribute' => 'status',
'label' => 'Status',
'content' => function ($model, $key, $index, $column) {
if ($model['status'] == "Pending") {
return Html::button('Pending', ['class' => 'status-pending']);
} elseif ($model['status'] == "Draft") {
return Html::button('Draft', ['class' => 'status-draft']);
} elseif ($model['status'] == "Approved") {
return Html::button('Approved', ['class' => 'status-approved']);
} elseif ($model['status'] == "Rejected") {
return Html::button('Rejected', ['class' => 'status-rejected']);
} elseif ($model['status'] == "Reimbursed") {
return Html::button('Reimbursed', ['class' => 'status-reimbursed']);
}
}
],
'type',
[
'label' => 'Action',
'content' => function ($model, $key, $index, $column) {
if($model['status'] == "Pending") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve'])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject']);
} elseif ($model['status'] == "Draft") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
. Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3', 'data-toggle'=>'tooltip', 'title'=>'Update'])
.'&nbsp'
. Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive','data-toggle'=>'tooltip', 'title'=>'Delete', 'data' => ['confirm' => 'Are you sure you want to delete this reimbursement?', 'method' => 'post']]);
} elseif ($model['status'] == "Approved") {
if ($model->type == 'Ad Hoc Reimbursement') {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
.Html::button('<i class="fa fa-check-square-o"></i> Reimburse', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'reimburse(value)', 'data-toggle'=>'tooltip','title'=>'Reimburse']);
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Approve'])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Reject']);
}
} else {
if ($model->type == 'Ad Hoc Reimbursement') {
echo "<style>.table-striped > tbody > tr { font-style: italic !important;color: #259A5A !important; }</style>";
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
.Html::button('<i class="fa fa-check-square-o"></i> Reimburse', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reimburse(value)', 'disabled' => true, 'data-toggle'=>'tooltip','title'=>'Reimburse']);
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2', 'data-toggle'=>'tooltip', 'title'=>'View'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Approve'])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip', 'disabled' => true, 'title'=>'Reject']);
}
}
}
]
],
]);
In the last part, you can see the if ($model->type == 'Ad Hoc Reimbursement') and inside it, I put echo "<style>.table-striped > tbody > tr { font-style: italic !important;color: #259A5A !important; }</style>"; which I think is not right since it's NOT working.
Do you have any ideas about this?
You're applying conditional styling within your column configuration, which isn't going to work.
What you want to do is define the styling as a class, and conditionally change the row class in the <tr>
echo GridView::widget([
'dataProvider' => $dataProvider,
'rowOptions' => function ($model, $index, $widget, $grid){
return ['class'=> ($model->type == 'Ad Hoc Reimbursement') ? 'ad-hoc-reimbursement' : null];
},
]);
<style>
tr.ad-hoc-reimbursement {font-style: italic !important;color: #259A5A !important; }
</style>

Save New Order of Rows in DB of Sortable Table

I've researched a lot of problems like this but I can't seem to find the right solution for me. I am using Yii2 framework in PHP nad MongoDB for my database. I'm also using a Yii2 sortable extension for GridView and already installed it in my web application using composer.
Here's my GridView in my view:
<?php echo SortableGridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code_type',
'payroll_name',
'cpf_type',
[
'label' => 'Action',
'content' => function ($model, $key, $index, $column) {
if ( ($model->company_id != null) && ($model->active == 1) ) {
if(in_array($model->payroll_name, array('Basic Salary', 'Overtime Pay', 'Daily/Hourly Wage', 'Commission', 'Bonus', 'Housing Allowance', 'Educational Allowance', 'Travel Allowance', 'Marriage Allowance', 'Leave Encashment', 'Reimbursement'))){
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2']);
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
. Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3'])
.'&nbsp'
. Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete-earnings', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive', 'data' => ['confirm' => 'Are you sure you want to delete this earnings item?', 'method' => 'post']])
.'&nbsp'
. Html::button('Unset as Default', ['value' => (string)$model->_id, 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'unset(value)']);
}
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
. Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value' => Url::to(['update-earnings']).'&id=' . (string)$model->_id, 'class' => 'btn btn-success btn-view btn-responsive','id' => 'modalButton3'])
.'&nbsp'
. Html::a('<span class="glyphicon glyphicon-remove"></span>', ['delete-earnings', 'id' => (string)$model->_id], ['class' => 'btn btn-danger btn-responsive', 'data' => ['confirm' => 'Are you sure you want to delete this earnings item?', 'method' => 'post']])
.'&nbsp'
. Html::button('Set as Default', ['value' => (string)$model->_id, 'class' => 'btn btn-info btn-responsive', 'onclick'=>'set(value)']);
}
}
]
],
'sortableAction' => Yii::$app->request->baseUrl . '/index.php?r=payroll-items/earnings-index'
]); ?>
Model:
public function behaviors()
{
return [
'sort' => [
'class' => SortableGridBehavior::className(),
'sortableAttribute' => 'sortOrder'
],
];
}
Controller:
public function actions()
{
return [
'sort' => [
'class' => SortableGridAction::className(),
'modelName' => PayrollItems::className(),
],
];
}
I found this problem but I don't know what to put inside updateworkshops.php (see top answer) since I am using MongoDB and not MySQL.
As in my current progress, I can only drag my GridView rows but the new order still can't be saved in my database.
Can somebody please explain to me how to do this?

Change the button action in Gridview based on a model attribute value Yii2

I generated a simple application by CRUD generator...
In the View page there is a action column assigned with some buttons like view, update, delete....
all I want is to create a status button....
If the status is inactive it should ask me and change the status into active and vice versa
This is my code:
'suspend' => function($url, $model) {
return Html::a(
'<span class="btn btn-xs btn-danger icon-remove bigger-80"style="margin-left:5px;"></span>',
$url,
['title' => Yii::t('app', 'Inactivate')]
);
},
'activate' => function($url, $model) {
return Html::a(
'<span class="btn btn-xs btn-success icon-ok bigger-80"style="margin-left:5px;"></span>',
$url,
['title' => Yii::t('app', 'Activate')]
);
},
Try with this..
[
'class' => 'yii\grid\ActionColumn',
'template' => '{activate}{deactivate}',
'buttons' => [
'activate' => function ($url, $model) {
if($model->status==1)
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('app', 'Activate'),
]);
},
'deactivate' => function ($url, $model) {
if($model->status==0)
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('app', 'Deactivate'),
]);
},
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'acivate') {
$url =Url::toRoute(['controller/activate', 'id' => $model->id]);
return $url;
}
if ($action === 'deactivate') {
$url =Url::toRoute(['controller/deactivate', 'id' => $model->id]);
return $url;
}
}
],
You need to include use yii\helpers\Url; in your view
You May also Change Url and Icon Conditionally...:)
['class' => 'yii\grid\ActionColumn',
'buttons'=>[
'servicestatus' => function ($url, $model)
{
if($model->service_status =="Paid")
{
return Html::a(
'<span class="glyphicon glyphicon-remove red"></span>',
['service-payment/status', 'id' => $model->id],
[
'title' => 'Status',
'data-pjax' => '0',
]
);
}else
{
return Html::a(
'<span class="glyphicon glyphicon-ok green"></span>',
['service-payment/status', 'id' => $model->id],
[
'title' => 'Status',
'data-pjax' => '0',
]
);
}
},
],
'template'=>'{view} {delete} {servicestatus}',
'header'=>'<label>Action</label>',
],

Categories