view
Using the onclick Event in gridview?
error : Trying to get property of non-object
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete} {myButton}',
'buttons' => [
'format' => 'raw',
'myButton' => function ($model) {
return Html::a('<li class="fa fa-folder"></li> info sale', ['#'], [
'class' => 'btn btn-primary btn-xs',
'onclick'=>'saleinfo('.$model->id.')',
]);
}
]
],
You could try this way
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete} {myButton}',
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
....
}
if ($action === 'update') {
...
}
if ($action === 'myButton') {
$url =\yii\helpers\Url::to(['/your_controller/your_action', 'id' => $model->id]);
return $url;
}
},
'buttons' => [
'myButton' => function($url, $model){
return Html::a('<li class="fa fa-folder"></li> info sale', ['#'], [
'class' => 'btn btn-primary btn-xs',
]);
}
],
],
Related
<?= GridView::widget([
'id' => 'CompanyGrid',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'floatHeader'=>true,
'floatOverflowContainer'=>true,
'floatHeaderOptions'=>['top'=>'0'],
'pjax'=>true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
'enableReplaceState' => true,
]
],
'hover'=>true,
'toolbar' => [
'{export}',
'{toggleData}'
],
'panel' => [
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> Companies</h3>',
'type'=>'secondary',
'before'=>Html::button('Create Company', ['value'=>Url::to('index.php?r=Company/company/create'), 'title' => 'Create Company', 'class' => 'btn btn-success', 'id' => 'modalButton']),
'after'=>false,
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
'CompanyID',
'CompanyName',
['class' => 'kartik\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
'view' => function($url, $model){
return Html::a('<span class="fa fa-eye"></span>', ['view', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'data-pjax'=>'w0',
'title' => Yii::t('yii', 'View Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'update' => function($url, $model){
return Html::a('<span class="fa fa-edit"></span>', ['update', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'title' => Yii::t('yii', 'Edit Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'delete' => function($url, $model){
return Html::a('<span class="fa fa-trash"></span>', ['delete', 'id' => $model->CompanyID], [
'class' => '',
'data' => [
'confirm' => 'Are you absolutely sure? This action is not reversible',
'method' => 'post',
],
]);
}
],
],
],
]); ?>
In Controller
public function actionView($id)
{
$model = Company::findOne($id);
return $this->renderAjax('view', [
'model' => $model,
]);
}
I am new in yii2-advance-apps and i am trying to solve this problem. Already worked on this problem and cannot find any solution for this.
The problem is when I click on the gridview actions buttons, it works. However, after PJax, the button just don't work anymore.
However, if i reload the page, the button works again.
How to solve this problem. Thank you in advance.
'data-pjax' => 0, in action buttons options and not 'w0'
I am working on an application with user platform shown below:
Controller
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Model
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_login_id' => Yii::t('app', 'User Login ID'),
'user_password' => Yii::t('app', 'Password'),
'user_type' => Yii::t('app', 'User Type'),
'is_block' => Yii::t('app', 'Block Status'),
'is_confirmed' => Yii::t('app', 'Block Status'),
'confirmed_at' => Yii::t('app', 'Date Confirmed'),
'created_at' => Yii::t('app', 'Created At'),
'created_by' => Yii::t('app', 'Created By'),
'updated_at' => Yii::t('app', 'Updated At'),
'updated_by' => Yii::t('app', 'Updated By'),
'current_pass' => Yii::t('app','Current Password'),
'new_pass' => Yii::t('app','New Password'),
'retype_pass' => Yii::t('app', 'Retype Password'),
'admin_user' => Yii::t('app', 'Admin Username'),
'create_password' => Yii::t('app', 'Password'),
'confirm_password' => Yii::t('app', 'Confirm Password'),
];
}
View
<?php Pjax::begin() ?>
<div class="box box-primary">
<div class="box-body">
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{items}\n{pager}",
'columns' => [
'user_login_id',
'user_type',
[
'attribute' => 'created_at',
'value' => function ($data) {
return (!empty($data->created_at) ? Yii::$app->formatter->asDate($data->created_at) : ' - ');
},
],
[
'header' => Yii::t('urights', 'Confirmation'),
'value' => function ($model) {
if (!$model->is_confirmed) {
return '<div class="text-center"><span class="text-success">' . Yii::t('urights', 'Confirmed') . '</span></div>';
} else {
return Html::a(Yii::t('urights', 'Confirm'), ['confirm', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to confirm this user?'),
]);
}
},
'format' => 'raw',
],
[
'header' => Yii::t('urights', 'Block status'),
'value' => function ($model) {
if ($model->is_block) {
return Html::a(Yii::t('urights', 'Unblock'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to unblock this user?'),
]);
} else {
return Html::a(Yii::t('urights', 'Block'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-danger btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to block this user?'),
]);
}
},
'format' => 'raw',
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
],
],
]);
?>
</div>
</div>
<?php Pjax::end() ?>
As shown in the diagram, when I click on Confirm (Green Button), it shown disable the button and turn it to Confirmed. Then set is_confimred to 0 (zero).
Also, if I click on Block (Red Button), it should change the button caption to unblock and set is_block to 0.
However, I am not getting result, but I have the page shown below:
How do I resolve it?
You should add toggle functions for both confirm and block to the controller.
public function actionConfirm($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_confirmed = $model->is_confirmed ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
public function actionBlock($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_block = $model->is_block ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
I have a listing page where I display email lists and if I want to delete any list I do not actually delete it but it is marked as deleted in the table column.
For the email_lists I am using the GridView widget for displaying the lists and I want to highlight a row if it is marked as deleted in the table email_lists.
Any idea how can I highlight the whole row instead of column?
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'total_recipients',
'list_type',
// 'is_deleted',
[
'class' => 'yii\grid\ActionColumn',
'header' => 'Actions',
'template' => '{update}{view_list}{delete}',
'buttons' => [
'view_list' => function($url, $data, $key) {
return Html::a('<i class="glyphicon glyphicon-eye-open"></i>', ['/promos/promolistemails/index', 'id' => $data->id], ['title' => 'View List Emails']);
},
'update' => function($url, $data, $key) {
if ($data->list_type == 'custom') {
return Html::a('<i class="fa fa-pencil"></i>', '#.', ['title' => 'Edit List Emails', 'data-url' => yii\helpers\Url::to(['/promos/promolists/update', 'id' => $data->id]), 'class' => 'edit-list']);
}
},
'delete' => function($url, $data, $key) {
if ($data->list_type == 'custom') {
return Html::a('<i class="fa fa-trash"></i>', $url, ['title' => 'Delete List', 'data-method' => 'post', 'id' => 'delete-list']);
}
},
]
],
],
]); ?>
i need to use the rowOptions for the GridView
'rowOptions'=>function ($model, $key, $index, $grid){if($model->is_deleted){return ['class'=>'red'];}},
Try This one
'rowOptions' => function($model, $key, $index, $grid){
if($model->is_deleted){
return ['class' => 'danger'];
}
},
Fighting form last 3 days not able to find what exactly problem is. When after search I export data it export all data, expected to export search result.Please help!
My Controller
public function actionAdvancesearch()
{
$searchModel = new CandidateSearch();
$model = new Candidate();
$dataProvider1 = $searchModel->search(Yii::$app->request->queryParams);
if(Yii::$app->request->post()){
$postedData=Yii::$app->request->post();
$searchModel = new CandidateSearch();
$dataProvider1 = $searchModel->searchAdvanced();
return $this->render('candidateAdvSearch', ['dataProvider1' => $dataProvider1,
'model' => $model ,
'searchModel' => $searchModel,
'posted'=>"posted",
'postedData'=>$postedData]);
}else{
return $this->render('candidateAdvSearch', ['searchModel' => $searchModel, 'model' => $model, 'dataProvider1' => $dataProvider1]);
}
}
View
Pjax::begin();
if($dataProvider1){
$gridColumns = [
[
'attribute'=>'HRMS_candidateID',
'label'=>'Candidate ID',
'vAlign'=>'middle',
'width'=>'190px',
'value'=>function ($model, $key, $index, $widget) {
return $model->HRMS_candidateID;
},
'format'=>'raw'
],
[
'attribute'=>'HRMS_candidateFirstName',
'label'=>'Candidate FirstName',
'vAlign'=>'middle',
'width'=>'190px',
'value'=>function ($model, $key, $index, $widget) {
return $model->HRMS_candidateFirstName;
},
'format'=>'raw'
],
[
'attribute'=>'HRMS_candidateLastName',
'label'=>'Candidate LastName',
'vAlign'=>'middle',
'width'=>'190px',
'value'=>function ($model, $key, $index, $widget) {
return $model->HRMS_candidateLastName; },
'format'=>'raw'
],
];
$fullExportMenu = ExportMenu::widget([
'dataProvider' => $dataProvider1,
'columns' => $gridColumns,
'target' => ExportMenu::TARGET_POPUP,
'fontAwesome' => true,
'pjaxContainerId' => 'kv-pjax-container',
'dropdownOptions' => [
'label' => 'Full',
'class' => 'btn btn-default',
'itemsBefore' => [
'<li class="dropdown-header">Export All Data</li>',
],
],
]);
// Generate a bootstrap responsive striped table with row highlighted on hover
echo GridView::widget([
'dataProvider' => $dataProvider1,
'columns' => $gridColumns,
'pjax' => true,
'pjaxSettings' => ['options' => ['id' => 'kv-pjax-container']],
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i> Candidate Advanced Search</h3>',
],
'toolbar' => [
$fullExportMenu,
],
'striped'=>true,
'hover'=>true,
'responsive'=>true,
'hover'=>true,
'resizableColumns'=>true,
'persistResize'=>false,
'columns' => $gridColumns,
]);
}
Pjax::end();
How can put an ActiveForm in a gridview column? The following is the code I made: I tried to render the page such that it includes the active form which I need.
'columns' => [
[ 'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'width: 30px; height: 30px'],
],
[ 'format' => 'raw',
'value' => function($model) { return "<p class='feedback'>". $model->KOMENTAR ."</p><br><p class='feedback-date'>". $model->TANGGAL ."</p><hr><div id='replay-". $model->ID_KOMENTAR."'><ul></ul></div>";},
],
[ 'format' => 'raw',
'contentOptions'=>['style'=>'width: 5px;'],
'value' => function($model) {
if($model->id == Yii::$app->user->identity->id) {
return Html::a('<i class="glyphicon glyphicon-share-alt"></i>',null,['id'=> 'replay-to-'. $model->ID_KOMENTAR ]).' '.
Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id]).' '.
Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], ['data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']]);
}
return Html::a('<i class="glyphicon glyphicon-share-alt"></i>',['feedback', 'id' => $model->id],['id'=> 'replay-to-'. $model->ID_KOMENTAR ]);
},
],
[
'content' => $this->render('feedback_test'),
],
But I got this error:
PHP Warning – yii\base\ErrorException
call_user_func() expects parameter 1 to be a valid callback, function '
<div class="feedback-form">
<p>test</p>
</div>' not found or invalid function name
How do I include the active form in the grid view's column?
Try this.
[
'content' => function($model, $key, $index, $column) {
echo $this->render('feedback_test');
OR
echo $this->render('feedback_test', ['model' => $model]);
},
],
The above answer outputted the form above my grid. I wanted the form in one of my columns for every row of data. I ended up customizing an ActionColumn like this:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{map}',
'contentOptions' => ['class' => 'text-center'],
'buttons' => [
'map' => function ($model) use ($m, $r) {
return $this->render('_form', ['model' => $m, 'req' => $model, 'ref' => $r]);
},
],
'urlCreator' => function ($action, $model) {
if ($action === 'map') {
return $model;
}
},
]