I created a collection of inputs with Zend\Form\Element\Collection, like
$this->add([
'type' => 'Zend\Form\Element\Collection',
'name' => 'some-name',
'options' => [
'label' => 'some name',
'count' => 3,
'target_element' => [
'type' => 'text',
],
],
]);
This codes renders 3 inputs with label and fieldset if I use
echo $this->formCollection($form->get('some-name'));
(or the like of formCollection) in the view script.
I want to wrap each input of the collection into divs. My idea is to iterate over this input collection to extract the inputs.
How can I do this?
You can treat the collection as a traversable object.
<?php foreach ($form->get('some-name') as $element) : ?>
<div>..</div>
<?php endforeach; ?>
Related
i'm trying without success to add an image inside a Navbar menuItem.
It works fine in the brandLabel
But doesn't work on anoter menu item
(I Want to display the logged user image near his name)
I Tryed several times but doesnt seem to make it work,
Would apreciate your help please...
The Code is this:
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => Html::img('#themes'.'/siteImages/Logo.jpg', ['alt'=>Yii::$app->name]),
'options' => [
'class' => 'navbar-default navbar-fixed-top',
],
]);
// display Login page to guests of the site
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => Yii::t('app', 'Login'), 'url' => ['/site/login'],
'options' => [
'class' => 'navbar-right',
],];
}
else
{
// Show sales content to sales+ users
if ( Yii::$app->user->can('useSalesContent'))
{
$menuItems[] = ['label' => Yii::t('app', 'Sales'),
// 'class' => "pull-left",
'url' => Yii::$app->homeUrl,
'linkOptions' => ['id' => 'sales',
'class' => 'navbar-left',
],
];
// Show Admin content to manager+ users
if (Yii::$app->user->can('useAdminContent'))
{
$menuItems[] = ['label' => Yii::t('app', 'Administration'),
'url' => Yii::$app->homeUrl,
'linkOptions' => ['id' => 'admin'],
'options' => [
'class' => 'navbar-left',
],];
}
// display Logout to logged in users
if (!Yii::$app->user->isGuest) {
$menuItems[] =
[
'label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user-
>identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
//****** This is where I want the user image to be shown ****//
];
}
}
// echo navbar with selected items
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
<div class="container">
<?= Alert::widget() ?>
<?= $content ?>
</div>
endBody() ?>
Provided the user model has an attribute imageUrl it would be accessible through:
Yii::$app->user->identity->imageUrl
This would become something like:
If you would like to add it to the logout button:
$menuItems[] = [ 'encode'=>false, label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user- >identity->username . ') ' . Html::img(Yii::$app->user- >identity->imageUrl), 'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post']];
make sure you include 'encode' => false,
To add it as a separate item (for instance, to give it a different link):
$menuItems[] = [ 'encode' => false, 'label' => Html::image(Yii::$app->user- >identity->imageUrl)];
Edit:
Make sure you include the Html with a use statement in your view, or provide the full namespace declaration with Html class.
Question already exists with answer (below link), but:
1) doesn't work for me
2) doesn't include the added need to have a selected option
yii2 how to add additional attributes to dropDownList() elements??
Can help?
IM USING:
<?=$form->field($invoice, 'id')
->dropDownList(ArrayHelper::map($some_items_array, 'value_field', 'show_field'), [
'options' => [
$some_selected_id => ['Selected'=>true]],
'data' => ['attrib1' => "valueA', 'attrib2' => "valueB']
'class' => 'form-control',
'prompt' => ''])->label(false);
?>
I NEED, BUT DO NOT GET:
<select name="name">
<option value="value" data-attrib1="valueA" data-attrib2="valueB">text< option>
</select>
Already answered here > YII - Add another attribute to dropDownList
$attributes = [
'attrib1' => 'valueA',
'attrib2' => 'valueB',
];
foreach ($some_items_array as $index => $att) {
$dropdownlist_options[$index] = $attributes;
}
<?=$form->field($invoice, 'id')
->dropDownList(ArrayHelper::map($some_items_array, 'value_field', 'show_field'), [
'options' => $dropdownlist_options, /* [
$some_selected_id => [
'selected' => true,
'attrib1' => 'valueA',
'attrib2' => 'valueB',
],
$some_other_id => [
'attrib1' => 'valueA',
'attrib2' => 'valueB',
],
],*/
'class' => 'form-control',
'prompt' => '',
])->label(false);
?>
I wanted to fetch the selected value from the database and display it in codeigniter form_dropdown() function but it displays wrong.
Controller:
$type = array(
'options' => array(
'section' => 'Section',
'transaction' => 'Transaction',
'document' => 'Document'
),
'attributes' => array(
'class' => 'form-control'
)
);
View:
<?php echo form_dropdown('type', $type['options'],'', $type['attributes']) ?>
The Screenshot
Try the below code:
Controller:
$this->data['type'] = array(
'name' => 'type_value',
'attributes' => 'class="form-control"',
'value' => (isset($database_type_value) && trim($database_type_value)) ? $database_type_value: $this->input->post('type_value',TRUE), //$database_type_value - value from database
'options_list' => array(
'section' => 'Section',
'transaction' => 'Transaction',
'document' => 'Document'
),
);
View:
<?php echo form_dropdown($type['name'],$type['options_list'],$type['value'],$type['attributes']);?>
I want to use PJax in Yii Grid View, not with the associative filter that comes inside the Grid View, but with the Search Filter that's outside it, so it can filter the results inside.
Here is the source of the index file:
<div class="cars-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Cars', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
['attribute' => 'code',
'label' => 'Colour',
'format' => 'raw',
'value' => 'colour',
'contentOptions' => function ($model, $key, $index, $column){
return ['style' => ' text-align: center; width: 100px;color:white;background-color:#'. $model -> code];
},
'headerOptions' => ['style' => 'text-align: center;'],
],
'price',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Am I supposed to create an active form just for the part I want to filter? Or is there another way?
If You can't simply add the filter to you table like this
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
I suggest you use a proper action and a proper search function called by submit based on a specified active form
for action form eg:
<?php $form = ActiveForm::begin([
'id' => 'search-form',
'method' => 'post',
'action' => ['controller/yourAction']
]); ?>
in the controller
$model = new yourActiveForm();
if ($model->load(Yii::$app->request->post()) ) {
$dataProvider = $searchModel->search( [ 'yuorSearchModel'=> ['your_att1' => $model->yourValue1]]);
}
then your render
Conforming to yii2 doc
Pjax only deals with the content enclosed between its begin() and
end() calls, called the body content of the widget. By default, any
link click or form submission (for those forms with data-pjax
attribute) within the body content will trigger an AJAX request. In
responding to the AJAX request, Pjax will send the updated body
content (based on the AJAX request) to the client which will replace
the old content with the new one. The browser's URL will then be
updated using pushState. The whole process requires no reloading of
the layout or resources (js, css).
You may configure $linkSelector to specify which links should trigger
pjax, and configure $formSelector to specify which form submission may
trigger pjax.
You must add the
<?php Pjax::begin(); ?>
.... your active form
<?php Pjax::end(); ?>
and configure the proper $linkSelect and $formSelector
In your filter view:
<div id="myFilter">
<?php $form = ActiveForm::begin([
'id' => 'myFilterForm',
'method' => 'post',
'action' => [...],
]); ?>
...
</div>
And make sure you render filter between Pjax::begin and Pjax::end
Yet here comes the trick. If your server does not respond within default timeout, Pjax gets ignored and page reloaded, so make sure the timeout is big enough:
<?php Pjax::begin([
'id'=>'myGrid',
'timeout' => 10000, // <------------ THIS !!!!!!!
'formSelector' => '#myFilterForm'
]); ?>
<?= $this->render('myFilter', ['model' => $searchModel]); ?>
<?= GridView::widget([
...
]); ?>
<?php Pjax::end(); ?>
Also in your controller, you might want to "reset" the search model, so only data from the request used are attributes actually used by search:
public function actionSearch()
{
$searchModel = new MySearch();
if ($searchModel->load(Yii::$app->request->post())) {
$searchModel = new MySearch(); // "reset"
$dataProvider = $searchModel->search(Yii::$app->request->post());
} else {
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
}
return $this->render('search', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Hope this helps. Cheers!
I am trying to make an Ajax GridView using Pjax. Everything is working fine except the view, update and delete buttons are not AJAX. The code is:
<?php yii\widgets\Pjax::begin(['id' => 'demo']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php yii\widgets\Pjax::end(); ?>
The problem is that the links for delete, view and update have the attribute data-pjax=0 which disables AJAX functionality. I cant find out how to set it too data-pjax=1.
You must do like below:
For Delete Action
1- Change your delete action like below:
public function actionDelete($id) {
$this->findModel($id)->delete();
if (Yii::$app->getRequest()->isAjax) {
$dataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
'sort' => false
]);
return $this->renderPartial('index', [
'dataProvider' => $dataProvider
]);
}
return $this->redirect(['index']);
}
2- In your grid view:
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-pjax'=>'w0',
]);
}
]
],
Now, it works with Pjax.
Notes
My code in deleteAction() may decrease performance. You can write your own.
w0 usually is the default id of PJax. You can add an id to PJax and write it there instead.
This is the same for Update and View, But you need to change the way you show your update and view views.
This is highly recommended to take a look at Yii2's official PJax document: http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html