yii2 Pjax + java script prompt - yii2

It there any way to make 'data-confirm' => Please enter the number' not just confirm but some like JS prompt and get imputed values to sent it to controller?
<?php \yii\widgets\Pjax::begin(['id' => 'pjax-orders_table','clientOptions' => ['method' => 'POST'], 'enablePushState'=>false]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'id'=>'orders_table',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
///some columns
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}{some}',
'buttons' => [
'some' => function ($url,$model,$key) {
if($model->status=='not confirm')
{
return Html::a('<span class="glyphicon glyphicon-trash"</span>',['my/some', 'id' => $model->id],[
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Please enter the number',
'data-method' => 'post',
]);
}
},
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end() ?>
In controller
public actionSome()
{ $dataProvider = new ActiveDataProvider();
$dataProvider->query = Orders::find()->all();
return $this->render('some',['dataProvider'=>$dataProvider]);
}

instead of Html::a() use Html::button()where button id = some_item_id and then write this JS code
$('.buttons_class').click(function(){
var selection;
do{
selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}
while(isNaN(selection));
if(!isNaN(selection))
{
$.post("some",{id:45,prompt_value:selection}, function(response)
{
console.log(response);
$.pjax.reload({container:'#pjax-orders_table'});
});
}
})

Related

Yii2 multiple ListView in one page

I'm using 2 widgets in one page, Both are using listview, one is for blogs and another one is for pages and both are using ScrollPager. I want to show more button in each separately. The problem is the show more button is shown only for pages but not for blog, if I remove the page widget it will display for blogs. I'm trying to display show more for pages and show more for blogs. I tried by pageParam but the problem still exist.
widget:
class UserPagesWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderContent = new ActiveDataProvider([
'query' => Post::find()->Where(['user_id' => $this->usr_id])
->orderBy(['post_crdate' => SORT_DESC]),
'pagination' => [
'pageParam' => 'contentPagination',
'pageSize' => 5,
],
]);
return $this->render('/user/widget/pstList', [
'dataProviderContent' => $dataProviderContent,
]);
}
}
Render file:
<?= ListView::widget([
'dataProvider' => $dataProviderContent,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Content',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-pst-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Blog Widget:
class UserBlogsWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderBlog = new ActiveDataProvider([
'query' => Blog::find()->Where(['user_id' => $this->usr_id])
->orderBy(['blog_update' => SORT_DESC]),
'pagination' => [
'pageParam' => 'blogPagination',
'pageSize' => 6,
],
]);
return $this->render('/user/widget/blgList', [
'dataProviderBlog' => $dataProviderBlog,
]);
}
}
Blog render file:
<?= ListView::widget([
'dataProvider' => $dataProviderBlog,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Blogs',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-blg-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Both widgets are called in one view file.
<div class="row chn-row">
<div class="col-sm-12" style="padding:15px">
<!-- separing -->
<?= UserPagesWidget::widget(['usr_id' => $model->user_id]) ?>
<!-- separing -->
</div>
<div class="col-sm-12" style="padding:15px">
<?= UserBlogsWidget::widget(['usr_id' => $model->user_id]) ?>
</div>
</div>
The problem is the show more button is displayed only for pages.
You need to adjust selectors to distinguish between two widgets. By default ScrollPager uses some generic selector (.list-view) that will match all list views. You should explicitly set ID for ListView widgets and use it in ScrollPager config as selector for widget initialization.
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderBlog,
'options' => ['id' => 'blog-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#blog-list-view',
'paginationSelector' => '#blog-list-view .pagination',
],
]) ?>
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderContent,
'options' => ['id' => 'content-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#content-list-view',
'paginationSelector' => '#content-list-view .pagination',
],
]) ?>
See https://kop.github.io/yii2-scroll-pager/#general-options

Two gridview from two model search by single field in yii2

I have one search form with a field and a button. On the button click I want to search 2 searchmodel - sellitembtdtSearch and puritembtdtSearch. The result will be shown in one view. I can display the view without any problem. The problem is when I'm searching only one searchmodel is getting searched. Please let me know how can I search both the searchModel at the sametime.
First I land on index2.php page where the form is.
'action' => ['/stock/sellitem/printproductledger3',],
'method' => 'get',
<?= $form->field($model, 'productname')->textInput(['maxlength' => true,]) ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
actionIndex2.php
public function actionIndex2()
{
$searchModel1 = new SellitemsbdtSearch();
$dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams);
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
return $this->render('_formbtdt', [
'model' => $searchModel2,
//'searchModel1' => $searchModel2,
]);
}
public function actionPrintproductledger3() {
$searchModel1 = new SellitemsbdtSearch();
$dataProvider1 = $searchModel1->search(Yii::$app->request->get());
$searchModel2 = new PuritemsbdtSearch();
$dataProvider2 = $searchModel2->search(Yii::$app->request->get());
//$productname = yii::$app->request->get('productname');
//$prodesc = yii::$app->request->get('prodesc');
$content = $this->renderPartial('_printproductledgerbtdt', [
//'productname' => $productname,
'searchModel1' => $searchModel1,
'dataProvider1' => $dataProvider1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
//'prodesc' => $prodesc,
]);
return $this->render('_printproductledgerbtdt', [
'dataProvider1' => $dataProvider1,
'searchModel1' => $searchModel1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
]);
This code only searchs puritemdtdtSearch. I want to search puritembtdtSearch and sellitembtdtSearch at the same time. Thanks.
The issue is you are using searchModel2 in your form so you are just getting the result of searchModel2. And not getting the result of searchModel1.
What you can do is send searchModel1 to your _search file.
<?php echo $this->render('_search', ['model1' => $searchModel1, 'model2' => $searchModel2]); ?>
Now in your form use a hidden input.
<?php echo $form->field($model1, 'productName')->textInput(['id' => 'model1']); ?>
<?php echo $form->field($model2, 'prodcutName')->hiddenInput(array('id' => 'model2')); ?>
Now its that we have two model fields we need to populate the hidden field, this can be done using jquery.
<?php $this->registerJs('
//add the .search class name for your search button
jQuery("body").on("click", ".search", function() {
alert("Hello");
var a = $("#model1").val();
$("#model2").attr("value", a);
});
');?>
Try this..tested completely and works fine!!

Set enablePushState = false on specific urls inside Pjax container (Yii2)

I need pjax to work on change status click and it's working fine but don't want it to change the URL as well. Below is the code:
<?php Pjax::begin(['id'=>'pjax-container-agency-index', 'timeout' => 10000, 'enablePushState' => true]); ?>
<?= GridView::widget([...,
[
'label' => 'Status',
'format' => 'raw',
'value' => function ($data) {
if ($data->deactive == 0) {
return Html::a(FA::i('circle'), ['agency/change-status', 'id' => $data->member_id, 'set' => 1], ['onclick' => "return confirm('Deactive this state/site?');", 'class' => 'status-inactive']);
} else {
return Html::a(FA::i('circle'), ['agency/change-status', 'id' => $data->member_id, 'set' => 0], ['onclick' => "return confirm('Active this state/site?');", 'class' => 'status-active']);
}
},
],
]);
<?php Pjax::end(); ?>
actionChangeStatus() is as below:
public function actionChangeStatus($id, $set) {
if (!empty($id) && isset($set)) {
$localGovt = $this->findModel($id);
$localGovt->deactive = $set;
if ($localGovt->save()) {
Yii::$app->getSession()->setFlash('success-status', 'State Status Changed');
} else {
Yii::$app->getSession()->setFlash('error-status', 'There is some error. Please consult with Admin.');
}
$searchModel = new AgencySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
}
Note: I need 'enablePushState' => true for other events, so I can't change it to false inside Pjax::begin
I believe it's
'enableReplaceState' => true
Pass this attribute to Pjax:
Pjax::begin(['enablePushState' => false]);
This worked for me.

How to use Pjax with a Custom Button in Gridview

This is my gridview
<?php Pjax::begin(['id' => 'vouchers']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
],
]); ?><?php Pjax::end() ?>
and this is my cancel_voucher action
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
$this->redirect( Yii::$app->request->referrer );
}
How can i update the grid view using pjax when i click on the Cancel Button?
I tried giving button an id and then on the onClick event of the button i tried to update the gridview using $.pjax.reload({container:"#vouchers"}); But it didn't work
EDIT
This is how i tried
<?php
\yii\widgets\Pjax::begin([
'id' => 'countries',
'timeout' => false,
// 'enablePushState' => false,
'clientOptions' => ['method' => 'POST']])
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger','id' =>'cancel_voucher',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
<?php
$this->registerJs(
'jQuery(document).ready(function($){
$(document).ready(function () {
$("#cancel_voucher").on("click", function(){
alert("hai");
$.pjax.reload({container:"#countries"});
});
});
});'
);
?>
Issue
You have to return content, which will be showing in Pajax block instead of $this->redirect( Yii::$app->request->referrer );
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
if (Yii::$app->getRequest()->getIsPjax()) {
//render part of view with grid
return $this->renderAjax($url, $params);
}
else { $this->redirect( Yii::$app->request->referrer ); }
}
For correct pajax redirect you have to setup X-Pjax-Url header.
if (Yii::$app->getRequest()->getIsPjax()) {
$response = Yii::$app->getResponse();
$destination = Url::to($url);
$response->getHeaders()->set('X-Pjax-Url', $destination);
$response->getHeaders()->set('Location', $destination);
return null;
}

How to access variable from other view page on yii2?

How to access variable ($imodel->grade) in tampil.php page and then subsequently put on the page view.php with post method. Sorry for the question, i'm newbie in OOP, MVC and Yii. Please help me guys
Here is The code of actionIndex:
public function actionIndex()
{
$imodel = new Grade();
if($imodel->load(Yii::$app->request->post()))
{
$imodel->load(Yii::$app->request->post());
$imodel->grade;
$query = Nilaiharian::find(); //select nis, $grade, nama from nilaiharian
$dataProvider = new ActiveDataProvider(['query' => $query,]);
return $this->render('tampil', ['imodel' => $imodel, 'dataProvider' => $dataProvider,] );
}
else
{
return $this->render('index', ['imodel' => $imodel, ] );
}
}
And this is a code of tampil.php
<?php
echo "Nilai untuk ".$imodel->grade;
echo "<br>";
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'nis',
$imodel->grade,
'nama',
['class' => 'yii\grid\ActionColumn',
'template' => '{view}{update}{delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-eye-open"></i>',
$url, ['title' => Yii::t('app', 'Show'), 'name'=>'view', 'value'=>'view', 'style' => 'margin-right:10px;', ]);
},
'update' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-pencil"></i>',
$url, ['title' => Yii::t('app', 'Edit'), 'name'=>'update', 'style' => 'margin-right:10px;', ]);
},
'delete' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-trash"></i>',
$url, ['title' => Yii::t('app', 'Delete'), 'name'=>'delete', 'data-confirm'=>'Are you sure you want to delete this record?']);
}
]
],
],
]);
?>