Yii2 Gridfilter date range picker - yii2

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

Related

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

Setting empty text in kartik dynagrid

Am using kartik dynagrid and i would like to setup a text that shows when the dataprovider returns empty
The grid
echo DynaGrid::widget([
'columns' => $columns,
'showPersonalize' => true,
'emptyText'=>'Sorry all pr have pritems',///-----------------This is what i had set
'options' => ['id' => 'assignsolic-grid'],
'gridOptions' => [
'options' => ['id' => 'grid'],
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showPageSummary'=>false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
'maxButtonCount' => 10,
],
'panel' => [
'type' => GridView::TYPE_PRIMARY,
// 'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i> </h3>',
'before'=>'<i>Select the Prs to assign solicitation and then click the Assign Solicitation button</i>',
'after' =>
Html::button(' <i class=" glyphicon glyphicon-road "></i> Assign Solicitation ', ['value' => Url::to('assignsolc'),'class' => 'btn btn-danger', 'id' => 'assignsolic']),
'footer' => false
],
'toolbar' => [
['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'],
'{export}',
'{toggleData}'
],
'pjax' => true,
'bordered' => false,
'striped' => true,
'condensed' => true,
'responsive' => true,
'responsiveWrap' => false,
'containerOptions'=>['style'=>'overflow:scroll'],
]
]) ;
This returs an error of Setting unknown property: kartik\dynagrid\DynaGrid::emptyText how can i set the empty text
You can define the value for null display directly in confi/main.php formatter component
'components' => [
.......
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'dd.MM.yyyy',
'decimalSeparator' => ',',
'thousandSeparator' => ' ',
'currencyCode' => 'EUR',
'nullDisplay' => '', // **** this param
],
.....
Otherwise if the widget don't provide a proper attribute you can use an anonymous function for value
[
'attribute' => 'your_attribute',
'value' => function ($model) {
if ( $model->your_attribute == NULL) {
return 'Sorry all pr have pritems';
} else {
return $model->your_attribute;
}
},
],
Since am using $dataProvider i found out that i just have to check if the dataProvider is empty by
if (!$dataProvider->totalCount > 0) { pass in message to display }
else{?>
SHOW THE GRID HERE
<?php
}
?>
?>

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

Put ActiveForm in Gridview Column Yii2

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

Yii2: Select2, How to set initValueText in Gridview or Tabularform?

I need to set initValueText for Select2, which is in loop, like gridview or Tabularform. but I don't know how to set right value for each.
<?= TabularForm::widget([
'dataProvider' => $dataProvider,
'form' => $form,
'actionColumn' => false,
'checkboxColumn' => false,
'attributeDefaults' => [
'type' => TabularForm::INPUT_RAW,
],
'attributes' => [
'test' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => Select2::className(),
'options' => [
'name' => 'test',
'options' => [
'class' => 'test-to-select',
],
'pluginOptions' => [
'allowClear' => true,
'minimumResultsForSearch' => 'Infinity',
'ajax' => [
'url' => Url::to(['/test/get-list']),
'dataType' => 'json',
'data' => new JsExpression('function(term,page) {
return {term : term.term};
}'),
'results' => new JsExpression('function(data,page) {
return {results:data.results};
}'),
'cache' => true
]
],
'initValueText' => 'Selected Text' /// how can I set this in gridview or Tabularform?
],
],
]
]) ?>
Of course this is not working,
'initValueText' => function($model){
retur $model->textValue;
}
Any help would be appreciated.
Into tabularform if you want dynamic initValueText, you can use options closure in this manner:
'test' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => Select2::className(),
'options' => function($model, $key, $index, $widget) {
$initValueText = empty($model['textValue']) ? '' : $model['textValue'];
return [
'name' => 'test',
'options' => [
'class' => 'test-to-select',
],
'initValueText' => $initValueText,
'pluginOptions' => [
'allowClear' => true,
'minimumResultsForSearch' => 'Infinity',
'ajax' => [
'url' => Url::to(['/test/get-list']),
'dataType' => 'json',
'data' => new JsExpression('function(term,page) {
return {term : term.term};
}'),
'results' => new JsExpression('function(data,page) {
return {results:data.results};
}'),
'cache' => true
]
],
];
}
],
If for example attribute for city, so try this..
$cityDesc = empty($model->city) ? '' : City::findOne($model->city)->description;
'initValueText' => $cityDesc, // set the initial display text
For init value first assign value to $model attribute, if you should not assign so this attribute can take value.
Set the data parameter with the array including the option you want to show. For example for cities:
'options' => [
'data' => \yii\helpers\ArrayHelper::map(\app\models\City::find()->orderBy('id')->asArray()->all(), 'id', 'name'),
]
try to put inside the options instead ouside
'widgetClass' => Select2::className(),
'options' => [
'initValueText' => 'Selected Text'
Don't use isset it return error if more the one filter are use there.
[
'attribute' => 'ad_partner_id',
'value' => function ($model, $key, $index, $widget) {
return $model->partner->name;
},
'filter' => Select2::widget([
'model' => $searchModel,
'initValueText' => !empty($searchModel->ad_partner_id) ? $searchModel->partner->name : "",
'attribute' => 'ad_partner_id',
'options' => ['placeholder' => Yii::t('app', 'Search Partner ...')],
'pluginOptions' => ['allowClear' => true, 'autocomplete' => true,
'ajax' => ['url' => Url::base() . '/partner/get-partners',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
]),
],