How to reset kartik depdrop to initial state with initial data - yii2

i have a depdrop in my project like
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
])->label(false); ?>
And
<?=$form->field($model, 'building_id')->widget(DepDrop::classname(), [
'data' =>ArrayHelper::map(Buildings::find()->all(),'id','buildingWithPlace'),
'options' => ['placeholder' => 'Select The Building','id'=>'select_building'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select_place'],
'url'=>Url::to(['property-commercial-rent/buildings']),
'loadingText' => 'Loading buildings ...',
]
])->label(false);?>
The second dropdown is depend on the first one.Initially they both have full list of data without any filtering.When i select a neighborhood in the first dropdown then the second populates with the building names under that neighborhood. Then i have a reset button like and has an action like
$( "#reset-location" ).click(function() {
$(select_place).val('').trigger('change');
$(select_building).val('').trigger('change');
});
And this click resetting the both select.BUt the problem is the building has only the items under the previously select neighborhood.I want the make it all like initial stage.How can i do this

You don't need a reset button. On the neighbourhood field allow clear, so it would be like this
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false); ?>
Then there will be a x in the field to clear it.

Related

Yii2 logout Method Not Allowed (#405)

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

How to add a placeholder to Select2 Widget - Yii2

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

Yii2 Redactor is not loaded after ajax/pjax request

I use redactor for editing comment. There are multiple redactors since there are multiple comments. Most of the time, redactor is loaded, but sometimes the redactor is not loaded and only generate normal text editor.
These comments are loaded after Pjax Request
My code:
<?= \yii\redactor\widgets\Redactor::widget([
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>
Problem solved:
You have to put ID :)
<?= \yii\redactor\widgets\Redactor::widget([
'id' => 'edit_redactor_' . $comment_id,
'name' => 'comment',
'value' => \yii\helpers\HtmlPurifier::process($comment),
'clientOptions' => [
'imageUpload' => \yii\helpers\Url::to(['/redactor/upload/image']),
],
]) ?>

Yii 2 Editable, how to use editable inside form?

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?
You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.

can use yii2 DepDrop::widget without model?

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'])
]
]); ?>