Hide row in yii2 gridview depending on actioncolumn - yii2

I have a gridview in which I have custom actioncolumn according to user authorization and other conditions. Due to this conditions, in few rows, there are no action item button for few rows. I want to hide these rows.
I can hide the row on condition by rowoption. But I want to know how can I hide it according to the action item button.
Following is the index view page -
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use frontend\modules\workpermit\models\Workpermit;
use frontend\modules\workpermit\models\Extension;
use frontend\modules\workpermit\models\WpindexSearch;
use frontend\modules\workpermit\models\Aclpath;
use yii\widgets\Pjax;
/* #var $this yii\web\View */
/* #var $searchModel frontend\modules\workpermit\models\WorkpermitSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'All Permits';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="workpermit-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Home', ['/site/index'], ['class' => 'btn btn-success']) ?>
<?= Html::a('Refresh', ['/workpermit/workpermit/awsup'], ['class' => 'btn btn-primary']) ?>
</p>
<?php Pjax::begin(['id' => 'allpermitview']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'rowOptions' => function($model){
if($model->wp_type == 'Excavation Permit'){
return ['class' => 'hide'];
}
},
'options' => [
'style'=>'overflow: auto; word-wrap: break-word;'
],
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
[
'class' => 'kartik\grid\ExpandRowColumn',
'value' => function($model, $key, $index, $column){
return GridView::ROW_COLLAPSED;
},
'detail' => function($model, $key, $index, $column){
$model = Workpermit::find()->where(['wp_no' => $model->wp_no])->one();
$searchModel = new WpindexSearch();
$aclfileuploadpaths = Aclpath::find()->select('aclp_path')->where(['aclp_wpno'=>$model->wp_no])->column();
// $searchModel->wp_no = $model->wp_no;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$id = $model->wp_no;
if($model->wp_type == 'Safe Work Permit'){
return Yii::$app->controller->renderPartial('_approvalstatussafe', [
'id' => $id,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
//'from_date' => $from_date, 'to_date'=>$to_date
]);
}
elseif($model->wp_type == 'Hot Work Permit'){
return Yii::$app->controller->renderPartial('_approvalstatushot', [
'id' => $id,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
//'from_date' => $from_date, 'to_date'=>$to_date
]);
}
elseif($model->wp_type == 'Excavation Permit'){
return Yii::$app->controller->renderPartial('_approvalstatusexcav', [
'id' => $id,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
//'from_date' => $from_date, 'to_date'=>$to_date
]);
}
elseif($model->wp_type == 'Confined Space Permit'){
return Yii::$app->controller->renderPartial('_approvalstatusconfined', [
'id' => $id,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
//'from_date' => $from_date, 'to_date'=>$to_date
]);
}
},
],
'wp_no',
[
'label' => 'WP Type',
'attribute' => 'permittyp',
'value' => 'permittype.wpt_short',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Workpermit::find()->asArray()->all(), 'wp_type', 'wp_type'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Type'],
],
[
'label' => 'Status',
//'attribute'=>'status',
'attribute'=>'wp_status',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Workpermit::Find()->asArray()->all(), 'wp_status', 'wp_status'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Permit Status'],
'contentOptions' => function($model, $key, $index, $column){
if($model->wp_status == 'Available'){
return['style' => 'background-color:red'];
}elseif($model->wp_status == 'Approved'){
return['style' => 'background-color:green'];
}elseif($model->wp_status == 'Work in Progress'){
return['style' => 'background-color:yellow'];
}elseif($model->wp_status == 'Completed'){
return['style' => 'background-color:blue'];
}
}
],
'wp_date',
//'wp_time',
'wp_validto',
[
'attribute'=>'wp_plant',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Workpermit::find()->asArray()->all(), 'wp_plant', 'wp_plant'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Plant'],
],
[
'attribute'=>'wp_area',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Workpermit::find()->asArray()->all(), 'wp_area', 'wp_area'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Area'],
],
//'wp_area',
'wp_jobdesc',
[
'label' => 'View',
'content' => function ($model, $key, $index, $column) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['viewall', 'id' => $model->wp_no])
. ' ' .Html::a('<span class="glyphicon glyphicon-print"></span>', ['/workpermit/workpermit/printpermit', 'id' => $model->wp_no]);
}
],
[ 'label' => 'Action',
'content' => function ($model, $key, $index, $column) {
//The Supervisor Conditions//
if ($model->wp_status == 'Assigned' && Yii::$app->user->can('s_jsapproval')) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['updateapproval', 'id' => $model->wp_no]);
}
elseif ($model->wp_type == 'Safe Work Permit' && $model->wp_apoapproval == 'Approved' && $model->wp_spsapproval == 'Approved' && $model->wp_officerapproval == 'Approved' && Yii::$app->user->can('s_jsapproval') && $model->wp_jc != 'Completed' && ($model->wp_status == 'Approved' || $model->wp_status == 'Work in Progress' || $model->wp_status == 'Assigned')) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['updateassignwp', 'id' => $model->wp_no]);
}
elseif ($model->wp_type == 'Hot Work Permit' && $model->wp_apoapproval == 'Approved' && $model->wp_spsapproval == 'Approved' && $model->wp_officerapproval == 'Approved' && $model->wp_emsuper == 'Approved' && $model->wp_plsuper == 'Approved' && Yii::$app->user->can('s_assignwp') && ($model->wp_status == 'Approved' || $model->wp_status == 'Work in Progress')) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['updateassignwp', 'id' => $model->wp_no]);
}
elseif ($model->wp_type == 'Confined Space Permit' && ($model->wp_status == 'Assigned' || $model->wp_status == 'Work in Progress' || $model->wp_status == 'Approved') && Yii::$app->user->can('s_assignwp')){
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['updateassignwp', 'id' => $model->wp_no]);
}
}
],
],
]); ?>
<?php Pjax::end(); ?>
</div>
<?php
/* start getting the another permit Type */
$script = <<< JS
$('#workpermit-wp_spost13').on('click',function(e){
$.pjax.reload({container: "#allpermitview"});
});
JS;
$this->registerJs($script);
/* end getting the another permit Type */
?>

A couple of options for you:
Add a Where Condition to you Search Query
Note: You could also apply optional filters in your search model.
class SomeController extends Controller
{
public function actionIndex()
{
$this->layout = "#app/views/layouts/main-app";
$searchModel = new WorkpermitSearch ();
$queryParams = Yii::$app->request->queryParams;
$dataProvider = $searchModel->search($queryParams);
$dataProvider->query->andWhere(['or',
['wp_type' => 'Safe Work Permit')],
['wp_type'=> 'Hot Work Permit'],
])
->orderBy(['wp_type' => SORT_DESC ]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Apply a Hide Class in Gridview at rowOptions
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'emptyCell'=>'-',
//'formatter' => ['class' => 'yii\i18n\Formatter', 'nullDisplay' => ''], //Hide "not set" fields.
//'tableOptions' => ['class' => 'table'],
//'options' => ['style' => 'font-size: 1.2em;'],
'rowOptions' => function($model, $key, $index, $grid){
if ($model->wp_type == 'Safe Work Permit'){
return ['style' => 'visibility:collapse;'];
} else {
return ['style' => 'visibility:visible;'];
}
},
...

Related

Yii2 Gridfilter date range picker

I have developed application in yii2 and for filter in grid view i had used Kartik date-range picker extension.
I have faced following problem.
When i select different date for start and end date then it will worked.
e.g start date 5 and end date 10
When i select next day same date for start and end date then it will worked.
e.g start date 6 and end date 6
When i select today's date for start and end date then it will not worked.
e.g start date 5 and end date 5
Sample code.
<?php
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn'
],
[
'label' => Yii::t('app', "Created At"),
'attribute' => 'created_at',
'value' => function ($model) {
if (extension_loaded('intl')) {
return $model->created_at;
} else {
return $model->created_at;
}
},
'filter' => DateRangePicker::widget(
[
'model' => $searchModel,
'attribute' => 'created_at',
'convertFormat' => true,
'presetDropdown' => false,
'pluginOptions' => [
'locale' => [
'format' => 'Y-m-d'
]
]
]
)
],
[
'attribute' => 'branch_id',
'value' => 'branch.name',
'filter' => Html::activeDropDownList(
$searchModel,
'branch_id',
ArrayHelper::map(Branch::find()->asArray()->all(), 'id', 'name'),
['class' => 'form-control', 'prompt' => 'Select Branch']
)
],
[
'attribute' => 'user_id',
'value' => 'user.firstname',
'filter' => Html::activeDropDownList(
$searchModel,
'user_id',
ArrayHelper::map(User::find()->asArray()->all(), 'id', 'firstname'),
['class' => 'form-control', 'prompt' => 'Select User']
)
],
[
'attribute' => 'order_status',
'format' => 'html',
'filter' => ["0" => "Completed", "1" => "Refund", "2" => "Cancel"],
'value' => function ($model) {
if ($model->order_status == '1') {
return '<span class="label bg-red">Refund</span>';
} else if ($model->order_status == '2') {
return '<span class="label bg-yellow">Cancel</span>';
} else {
return '<span class="label bg-blue">Completed</span>';
}
}
],
[
'attribute' => 'created_from',
'format' => 'html',
'value' => function ($model) {
if ($model->created_from == '1') {
return '<span class="">WEB<small>';
} else if ($model->created_from == '2') {
return '<span class="">IOS<small>';
} else if ($model->created_from == '3') { //MARKAS
return '<span class="">ANDROID<small>';
}
}
],
[
'attribute' => 'pax',
'format' => 'html',
'value' => function ($model) {
$setting = common\constants\EstablishmentConfiguration::activeSetting();
$type = '';
$paxFlag = '0';
if (!empty($setting)) {
$type = $setting->establishment_type;
$paxFlag = $setting->enable_pax;
}
if ($type == 'restaurant' && $paxFlag == '1') {
return $model->pax;
} else {
return '--';
}
}
],
//'pax',
'sub_total',
'discount',
'tax',
'grand_total',
[
'class' => 'yii\grid\ActionColumn',
'header' => Yii::t('app', 'Action'),
'template' => '{view} {movetofack}',
'contentOptions' => ['style' => 'width:10%'],
'buttons' => [
'view' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-eye-open"></span>',
['view', 'id' => $model->id],
[
'class' => 'btn btn-xs btn-warning',
'title' => 'View',
'data-skin' => 'skin-warning'
]
);
},
'movetofack' => function ($url, $model) {
return Html::a(
'<span class=" fa fa-trash"></span>',
[
'movettotrash', 'id' => $model->id
],
[
'class' => 'btn btn-xs btn-warning',
'title' => 'Fack Record',
'data-skin' => 'skin-warning',
'data' => [
'confirm' => Yii::t('app/msg', 'Are you sure this is fake record? would you like to move to trash this order?'),
'method' => 'post'
]
]
);
}
]
]
];

Toggle user confirmation not working

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']);
}

Yii2: Highlight Row in gridview based on db column value

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'];
}
},

Filter gridview in form in Yii2

I want to use a gridview in a form. The idea is to insert multiple selected rows from the gridview into the table. I've managed to get the gridview in the form. But when I'm filtering it it is running the insert command in stead of filtering.
Also, please let me know if there is any better way to do this.
Code of my _form
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\grid\GridView;
use dosamigos\datepicker\DatePicker;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use frontend\models\Rmtemplate;
/* #var $this yii\web\View */
/* #var $model frontend\models\Rawmaterial */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="rawmaterial-form">
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'usedate')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-9 col-sm-9 col-lg-9">
<?= $form->field($model, 'productname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Rmtemplate::find()->select('productname')->distinct()->orderBy(['productname' => SORT_ASC,])->all(),'productname','productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Charge...', 'id' => 'productid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-xs-12 col-sm-12 col-lg-12">
<?= GridView::widget([
'dataProvider' => $dataProvider2,
'filterModel' => $searchModel2,
'columns' => [
['class' => 'kartik\grid\CheckboxColumn'],
//'id',
//'productname',
[
'attribute'=>'productname',
'filterType'=>GridView::FILTER_SELECT2,
'filter'=>ArrayHelper::map(Rmtemplate::find()->orderBy(['productname' => SORT_ASC])->asArray()->all(), 'productname', 'productname'),
'filterWidgetOptions'=>[
'pluginOptions'=>['allowClear'=>true],
],
'filterInputOptions'=>['placeholder'=>'Charge Name'],
],
'rmname',
'qty',
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
</div>
<?= $form->field($model, 'rmname')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'useqty')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'unitcost')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'chargenumber')->textInput()->hiddenInput()->label(false) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
/* start getting the chargeno */
$script = <<<EOD
$(window).load(function(){
$.get('index.php?r=rmprod/rawmaterial/get-for-chargeno',{ orderid : 1 }, function(data){
//alert(data);
var data = $.parseJSON(data);
$('#rawmaterial-chargenumber').attr('value',data.chargeno);
}
);
});
EOD;
$this->registerJs($script);
/*end getting the chargeno */
?>
Code of Controller Action
public function actionCreate()
{
$model = new Rawmaterial();
$searchModel2 = new RmtemplateSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
Code of create.php
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model frontend\models\Rawmaterial */
$this->title = 'New Charge';
$this->params['breadcrumbs'][] = ['label' => 'Rawmaterials', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="rawmaterial-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]) ?>
</div>
The error -
Update:
On ScaiseEdge's answer I've updated the _form and the Controller action looks like below.
public function actionCreate()
{
$model = new Rawmaterial();
$searchModel2 = new RmtemplateSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
if ($_POST('submit') == 'create_update' ) {
// then perform the insert
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
} else {
// no insert but render for filter ..
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
Error:
Update 2
public function actionCreate()
{
$model = new Rawmaterial();
$searchModel2 = new RmtemplateSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
if (!$request->isAjax) {
if ($_POST('submit') == 'create_update' ) {
// then perform the insert
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
} else {
// no insert but render for filter ..
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
Error
Update 3
public function actionCreate()
{
$model = new Rawmaterial();
$searchModel2 = new RmtemplateSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
if (isset($_POST('submit'))) {
if ($_POST('submit') == 'create_update' ) {
// then perform the insert
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
} else {
// no insert but render for filter ..
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
Error
Update 4
public function actionCreate()
{
$model = new Rawmaterial();
$searchModel2 = new RmtemplateSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
if (isset($_POST['submit'])) {
if ($_POST('submit') == 'create_update' ) {
// then perform the insert
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
} else {
// no insert but render for filter ..
return $this->render('create', [
'model' => $model,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
}
}
This could be related to the fact the filter value is submitted for perform the search.
You could override this behavior adding a value to the submit button
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update',
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
'name' => 'submit', 'value' => 'create_update']) ?>
and in the controller/action (insert and update) check the content of the submit button
if (isset($_POST['submit'])) {
if ($_POST['submit'] == 'create_update' ) {
// then perform the insert
} else {
// no insert but render for filter ..
}

Kartik export widget not export search result Yii2

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();