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...
Related
I have two tables; applicant_rating (AR) and applicant_rating_score (ARS). I have made a dropdown menu using the values in applicant_rating:
<?= $form->field($model,'rating_id')->dropDownList(Applicant::getApplicantRatingNames(),['prompt' => 'Please select']);?>
This is the function being called in the model:
public function getApplicantRatingNames() {
$rate = ApplicantRating::find()->all();
$listData = ArrayHelper::map($rate, 'id', 'name');
return $listData;
}
AR.id = ARS.rating_id
The chosen value from the dropdown menu on submission goes into rating_id in ARS table.
IN THE DROPDOWN MENU I ONLY WANT TO DISPLAY VALUES THAT ARE NOT IN rating_id. So in dropdown menu if for example id 1 was chosen, i want to only display values which are not id 1; where id != rating_id
Any help would be appreciated.
The syntax of not statement in ActiveQuery is ['not', [columnName => value]]
So I suppose you should do this like this:
Model::find()->where(['not', ['id', $rating_id]])->all();
I inherited a YII based web application but I am not really familiar with Yii and don't find the solution yet. So if you can, please help me!
There is a form. In the form there is a dropdown list which get data from a db table. This table contains the name and the price of leases. The dropdown lists only the name.
<?php
$typeNames = array();
foreach(LeaseType::model()->findAll() as $lt) {
$typeNames[$lt->id] = $lt->name;
}
?>
<?php echo $form->dropDownListControlGroup($model, 'lease_type_id',
$typeNames,
array(
'empty'=>'Válassz!',
'onchange'=>'$("#model-payed").val($("#model-lease_type_id option:selected").text());',
)
); ?>
But I need to change the value of a specific input text field with the price of the selected lease.
<?php echo $form->textfield($model,'payed');?>
I can get the name of the lease but I can't set the value of the input for the price of the lease. This field has to be changable based on the customer payed all the price or just partial.
Other problem is that the name and price of the lease in different table than the value needed.
I hope all this are understandable because I am not fluent in English.
Thanks for any help!
Finally I found the solution, so share it maybe someone can use it.
In the views/_form.php I modified the dropdownlist:
<?php
echo $form->dropDownListControlGroup($model, 'lease_type_id', $typeNames, array(
'empty' => 'Válassz!',
'onchange' => '$("#model_comment").val($("#model_lease_type_id option:selected").$typeNames);',
//'onchange'=>'if($(this).val() == "no"){("#quantity").val("0"); }'
)
);
?>
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
],
])?>`
I'm very new to Yii and I'm trying to update a value of an textinput , depending on the value selected on a dropdown list. in my view I have this inside my active form
<?= $form->field($model, 'quantity')->textInput(['readonly' => !$model->isNewRecord]) ?>//this is an integer
<?= $form->field($model, 'allowquantity')->dropdownlist(['yes' =>'yes','no'=>'no']) ?>
I want to be able to change the value of the field quantity, depending on the selected value in dropdownlist. example, if i choose yes in the dropdown, it will retain the value in the quantity field. I choose no , it will turn the value to 0. this will happen in my update form so that i can update the values in the database. Any help will be much appreciated
Change your dropdown , make sure #quantity is the id of quantity textbox
echo $form->field($model, 'allowquantity')->dropDownList(['yes' =>'yes','no'=>'no'],
['prompt'=>'-Choose a option-',
'onchange'=>'if($(this).val() == "no"){
$("#quantity").val("0"); }'
]);
I have two tables: product and product_type (related to the models resp. Product and Product_type):
product : product_id, product_type_id, name, etc...
product_type : product_type_id, name, desc, etc...
Both are related with key "product_type_id".
I have created the crud for both tables using gii crud generator. Now on Product form page I want to display the listing of all product_type name in dropdown field using Yii ActiveRecord. I have added this line in views/product/_form.php script:
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'id', 'id');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
But it's showing blank dropdown field :(
How can I do this?
Solved MySelf :)
By just providing product_type_name.
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'product_type_id', 'product_type_name');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
In Yii2 the class CHtml does not exist anymore.
Below is a solution, based on the following assumptions:
The Yii2 framework is used;
The table product_type is associated with the model Product_type;
The product_type table has a field called "type-name".
<?php
// get all product types from the corresponding table:
$productTypes = Product_type::find()->orderBy('type-name')->asArray()->all();
// create an array of pairs ('id', 'type-name'):
$productTypeList = ArrayHelper::map($productTypes, 'id', 'type-name');
// finally create the drop-down list:
$form->field($model, 'product_type_id')->dropDownList($productTypeList)
?>
Change your code like bellow
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'table_col_name1', 'table_col_name2'); //table_col_name1 is value of option, table_col_name2 is label of option
// echo $form->dropDownList($model, 'product_type_id', $list);
echo CHtml::dropDownList('name_of_the_dropdown', $model, $list);
?>