Change User Status From SwitchInput into Dropdown list - php

Currently, I have an existing code to show user status as 'Inactive' or 'Active' based on ban_time field of table User. When status of User is 'Inactive', the ban_time field will be updated by current timespan (I guess it use external wrapper plugin)
$form->field($user, 'ban_time')->widget(SwitchInput::classname(), [
'type' => SwitchInput::CHECKBOX,
'containerOptions' => ['class' => 'inner-form-group'],
'pluginOptions' => [
'state' => empty($user->ban_time),
'handleWidth' => 60,
'onText' => 'Active',
'offText' => 'Inactive'
],
'pluginEvents' => [
"switchChange.bootstrapSwitch" => "function(event, state) { $('[name=\'User[ban_time]\']').val(state ? 0 : 1) }",
]
])->label('Status');
Now, I need to add more status instead of 'Inactive' or 'Active'. So I want to change this field into dropDownList but when changing status of User, ban_time was not changed
$form->field($user, 'ban_time')->dropDownList(
[empty($user->ban_time) =>'Active', !empty($user->ban_time) =>'Inactive']
)->label('Status');
Please help me how to change it

for example if you have dropdownlist like this given below
echo $form->dropDownListGroup(
$model, 'status', array(
'wrapperHtmlOptions' => array(),
'widgetOptions' => array(
'data' =>$model->getDropdownvalue(),
'htmlOptions' => array(
'prompt' => 'Select Project',
'ajax' => array(
'type' => 'POST',
'url' => your url,
//'dataType' => 'json',
'data'=>array('status'=>'js:this.value'),
)
in your controller you will get the value of dropdown list using url
public function actiondropdownvalue(){
$model = new status();
$status = $_POST['status'];
$model->save();
this example is only showing how it will work . You will need user id to save status for particular user to update or save status.

You can make your form element as dropDownList as
$items = [1 =>'Active', 0 =>'Inactive' ,2 => 'Subscribed' ,3 => 'Deleted'];
$form->field($user, 'ban_time')->dropDownList($items)->label('Status');
See DropDownList

Related

How to set default values in Yii2 kartik\tree\TreeViewInput

I used kartik\tree\TreeViewInput in my project but in update form I can't display current selected values in treeview input!
I tried some thing like this based on documentation:
<?= $form->field($model, 'tags')->widget(\kartik\tree\TreeViewInput::className(),[
'name' => 'tags',
'query' => Tags::find()->addOrderBy('root, lft'),
'value' => 1,
'headingOptions' => ['label' => 'tags'],
'rootOptions' => ['label'=>'<i class="fa fa-building"></i>'],
'fontAwesome' => true,
'asDropdown' => true,
'multiple' => true,
'options' => ['disabled' => false]
]); ?>
But it doesn't display tag with (id='1')! How should I display values?
Use:
'displayValue' => 1,
to auto display a node with id = 1 on the form.
If you do not want any node to be auto displayed use:
'displayValue' => 0,

Select2 initial value doesn't appear

I Want to select value base on $_GET if exists soon.
Here is my code in view
<?php
/*if ($model->isNewRecord && !isset($model->agent_id_upper)) {
$model->agent_id_upper = 65;
} not working too*/
//$model->agent_id_upper = 65; ->not working
echo Select2::widget([
'model' => $model,
'name' => 'TbAgent[agent_id_upper]',
//'id' => 'to_id',
'initValueText' => '', // this only for ajax
'data' => $data,
'options' => [
'placeholder' => 'Choose Agent ...',
'multiple' => false,
//'selected' => 65, -> not working
//'value' => 65, -> not working
'class' => ''
],
]
);
?>
65 is ID of record, If selected then it should show username of thus ID.
But my problem is username of 65 is not selected, it only show place holder.
How I can fix this? and Please give me references.
already read this
yii2 select2 by kartik-v set default value
Yii2: Kartik Select2: Initial Value from Model Attribute
http://www.yiiframework.com/forum/index.php/topic/52278-kartik-select2-not-select-corretly/
but no luck with above.
Thanks in advance.
have you tried this? value is not inside options
<?= Select2::widget([
'model' => $model,
'name' => 'id',
'data' => $data,
'value' => 65,
'options' => [
'placeholder' => 'Choose Agent ...',
'multiple' => false,
]
]) ?>

Multi select dropdown CakePHP 3 selected issue

$selected_country = array(1,3);
$this->Form->input('country', [
'options' => $source_types,
'label' => 'Country: ',
'multiple' => true,
'class' => ' form-control',
'selected' => $selected_country,
'type' => 'select'
]);
If selected country has only one value then it selects the option but if the selected country has more than one value then it doesn't select any value.
If you want to pass more than one value on $selected_country, try it:
echo $this->Form->select('rooms', [
'multiple' => true,
// options with values 1 and 3 will be selected as default
'default' => [1, 3]
]);
Reference: CakePHP Cookbook
For anyone this works
echo $this->Form->input('venues._ids', ['options' => $venues, 'class'=>'form-control select4', 'label' => false]); IF you have the database associations set up AND you have it referenced in the controller like so
$event = $this->Events->get($id, [
'contain' => ['Venues']
]);
otherwise adding the 'default' => [1, 2] ids in also works but it has to be first created in the controller and then fed to the view in list form.

Yii2 Doesn't Get the Value of Disabled Dropdown List or Textfield

I want to disable certain textfields and dropdown lists to prevent user from changing its values. But whenever I try to, it doesn't collect/get the data of that specific disabled textfield or dropdown list.
Here's my view where I display my dropdown lists. It's inside a for loop:
echo $form->field($model1[$i], 'earning_item_id')->widget(Select2::classname(), [
'data' => $earningslistData,
'options' => ['placeholder' => '', 'prevOptionID' => $model1[$i]->earning_item_id, 'prevOptionName' => $earningslistData[$model1[$i]->earning_item_id],
"name" => "EarningDetails[".$i."][earning_item_id]", "row_count1" => $i],
//'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
'label' => false
]
]);
Here's how it looks like without disabling them:
Then, when I save it, it looks like this:
But, when I disable the dropdown lists, it will give me this:
I think the Full Name comes from my model but I don't know why:
public function getFullName()
{
return $this->user ? $this->user->fname . ' ' . $this->user->lname : 'Full Name';
}
It goes the same when I disable a textfield:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff'],
'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
I am using Kartik widgets for my form fields.
Is there a way to fix this? Please tell me how.
EDIT
Thanks to the commenters below I found out the difference between disabled and readonly. Since it's a dropdown list, here's what I did:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', ],
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', 'style' => 'display:none'],
'pluginOptions' => [
'allowClear' => true,
],
])->label('');
Disabled html form field will not submit, the problem is not with yii itself. The solution in this case is to have 2 copies of the same field, one as disabled as you have already included and the other one hidden with the same value as below after the original one.
echo $form->field($model1[$i], 'earning_item_id')->hiddenInput()->label('');

Yii2 Autocomplete : Save the ID instead of value

I have a list of array called as datalist which contains the name of companies with id.when i use it with the typeahead widget,it captures the value of company with the variable coSearch(id of input).but i want to display the list of companies and when selected,it must give the vale of that id in variable .i am really messed with this problem working from three days.Please help me out.
Here is the code for my activeform which contains the widget.
<?php
$form = ActiveForm::begin([
'action' => ['search'],
'method' => 'get',
]);
$dataList=ArrayHelper::map($companies, 'id', 'name');
echo Typeahead::widget([
'model' => $companySearched,
'name'=>'coSearch',
'options' => ['placeholder' => 'Search company','id'=>'searchCompany1','class' => 'form- control','value'=>'1'],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => $dataList,
'limit' => 10,
]
]
]);
?>
This can be solved with the help of a hidden field.I use autocomplete here
<?php
use yii\web\JsExpression;
use yii\jui\AutoComplete;
$data = Company::find()
->select(['name as value', 'name as label','c_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'Company',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'4',
'select' => new JsExpression("function( event, ui ) {
$('#model-company').val(ui.item.id);
}")],
]);
?>
<?= Html::activeHiddenInput($model, 'company')?>
Hope this help!

Categories