Yii2 Pjax Pagination On multiple Gridview not working - php

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

Related

Displaying yii2 checkboxlist vertically

I have an active form which currently displays checkboxlists horizontally but I would like it to display them vertically. I have set the form layout to vertical but it still displays them horizontally:
This is what I have tried:
//generates an array of permissions
$options = Permission::value_list(
Permission::findWhere()->select('name')->andWhere(['not', ['name' => $name]])->all(),
['name']
);
This is the form
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
What do I need to add: currently they are displayed in this way.
I would like the displayed vertically.
You could use the separator option mentioned here: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeCheckboxList()-detail
Your call would then look like this for example:
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options, ['separator'=>'<br/>'])
->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
Or whatever you want to use as the separator in your specific case.
Hope that helps...
The label to use Note that this will NOT be encoded.
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'country')
->inline(true)
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>

Take data from another field in yii2 form

I'm making a form with yii2, now I have two field:
<?php echo $form->field($model, 'Protocol')->textInput(['maxlength' => true])->dropDownList(
array("rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"), // Flat array ('id'=>'label')
['prompt'=>'Select'] // options
); ?>
<?php echo $form->field($model, 'url')->textInput(['maxlength' => true]); ?>
They look like this:
How can I take the selection from Protocol's drop down list and automatically add it to the below URL field? Like this: I type the http:// in the field manually, is there anyway I can make it automatic?
Add onchange event in your 'protocol' dropdownList. show below code
<?= $form->field($model, 'Protocol')->dropdownList(["rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"], [
'onchange'=>'$( "#'.Html::getInputId($model, 'url').'").val($(this).val());'
]) ?>
<?= $form->field($model, 'url')->textInput(['maxlength' => true]);
?>

yii2 foreign key dropdown

I am trying to display a dropdown with a category list in yii2 framework.
The tables in my database are setup with foreign key and used Model and Crud generator to generate the code.
I am trying to edit the code now to change the textfield into a dropdown with the values from the category table.
<?php $categoryArray = ArrayHelper::map(\app\models\Category::find()->orderBy('name')->all(), 'id', 'name') ?>
<?= $form->field($model, 'category_id')->dropDownList($categoryArray, ['prompt' => '---- Select category ----'])->label('category') ?>
This comes back with the error "2.yii\base\ErrorHandler::handleFatalError()"
Most related post to my problem refer to version 1 of the framework but can't find a good example how to do this with version 2.
use yii\helpers\ArrayHelper;
use backend\models\Model_name;
<?= $form->field($model, 'Field_id')->dropDownList(
ArrayHelper::map(<Model_name>::find()->all(),'Field_id','Field_name'),
['prompt'=>'Select XYZ']
)?>
I overlooked the error on top saying 'Class ArrayHelper not Found' this has been resolved by adding the following line on top:
use yii\helpers\ArrayHelper;
Add ->asArray() to your find query:
$categoryArray = ArrayHelper::map(\app\models\Category::find()->orderBy('name')->asArray()->all(), 'id', 'name');

how to change grid view through Ajax? yii2

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

Basic Hidden field in yii

I'm trying to place data in hidden text in yii, but I don't know how.
I need a similar code to a regular php syntax:
<input type="hidden" name="field_name" value="a"/>
It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.
Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.
in views
hidden field with model and form:
<?php echo $form->hiddenField($model, 'name'); ?>
or without model
<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>
Yii hidden input :
<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
In Yii2 this has changed too:
<?= Html::activeHiddenInput($model, 'name') ;?>
References:
http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/
https://github.com/yiisoft/yii2/issues/735
if data from database and value or size field:
echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Yii 1
<?php echo $form->hiddenField($model, 'name'); ?>
Yii2
<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>
Also, worth noting for Yii2, the array parameter works different to a normal form field.
E.G. A normal input would look more like this.
<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>
Hope this helps.
for yii2 you can try this
<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>
It worked for me
Alternatively,
echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));
This would set hidden field value as the id from model. The [$i] is useful for multiple record update.
Here are two ways to do that...
without model
echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));
with model
echo $form->hiddenField($model, 'name');

Categories