Yii2 : Dropdownlist - php

I'm trying to create A Point of sale in YII2 so i need to create a select2 for items search in sales page , i need it to search between to tables items , Bar_code i made it in tow tables because there is items have more than 1 Bar_code
I know how the select2 works and its works with me fine in on table
<?= $form->field($model, 'item_id')->widget(select2::className(),[
'data'=> arrayhelper::map(items::find()->all(),'item_id','item_name'),
'options'=>['placeholder'=>'Enter item name or scan barcode'],
'pluginOptions'=>[
'allowClear'=>true
],
])?>`
but in tow its complicated or is there other way to do it ?

try this
<?php
$data1 = arrayhelper::map(Items::find()->all(),'item_id','item_name'),
$data2 = arrayhelper::map(Items2::find()->all(),'item_id','item_name'),
?>
<?= $form->field($model, 'item_id')->widget(select2::className(),[
'data'=> ["$data1", "$data2"],
'options'=>['placeholder'=>'Enter item name or scan barcode'],
'pluginOptions'=>[
'allowClear'=>true
],
])?>`

Related

how to get and display username in another form field when user select name from dropdownlist in yii2?

I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function(){
$("#{$ddId}").on('change',function(e){
$("#{$inputId}").val($(this).val());
});
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
A better way for dropdowns is to use the library Kartik-v\select2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() { $("#{$ddId}").on('change',function(e){
$("#{$inputId}").val($(this).val());
}); }',
]
]);

Yii2 how to create table template using select2 widget

I have a dropdown list using select2 widget to get the customer contact
$getcontact = ArrayHelper::map(OpContact::find()->all(),'id','code');
<div class="col-md-10">'.
$form->field($model, 'contact_id')->widget(Select2::classname(), [
'data' => $getcontact,
'language' => 'en',
'options' => ['placeholder' => 'Select'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false).'
For now, its just a normal dropdownlist which show only the contact id.How I can like insert 2nd column for it and put all the data inside a table. For example, I want the dropdown to look like this where I can see the id and the contact name together.
------------------------------
ID Contact ID
------------------------------
21 contact1
22 contact2
23 contact3
24 contact1
Anyone have any idea? Thanks
if you want store only the id and see both id and contactID and contact ID is th concat of id and code you couuld use
$getcontact = ArrayHelper::map(OpContact::find()
->select('id, concat(id, code) as contactID')
->all(),'id','contactID');
if you want store both you can
$getcontact = ArrayHelper::map(OpContact::find()
->select('concat(id, code) as id, concat(id, code) as contactID')
->all(),'id','contactID');
you can't show all of them , but you can merge that field in string mode in your query like CONCAT or in your foreach merge string with " . " together
Change ArrayHelper as below .
$getcontact = ArrayHelper::map(OpContact::find()->all(),'id',function($model){ return $model->id.'-'.$model->code; });

How do I get a list of key, value pairs from the database for a select list in Yii?

How do I get a list of key/value pairs to populate the options to a relation in a select list in Yii?
Similar to the following in Rails:
= f.select :age_group_id, AgeGroup.order(:name).pluck(:name, :id)
Get the list of values, perhaps in the controller
$campaigns = Campaign::find()->select('name')->indexBy('id')->column();
Display the list
<?= $form->field($model, 'campaign_id')->dropDownList($campaigns) ?>
Could be useful somethings like his
<?= $form->field($model, 'your_field')->listBox( ArrayHelper::map(Country::find()->all(), 'country_id', 'Country_description'),['prompt'=>'']) ?>
or for dropdown
<?= $form->field($model, 'your_field')->dropDownList( ArrayHelper::map(Country::find()->all(), 'country_id', 'Country_description'),['prompt'=>'']) ?>
change country_id, your_field and country_description for your need

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

Dropdown Yii 2.0 with 2 parameters

I'm developing a dropdown list that gets the values and his related from the same table.
AssetType
asset_type_id
name
order
parent_asset_type (related to AssetType.asset_type_id) - the top type as value = NULL
<?= $form->field($model, 'asset_type_id')->dropDownList(
ArrayHelper::map(AssetType::find()->where("parent_asset_type IS NOT NULL")->all(), 'asset_type_id', 'name', 'parent_asset_type'),
['prompt'=>'Choose a Category']);?>
And with this Yii2 arrayHelper i can save 3 values, the id of the asset which is not null, his name and the parent_asset_type (Which shows the ID)
Basically:
DropDownList
Choose a Category
1
T-Shirts
Jeans
2
Computers
Cellphones
Instead of the group Id i would love to know how can i make it show the name of that Asset Type.
I hope this will help you...
I have created a function in the model modelname.php
public function getAssetType(){
return $this->name .'-'.$this->parent_asset_type;
}
view.php
<?php
$asset = ArrayHelper::map(AssetType::find()->all(),'id','AssetType');
echo $form->field($model, 'asset_type_id')->dropDownList($asset,
['prompt'=>'Choose a Category']);
?>
Thank You...

Categories