I have a gridview with pjax.
<div class="request-index">
<div id="ajaxCrudDatatable">
<?=
GridView::widget([
'id' => 'crud-datatable',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => true,
'columns' => require(__DIR__ . '/_columns.php'),
'toolbar' => [
['content' =>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add Request', ['create'], ['role' => 'modal-remote', 'title' => 'Create new Requests', 'class' => 'btn btn-success']) .
Html::a('<i class="glyphicon glyphicon-repeat"></i> Reload', [''], ['data-pjax' => 1, 'class' => 'btn btn-primary', 'title' => 'Reset Grid']) .
'{toggleData}' .
'{export}'
],
],
'panel' => [
'type' => 'primary',
'heading' => '<i class="glyphicon glyphicon-list"></i> Requests listing',
'before' => '<button type="button" class="btn btn-danger btn-secondary"> Belum Selesai : <strong>'. $count_request_belum_selesai .'</strong> </button>',
'after' => BulkButtonWidget::widget([
'buttons' => Html::a('<i class="glyphicon glyphicon-trash"></i> Delete All', ["bulk-delete"], [
"class" => "btn btn-danger btn-xs",
'role' => 'modal-remote-bulk',delete this item'
]),
]) .
'<div class="clearfix"></div>',
]
])
?>
</div>
if you see from code above, please concern with
'before' => '<button type="button" class="btn btn-danger btn-secondary"> Not finished : <strong>'. $count_request_belum_selesai .'</strong> </button>',
When this button is clicked, I want to running a function in my model named RequestSearch :
public function searchRequestBelumSelesai(){
$query = Request::findAll(['tanggal_selesai' => NULL]);
return $query;
}
Which is the gridview will be displayed the result, is it possible ?
In the action of the controller that will receive the click of your button, you must modify the dataProvider given to the View, so that it looks like something like this:
$dataProvider= new ActiveDataProvider([
'query' => $your_model->searchRequestBelumSelesai(),
]);
Related
I generated a model,controller and a view by Gii,And I enabled the actionColumn in index.php.
I have delete button to delete the records.The buttons are working good but,It doesn't shows me the Alert box to confirm my action.
I am attaching my code here.
Can anyone tell what I am missing?
Index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
$this->title = 'Cities';
$this->params['breadcrumbs'][] = '/ '.$this->title;
?>
<div class="cities-index">
<h2><?= Html::encode($this->title) ?>
<?php //echo $this->render('_search', ['model' => $searchModel]); ?>
<?= Html::a('Create Cities', ['create'], ['class' => 'btn btn-success','style'=>'float:right']) ?>
</h2>
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'country_id',
[
'attribute'=>'country_id',
'value'=>'country.country_name',
],
'city_name',
'city_tax',
[ 'class' => 'yii\grid\ActionColumn',
'header'=>'Action',
'template'=>'{view}{update}{delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-success icon-search bigger-120" style="margin-left:5px;"></span>', $url, [
'title' => Yii::t('app', 'View'),
]);
},
'update' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-info icon-pencil blue bigger-120" style="margin-left: 5px;"></span>', $url, [
'title' => Yii::t('app', 'Update'),'<span class="glyphicon glyphicon-search"></span>'
]);
},
'delete' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-danger icon-trash bigger-120" style="margin-left: 5px;" ></span>',
$url, [
'title' => Yii::t('app', 'Remove'),'<span class="glyphicon glyphicon-search"></span>',
'data-confirm' => 'Are You sure',
'data-method' => 'post'
]);
},
],
],
],
]); ?>
</div>
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'])
.' '
.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'])
.' '
.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'])
.' '
. 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'])
.' '
. 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'])
.' '
.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'])
.' '
.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'])
.' '
.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'])
.' '
.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'])
.' '
.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'])
.' '
.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>
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'])
.' '
. 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'])
.' '
. 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']])
.' '
. 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'])
.' '
. 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'])
.' '
. 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']])
.' '
. 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?
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'])
.' '
.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', ]])
.' '
.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'])
.' '
.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'])
.' '
.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', ]])
.' '
.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']);
},
],
I have adjusted the widget like:
> import kartik/grid/GridView
{{ use('kartik/grid/GridView') }}
> use GridView::widget(...)
{{
grid_view_widget(
{
'dataProvider': dataProvider,
'export': false,
'responsive': true,
'condensed': true,
'toolbar': [
'{export}',
'{toggleData}'
]
}
)
}}
but the table was cut and toolbar wasn't shown:
Try
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => $columns,
'pjax' => true,
'condensed' => true,
'panel' => [
'heading' => '<h3 class="panel-title">' . $this->title,
'before' => Html::a('<i class="glyphicon glyphicon-plus"></i>', ['create'],
['class' => 'btn btn-success']) . ' ' .
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['class' => 'btn btn-info']),
],
'export' => false,
'toolbar' => [
'{toggleData}'
]])
With this atleast your toolbar will be shown. Let me know what happens. :)