How to change Grid View using Pjax in yii2? - php

Ok, ill explain what i have first,
--A database table called cases.
This holds all of the cases to i need to display in a gridview
--Three tables called Category, Subcategory and ChildCategory
The cases from table case will be linked to a childcategory.
So i have made three DropDownLists that are populated from the individual category tables in the database. For example:
_categories.php
yii\widgets\Pjax::begin(['id' => 'categories']);
$form = ActiveForm::begin();
echo $form->field($searchModel, 'category')
->dropDownList(
ArrayHelper::map($allCategory, 'id', 'name'),
[
'onchange'=>'getSubcategory()',
]
);
//To stop errors, if first category not chosen make subcategory and empty drop down.
$subcategory = array(
"empty" => ""
);
echo $form->field($searchModel, 'subcategory')
->dropDownList(
ArrayHelper::map($subcategory, 'id', 'name'),
[
'onchange'=>'getChildcategory()',
]
);
//To stop errors, if second category not chosen make childcategory and empty drop down.
$childcategory = array(
"empty" => ""
);
echo $form->field($searchModel, 'childcategory')
->dropDownList(
ArrayHelper::map($childcategory, 'id', 'name'),
[
//'onchange'=>'getChildCategory()',
'onchange'=>'submitNow()',
]
);
ActiveForm::end();
yii\widgets\Pjax::end();
So what happens is when the first category is selected it runs "onchange" => getSubcategory. This will basically send an Ajax request to my controller with the value of the selected option. It will then pull back the subcategories where the subcategory_id = the value of the selected option. It then populated the subcategory drop down with this information.
this function is on _categories.php with the category drop downlists above
function getSubcategory(){
//#casesearch-category is the first drop down list
var firstcategory = $('#casesearch-category').val();
var childcategory = document.getElementById('casesearch-childcategory');
childcategory.options.length = 0;
$.ajax({
url: '<?php echo \Yii::$app->getUrlManager()->createUrl('cases/subcategories') ?>',
type: 'POST',
dataType: 'json',
data: {
firstcategory: firstcategory
},
success: function(data) {
var subcategory = document.getElementById('casesearch-subcategory');
//if select is changed again, make the options length 0 so that it gets rid of previous appends.
subcategory.options.length = 0;
for(var i=0; i<data.length; i++){
subcategory.options[i] = new Option (data[i].name);
subcategory.options[i].value = data[i].subcategory_id;
}
subcategory.options.selectedIndex = -1;
if(subcategory.options.length === 1){
getChildcategory();
}
}
});
}
So when this ajax request reaches my controller it does this:
CasesController.php
public function actionSubcategories()
{
if(isset($_POST['firstcategory'])){
$firstcategory = $_POST['firstcategory'];
// SELECT * FROM `subcategory` WHERE `parent_id` = $firstcategory
$subcategory = Subcategory::findSubcategory($firstcategory);
}
return \yii\helpers\Json::encode($subcategory);
}
Okay so that was just a little bit to help you understand the category side of things. Now i have a gridview that is populated from the database when the page is submitted. However as i have done ajax to get my categories i need the gridview to change with pjax when ever the categories are changed.
So in my controller actionIndex is sends through the searchModel and dataprovider for the gridview like so:
CasesController.php
public function actionIndex()
{
$model = new Cases;
$searchModel = new CaseSearch();
$allCategory = Category::find()->all();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'allCategory' => $allCategory,
'model' => $model
]);
}
Then on my index page where it displays the grid view is here::
NOTE::Index.php renders the category dropdownlists seen above _categories.php
<?= $this->render('_categories', [
'model' => $model,
'searchModel' => $searchModel,
'allCategory' => $allCategory
]) ?>
<?php Pjax::begin(['id' => 'cases']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
Ok So here is the part where i am stuck! I assume what i am meant to do is somehow update the searchModel and dataProvider for the gridview but i am unsure how to do this. As if i send an ajax request to controller to change it it will then have to render the page again which defeats the objective.
at the top of _categories.php
function submitNow(){
$.pjax.reload({container:"#cases"}); //Reload GridView
}
This function is called when the last childcategory is selected.I know something will have to happen here to do this but i do not know what.
Can anyone help?

<?php use yii\widgets\Pjax; ?>
<?php Pjax::begin() ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
'neutral_citation',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>

Related

Pjax reloads page on gridview search on Yii2

I'm loading a gridview inside a page with an ajax request.
Then, after the page is loaded, i want to let the user order and search as usual with gridview, obviously not reloading the page.
While the sorting works, the search reloads the page (and since the action loading the ajax content is different from the current one the page changes entirely). I know Pjax reloads the entire page after the timeout value, but that is not the problem as i changed to a really high value and i still get the reload.
Also, that is the only pjax on the page.
What could be the problem?
This is the code for the view with the gridview
<?php Pjax::begin([
"id" => "associates-ajax-list",
"enablePushState" => FALSE,
"enableReplaceState" => FALSE,
"timeout" => 5000,
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'surname',
],
]); ?>
<?php Pjax::end(); ?>
</div>
This is the code for the ajax action
public function actionAssociatesList($id) {
$searchModel = new \app\models\AssociateSearch();
$searchModel->associates_for = $id;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->isAjax) {
return $this->renderAjax('associates_list', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} else {
return $this->render('associates_list', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
The page where this content is loaded is a standard view page generated with Gii
Can you try with adding this script in the view?
<?php
$this->registerJs(
'$("document").ready(function(){
$("#associates-ajax-list").on("pjax:end", function() {
$.pjax.reload({container:"#associates-ajax-list"}); //Reload GridView
});
});'
);
?>
You might need to alter some selectors since I don't know how the HTML looks like in your project.
AND you might need some JS to prevent the default submit action if the search fields are in a <form>. If not, the form will submit before the PJAX can trigger.
The problem was the gridview.
You need to specify an id.
The funny thing is that this gridview was the only one generated for this page, so apparently there was no conflict.
<?= GridView::widget([
'id' => "grid-view-name",
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,

Q: Yii2 load data from database

I have problems with loading data from database.
My goal is to implement a MVC which dynamically shows in the view the values stored in the db table. So, in the Project model I have this code:
public function search($params)
{
$query = project::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'position' => $this->position,
'created_at' => $this->created_at,
'is_deleted' => $this->is_deleted,
]);
$query->andFilterWhere(['ilike', 'title', $this->title])
->andFilterWhere(['ilike', 'description', $this->description])
->andFilterWhere(['ilike', 'link', $this->link])
->andFilterWhere(['ilike', 'image', $this->image]);
return $dataProvider;
}
In the Controller:
public function actionIndex()
{
$searchModel = new SeachProject();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
And in the view (index.php):
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title',
'description',
'image',
],
]); ?>
Well, it works but I need to print the values into Uikit cards and not through Gridview limitations (which is actually the only way I know). I should have one card for each record in the table with an image (the url in the image field of the table) and a link (the url in the link field of the table).
Any words of advise would be good, thanks in advance!
You can use ListView and create any kind of structure using UiKit cards or material design.
The ListView widget is used to display data from a data provider. Each
data model is rendered using the specified view file. Since it
provides features such as pagination, sorting and filtering out of the
box, it is handy both to display information to end user and to create
data managing UI.
A typical usage is as follows:
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_ui-card',
'viewParams' => [
'fullView' => true,
'context' => 'main-page',
],
]);
The _ui-card view file could contain the following basic card html :
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="uk-card">
<div class="uk-card-header">
<h3 class="uk-card-title"><?= Html::encode ( $model->title ) ?></h3>
</div>
<div class="uk-card-body"><img src="<?= Html::encode ( $model->image ) ?>"><?= Html::encode ( $model->link ) ?></div>
<div class="uk-card-footer"></div>
</div>
This way you can show every project as a separate card using the view file.
Hope this helps

How to add grand total to the bottom of yii2 grid view

I have a grid view which is like this. The columns are calculated with the help of MySQL query and displayed accordingly.
<?php
$subquery = Adanalytics::find()->
select('id,ad_id,date_event,max(cpc) cpclick,max(cpv) cpview,max(impression) impression,max(view) view,max(clicks) clicks,visitor_ip,publisher_id')->
from('ad_analytics')->
where(['publisher_id' => Yii::$app->user->identity->id ])->
groupBy('ad_id,date_event,visitor_ip');
$query=Adanalytics::find()->
select('ad_id,date_event,sum(cpclick) total_click_cost,sum(cpview) total_view_cost,sum(impression) total_impression,sum(view) total_views,sum(clicks) total_clicks,publisher_id')->
from(['t'=>$subquery])->
groupBy('t.ad_id,t.date_event');
?>
<?= GridView::widget([
'dataProvider'=>new ActiveDataProvider([
'query' => $query,
]),
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'ad_id',
'total_impression',
'total_views',
'total_clicks',
'total_click_cost',
'total_view_cost',
'date_event',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I want to show a grand total column at the bottom of this grid view.
For example
'grand_total_impression',
'grand_total_views', //This is the sum of all displayed views
'grand_total_clicks',//This is the sum of all displayed clicks
'grand_total_click_cost',//THis is the sum of all displayed cost
'grand_total_view_cost',//This is the sum of all displayed view cost
To do that I have code in my controller which is like this.
public function actionIndex()
{
$searchModel = new Adanalytics2Search();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$grandTotal = [];
foreach ($dataProvider->getModels() as $value) {
// var_dump($value);
// exit();
$grandTotal['total_click_cost'] += $value['total_click_cost'];
$grandTotal['total_view_cost'] += $value['total_view_cost'];
}
//var_dump($dataProvider);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'grandTotal' => $grandTotal,
]);
}
But it is producing the error in at this way.
Undefined index: total_click_cost
Where am I going wrong? Or is there any other way to solve this problem?
I strongly advise you to use katrik gridview. It handles page summary much better and implements a lot of other usages. Alternatively add 'showPageSummary' => true at your gridview to display columns summary.
use kartik\grid\GridView;
// Create a panel layout for your GridView widget
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
Similar question: Yii2: Kartik Gridview sum of a column in footer
This has solved the problem anyway.
This in grid column.
[
'attribute'=>'total_impression',
'pageSummary' => true
],
And in grid view
'showPageSummary' => true,

Yii2 : How to pass additional parameters to listview

I read this post and answer give by Songwut K. in this question:
Yii2 ListView and dataprovider
But i want to know that is possible to use second model in _item. Suppose the _item is a post in the forum that retrieves data from the $model but I'd like to use a different model like $comment for me comment on a this post and view the post together with a commentary as one _item. Imagie that item is post on facebook and it display only text, date and user which write this post. But how i can add comment to this from other model? I just want to pass my $comment to _item view.
I was tried add new Commnet in my controller:
public function actionIndex()
{
$model = new NewsForm();
$searchModel = new NewsSearch();
$comment= new UrComment();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->setSort(['defaultOrder' => ['Id'=>SORT_DESC]]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->saveNews();
return $this->redirect(['/content/news']);
} else {
return $this->render('index', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'comment' => $comment
]);
}
}
And render this to index. But i can use my $comment only in index how can i pass this to _item? i tried this:
<?php
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'summary'=>'',
'comment' => $comment
]); ?>
And tried in my _item:
<?= $model->getStatus($model->cnNewsContentType_Id); ?> <br>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($comment, 'Text')->textInput(['maxlength' => true])->label('Treść') ?>
<?php ActiveForm::end(); ?>
But have error:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\widgets\ListView::comment
In _item i can only use $model. It is possible to pass my $comment to _item view? Pls help me
You should simply use viewParams :
Additional parameters to be passed to $itemView when it is being rendered. This property is used only when $itemView is a string representing a view name.
e.g. :
<?= ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
'viewParams' => ['comment' => $comment],
'summary'=>'',
]); ?>

Yii2 ListView and dataprovider

What data must be sended to dataprovider?
In my controller:
public function actionIndex() {
$searchModel = new UserSearch();
$dataProvider = $searchModel->search( Yii::$app->request->queryParams );
//other stuff and sending array of params to view
in a view:
echo ListView::widget( [
'dataProvider' => $dataProvider,
] );
but i got only id`s:
And if i`m set single view like:
'itemView' => '_single',
how send data to _single.php ?
I mean - need default template for view list items like in GridView:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'email:email',
'password',
'role',
//....
And then i got perfect grid:
Controller - SiteController.php
<?php
// Yii2 Listview Example : by Songwut Kanchanakosai, Thailand.
use common\models\Members;
use common\models\SearchMembers;
...
public function actionIndex() {
$searchModel = new SearchMembers();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
?>
View (1) - index.php
<?php
use yii\widgets\ListView;
...
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
] ); ?>
View (2) - _item.php
<?php
use yii\helpers\Html;
?>
<?=$model->name;?>
<?=$model->age;?>
<?=$model->mobile;?>
Example Result :
Songwut 36 +668-3949-5153
Prawee 41 +668-7323-2334
Kosol 32 +668-8014-0165
Utehn 39 +668-7874-5643
how send data to _single.php ?
Here is how, use $viewParams
$viewParams public property array $viewParams = []
Additional parameters to be passed to $itemView when it is being
rendered. This property is used only when $itemView is a string
representing a view name.
echo ListView::widget( [
'dataProvider' => $dataProvider,
'viewParams'=>['name'=>'My Name is Stefano'], //acccessed in view as $name with value 'My Name is Stefano'
] );
in official docs http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html#$itemView-detail
$itemView public property
The name of the view for rendering each data item, or a callback (e.g. an anonymous function) for rendering
each data item. If it specifies a view name, the following variables
will be available in the view:
$model: mixed, the data model
$key: mixed, the key value associated with the data item
$index: integer, the zero-based index of the data item in the items array returned by $dataProvider.
$widget: ListView, this widget instance
So your User model data should be available in _single.php as $model->username
So i suppose can use Detail View in _single.php i think:
DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'username',
'email:email',
//....

Categories