I want to write a condition like this, for example:
Yii::$app->user->isGuest ? (
['label' => 'Sign Up', 'url' => ['/site/signup']]
):(
//do nothing
)
Can ?: condition be without else part?
EDIT:
It is sent to the array 'items' => [] and that's why the code below doesn't work:
if(Yii::$app->user->isGuest)(
['label' => 'Sign Up', 'url' => ['/site/signup']];
)
The full code:
NavBar::begin([
//somecode
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
Yii::$app->user->isGuest ? (
['label' => 'Sign Up', 'url' => ['/site/signup']]
):(
//HOW TO DO NOTHING HERE???
)
],
]);
it's possibe if you use condition in array,..
maybe you can check this link.
Conditionally show items of Nav widget
maybe useful
CMIIW
You may write this as -
['label' => 'Sign Up', 'url' => ['/site/signup'], 'visible' => Yii::$app->user->isGuest ]
Related
I'm using adminlte advanced template for backend. I want to add logout in the left column.
I've read other posts and I understand I've to add data method post. I've added it in following line in left.php file, but it doesn't work. How to make it work?
<?= dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'data-method'=>'post'],
]
) ?>
It is extending the yii\widgets\Menu and you need to specify the template to modify or add any attribute to the link as the data-method="post" needs to be added to your link you should change the code to the following
echo
dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
'items' => [
['label' => 'Logout', 'icon' => 'file-code-o', 'url' => ['/site/logout'], 'template'=>'{label}'],
]
);
You can add a form in to your click field:
$items[] = [
[
'label' => 'Logout',
'icon' => 'file-code-o',
'url' => ['/site/logout'],
'template' => Html::beginForm(array('site/logout')) .
Html::submitButton('Logout') . Html::endForm(),
],
];
I am working a yii2 page and it's giving me problems adding a placeholder or a prompt to Select2 Widget. Here is my code:
<?php
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\datecontrol\DateControl;
use yii\helpers\ArrayHelper;
/**
* #var yii\web\View $this
* #var app\models\FinancialAccounts $model
* #var yii\widgets\ActiveForm $form
*/
?>
<div class="financial-accounts-form">
<?php $form = ActiveForm::begin(['type' => ActiveForm::TYPE_VERTICAL]); echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 1,
'attributes' => [
'type_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\Select2', 'options' => ['data'=>ArrayHelper::map(app\models\FinancialAccountType::find()->all(), 'type_id', 'name')]],
'account_name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Name...', 'maxlength' => 100]],
'account_code' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Code...', 'maxlength' => 10]],
'parent_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\Select2', 'options' => ['data'=>ArrayHelper::map(app\models\FinancialAccounts::find()->all(), 'account_id', 'account_name'), 'placeholder' => 'Select a Parent Account...']],
'description' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Description...', 'maxlength' => 250]],
],
]);
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
);
ActiveForm::end(); ?>
</div>
The problem is in the parent_id attribute as I cannot add a placeholder as an option as most of the tutorials recommend. Everytime I try that, I get an error as this:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: kartik\widgets\Select2::placeholder
Does anyone know how I can solve this? My main problem is that I cannot leave this option as blank when submitting data yet that is one of the possibility. It forces me to submit an item selected.
You'll note that if you follow the examples in the documentation carefully, placeholder needs to be wrapped within an options array.
'parent_id' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\widgets\Select2',
'options' => [
'data' => ArrayHelper::map(app\models\FinancialAccounts::find()
->all(), 'account_id', 'account_name'),
'options' => ['placeholder' => '...'],
'pluginOptions' => ['allowClear' => true],
]
],
In my yii2 project, I'm using Pjax GridView.
My index page:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'batch',
[
'attribute' => 'file_import',
'format' => 'raw',
'value'=>function ($data) {
return Html::a($data->file_import, ['/device/index', 'DeviceSearch', 'batch' => $data->batch]);
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
The link in the file_import column goes to http://localhost/index.php/device/index?1=DeviceSearch&batch=200325806610154437. But in this url, all the data is showing instead of showing only the search result. I wanted to set the file_import column as an url which will show only the search result by the provided parameter in the url.
Thank you in advance.
Change URL route to
['/device/index', 'DeviceSearch[batch]' => $data->batch]
all developer kartik\DepDrop widget The ability to use without model
My code this but error
use kartik\widgets\DepDrop;
<?php echo DepDrop::widget([
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends => ['province'],
'placeholder => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>
I use this code for show dropdown without lable
Yes, for it you need set the attribute name:
<?php echo DepDrop::widget([
'name' => 'city',
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends' => ['province'],
'placeholder' => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>
anyone knows how can i in Yii2 using the navbar widget make a hyperlink to a div that is above the 100% height screen and is hidden in order that the screen scrolls down to it?
The code i'm using for the navbar is the standard one that comes with yii in layout.php
['label' => 'Contact', 'url' => ['/site/contact']],
I substitute 'url' => ['/site/contact']], --> by something like 'url' => ['#divid']],
But it doesn't works.
Many thanks in advance for an answer.
Just specify url as a string:
['label' => 'Contact', 'url' => '#divId'],
Url parameter is processed by Url::to() method, you can see there how string is interpreted:
a normal string: it will be returned as is.
Thank you very much for your help, it works. I was trying to do this
<body data-spy="scroll" data-target="#navbar" data-offset="50">
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
//'brandLabel' => Yii::$app->name,
'brandLabel' => '<img src="SLOGAN.png" style="display:inline; vertical-align:top; margin-top:20px; margin-left: 10px;"/>',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar navbar-fixed-top navbar-inverse',
'id'=>'navbar'
],
]);
echo Nav::widget([
'options' => ['class' => 'nav navbar-nav navbar-left'],
'items' => [
['label' => 'HOME', 'url' => '#section1'],
['label' => 'ABOUT', 'url' => '#section2'],
['label' => 'SERVICES', 'url' =>'#section3'],
['label' => 'FAQS', 'url' => '#section4'],
['label' => 'CONTACTS', 'url' => '#section5'],