how to change grid view through Ajax? yii2 - php

First of all i am working with yii2.0 framework.
Right, i have a gridview that pulls data from my database. It currently works and if i use any of the searches it will reload the page with the new data.
However i now have created some dropdownlist categories. Basically there are three tiers of categories , so a main category subcategory and child category. At the moment i have two ajax requests that will populate the sub and child category when the dropdownlist changes. (It populates the categories from my database).
Now i want the gridview to display the cases that are linked to the child categories. So when i choose my first category then choose my second and child category the gridview displays the content that relates to it.
At the moment my controller renders the searchModel + dataProvider for the grid view as seen below::
public function actionIndex()
{
$searchModel = new CaseSearch();
$allCategory = Category::find()->all();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'allCategory' => $allCategory
]);
}
and in my view it displays the data with this::
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'case_id',
'name',
'judgement_date',
'year',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
How should i go about achieving this? should i create something _grid.php which i can then render from my view and send the ajax request there?

The simplest way to achieve this functionality is to wrap the GridView with built-in Pjax widget like so:
use yii\widgets\Pjax;
<?php Pjax::begin(); ?>
// Place GridView code here
<?php Pjax::end(); ?>
From the js you can trigger form submit like that:
$('.grid-view-selector').yiiGridView('applyFilter');
If pjax is attached, content will be replaced dynamically without page reload.
Official docs:
Pjax

I would probably will use GridView $filterSelector.
And add the selector for youre custom category dropDown.
After you will need add category attribute for youre SearchModel.
In final - wou will get the filter with youre custom field added to standart some

Related

Yii2 Pjax Pagination On multiple Gridview not working

Have One Page 2 Gridview Table . Pagination Does not work properly ,
Here is the Code :
// table 1
<?php Pjax::begin(['id'=>'table_1']); ?>
<?= GridView::widget([
//
]); ?>
<?php Pjax::end(); ?>
// Table 2
<?php Pjax::begin(['id'=>'table_2']); ?>
<?= GridView::widget([
//
]); ?>
<?php Pjax::end(); ?>
It is already explained in the guide and here I quote.
You can use more than one GridView on a single page but some additional configuration is needed so that they do not interfere with each other. When using multiple instances of GridView you have to configure different parameter names for the generated sort and pagination links so that each GridView has its own individual sorting and pagination. You do so by setting the sortParam and pageParam of the dataProvider's sort and pagination instances.
In your case:
use yii\grid\GridView;
$tbl1Provider->pagination->pageParam = 'tbl1_page';
$tbl2Provider->pagination->pageParam = 'tbl2_page';
echo GridView::widget([
'dataProvider' => $tbl1Provider,
]);
echo GridView::widget([
'dataProvider' => $tbl2Provider,
]);

yii2 DetailView template/layout without values

I have a little problem in yii2 framework.
I have DetailView widget
<?= DetailView::widget([
'model' => $table_1,
'attributes' => [
'year',
'table_zs_field_1',
'table_zs_field_2',
'table_zs_field_3',
'table_zs_field_4',
'table_zs_field_5',
'table_zs_field_6',
'table_zs_field_7',
'table_zs_field_8',
'table_zs_field_9',
'table_zs_field_10',
'table_zs_field_11',
'table_zs_field_12',
'table_zs_field_13',
'table_zs_field_14',
'table_zs_field_15',
'table_zs_field_16',
'table_zs_field_17',
'table_zs_field_18',
'table_zs_field_19',
],
]) ?>
If i write this to code I'll see a DetailView widget with names of fields(get from model) and values.
Problem: I want to hide values and show only names of fields from model and in next time hide names and show only values. Anybody know ?
Change the $template property of the Detailview.
The Default is
$template = '<tr><th>{label}</th><td>{value}</td></tr>'
Adding
'template'=>'<tr><th>{label}</th></tr>' ,
to the config array of your DetailView should show only the names of the fields.
Adding
'template'=>'<tr><td>{value}</td></tr>',
should show only the value.
See the corresponding section in the Documentation of DetailView.

yii2: how to add fixed filter in SerachModel->search(...)

I'm quite new to Yii and I'm facing a little problem,
I would like to display a table already filtered adn allow the filter with query parameters string.
I declare a new action in Controller
public function actionIndexFiltered($candidateId)
{
$searchModel = new CvRisorsaTitoloSearch();
//$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider = $searchModel->search([$searchModel->formName() => ['is_cod_candidato' => $candidateId]]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This code works but it doesen't allow filter with query paramters.
'CvRisorsaTitolo' is a "many to many" table with other information,
and 'is_cod_candidato' is a field of 'CvRisorsaTitolo'.
Hiow can I combine my filter and filter of query parameters ion other fileds "Yii::$app->request->queryParams".
Can anyone help me?
Well that sounds easy - you just merge your query data an predefined data:
$dataProvider = $searchModel->search(\yii\helpers\ArrayHelper::merge(
Yii::$app->request->queryParams,
[$searchModel->formName() => ['is_cod_candidato' => $candidateId]]
));
If you put predefined data as a first argument it may be redefined by user request.

yii gridview does not display all the data from its respective table

i have this query using CActiveDataProvider. My table contains 15 records that satisfies the condition, but for some reason, only ten records are being displayed. What might be missing here.
$prov2 = new CActiveDataProvider('BaseSiReceivedItem', array(
'criteria' => array(
'condition' => 'iar_no = 0'
)));
echo count($prov2->data);
'pagination'=>array(
'pageSize'=>15,
)
Does that now show you 15?
This link about itemCounts may also be helpful. Yii Pagination Variables from DataProvider
The listview widget's pageSize value in the view file might be overriding the pageSize value in the controller (if it's not set, the default value may override the controller).
Or, you could create a custom list view like this:
extensions/CustomListView.php
<?php
Yii::import('zii.widgets.CListView');
class CustomListView extends CListView {
public function init() {
if ($this->dataProvider !== null)
$this->dataProvider->pagination->pageSize = 30;
parent::init();
}
}
and use it in your view: views/{model}/index.php
<?php $this->widget('CustomListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view'
)); ?>
I had also encountered this problem , listing only 10 items while disabling pagination from my list view. At that time I used the following code and I got all the items listed :)
$data = new CActiveDataProvider('ProviderPortfolioSet', array('criteria' => $criteria, 'pagination' => false));

Yii CGridView - Custom Columns

been looking for a solution to add a feature for "Custom Columns"... Meaning, I present a list of columns that I can show the user and he selects the ones he wants to see and after the selection the table is updated and add/removes the needed columns.
Didn't find anything on Google (perhaps it has a different name than what I was looking for...)
Anyone has an Idea on how it can be accomplished?
Thanks in advance!
This is not a complete sample, but can give you some clues on how to implement it. You've to define some kind of form to collect the data about how your grid has to be rendered. I recommend you to create a CFormModel class if there are more than 3 input fields. Create a view file with the form and a div or renderPartial of a file containing a grid:
$form = $this->beginWidget('CActiveFormExt');
echo $form->errorSummary($model);
echo $form->labelEx($model,'column1');
echo $form->dropDownList($model
echo $form->error($model,'column1');
echo CHtml::ajaxSubmitButton('UpdateGrid',array('controller/grid'),
array('update'=>'#grid'),
$this->endWidget();
// you can render the 'default options' before any ajax update
$this->renderPartial('_grid',array($customColumns=>array('id','name'),'dataProvider'=>$dataProvider));
In the _grid.php view file:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'grid',
'dataProvider'=>$dataProvider,
'columns' => $customColumns;
));
In the controller:
function actionGrid(){
// recover the form data, and build the custom columns array
$customColumns = array();
$customColumns[] = '.....';
$dataProvider = ...;
$this->renderPartial('_formTrabajo', array('customColumns' => $idSiniestro, 'dataProvider' => $dataProvider'), false);
}
When you click the ajaxSubmitButton, the form is sent to the url specified through ajax, and the reply from the controller must contain the renderPartial of the view containing the grid, so the jQuery call can replace the html correctly. You must pass an array from your controller to the partial view of the grid, with the custom list of columns you want to display.

Categories