yii2 Pjax and GridView (pagination and post request) - yii2

As I understood when I click on the button "Confirm" pjax reload widget with new data,and started with firs paging page.
1)Is there any way to load new data but stay at current paging page?
2)Is there any way to sent 'id' parameter by POST method?
Controller
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Orders::find()->with('orderServices.services','orderEmployees','user'),
'pagination' =>['pageSize' => 3],
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
In View
<?php \yii\widgets\Pjax::begin(); ?>
<?= GridView::widget([
// ... configuration here
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete} {approve}'
,
'buttons' => [
'approve' => function ($url,$model,$key) {
if($model->status=='not confirm')
{
return Html::a('Confirm',Url::toRoute(['controller/action', 'id' => $model->id]));/// має бути Ajax
}
},
],
]
]);
<?php \yii\widgets\Pjax::end(); ?>

I don't if this could be useful for you but with :
Yii::$app->request->get('page')
You can obtain the current GET page in this way and use for your need

Related

How to change date format in filter search label after filtering the data in yii2?

I am working in yii2 framework, I want to display date format mm/dd/yyyy,but in my database format was yyyy-mm-dd. In model-search I converted the format mm/dd/yy and I am getting result. After in felter search label it showing as yyyy-mm-dd,I want display mm/dd/yyyy format.Please give suggestion.
UserSearch model
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->andFilterWhere(['like', 'start_date', trim($this->start_date)])
return $dataProvider;
index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'tableOptions' => ['class' => 'table table-bordered table-hover '],
'headerRowOptions' => [
'class' => 'thead-light',
],
'rowOptions' =>function($model){
if($model->status == 0){
return ['class' =>'inactive-border-color',];
}
},
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'start_date',
'format' => 'date',
'value' => 'termination_date',
'filterInputOptions' => [
class' => 'form-control',
'placeholder' => 'MM/DD/YYYY'
],
],
]
]); ?>
I want display mm/dd/yyyy format in filter search .
After you use your UserSearch model to create data provider, the value in its $start_date is not used for filtering anymore so you can change it to the format you need for output. You simply need to add following code in your controller's action anywhere between creating the data provider and rendering the view.
$searchModel->start_date = \Yii::$app->formatter->asDate($searchModel->start_date);
The other option is that instead of converting the value in $start_date property you will only convert it when creating data provider like this:
public function createDataProvider()
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->andFilterWhere(['like', 'start_date', $this->normalizeDate($this->start_date)]);
return $dataProvider;
}
protected function normalizeDate($date)
{
//your code to convert date
return $convertedDate;
}

(Yii2) how to pass value from table to an HTML button

I'm new to Yii2 and currently I'm working on the admin section in which admin could update a typical webpage, in this case, the "About us" page. The application will check if there's already an existing row in the "About" table. If so, an "Update" button will be shown right in the view's index page as only one content is allowed in the "About us" section. Else, a "create" button will be shown. The update in the gridview works well but I wonder how to pass the "id" value in the table into the update button because I keep getting the error message "missing required parameters id". I've spent a whole day but stil stuck. Thank you in advance for your kind help.
This is how my Index looks like :
<?php
if(!$dataProvider)
{
echo Html::a('Create', ['create'], ['class' => 'btn btn-success']) ;
}
else
{
echo Html::a('Update-about', ['update'], ['class' => 'btn btn-success']);
}
?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'Tite',
'Content:ntext',
'Date',
'ImageID',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
And this is the controller:
public function actionIndex()
{
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->ID]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
Link should be
Html::a('Update-about', ['update', 'id' => $id], ['class' => 'btn btn-success']);
And you should pass ID of the row to index view:
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $id,
]);
So the main question is - what row of table About you want to change by update action? If this is the first available record then:
public function actionIndex()
{
$searchModel = new aboutSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$about = About::find()->one(); // get first record found
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'id' => $about->id, // pass record id to view
]);
}
Or find the row you need by passing some condition to where method: About::find()->where(...)->one();

Yii2 renderPartial view in Layout

I'm trying to render a view to my layout on yii2 but I get an error
PHP Notice – yii\base\ErrorException
Undefined variable: dataProvider
I create a controller category with function
public function Wdgcategory()
{
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
return $this->renderPartial('wdgCategory', [
'dataProvider' => $dataProvider,
]);
}
And in my view I create a view file in category/wdgCategory.php
<?php
use yii\helpers\Html;
use yii\widgets\ListView;
?>
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemOptions' => ['class' => 'item'],
'itemView' => function ($model, $key, $index, $widget) {
return '<li>'
.Html::a(Html::encode($model->category_name),
['view', 'id' => $model->category_id])
.'<li>';
},
]) ?>
and in layout I add this code
<ul>
<?= $this->render('/category/wdgCategory') ?>
</ul>
but I get error : Undefined variable: dataProvider
You have defined Wdgcategory() method but you're never using it. You're just trying to render view from layout without providing necessary data ($dataProvider).
In your case you should probably create widget for this list:
class CategoriesWidget extends \yii\base\Widget {
public function run() {
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
return $this->render('categories', [
'dataProvider' => $dataProvider,
]);
}
}
And use it in your layout:
<?= CategoriesWidget::widget() ?>

yii2-scroll-pager extension how apply in yii2

yii2-scroll-pager extension used but that extension not work.
Extesion Link
View File -1: index.php
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'pager' => ['class' => \kop\y2sp\ScrollPager::className()]] );
View File-2 : _item
<?php $model->name;?><?php $model->age;?><?php $model->mobile;?>
Controller File : SiteController.php
$dataProvider = new ActiveDataProvider([
'query' => backend\models\Product::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
where is wrong in that code please hep guys
add this to your pager config
'triggerOffset'=>5
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'triggerOffset'=>5
]

yii2 kartik export menu - No results found

Using karktik export menu only. Why do the following exports an excel file with no results found.. I am sure that there are records inside the TblDv model. But in the excel it says no records found.
-in the controller
public function actionExport(){
$provider = new ActiveDataProvider([
'query' => TblDv::find(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('export', [
'dataProvider' => $provider,
]);
}
-the view
<?php
use kartik\export\ExportMenu;
use kartik\grid\GridView;
use kartik\helpers\Html;
$gridColumns = [
'id',
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'fontAwesome' => true,
]);
?>
I dont know why but it worked when I changed the gridcolumns to this..
$gridColumns=[
['class' => 'yii\grid\SerialColumn'],
'id',
];