Yii2 dropdownlist WITHOUT $model
Tag : php , By : CHeMoTaCTiC
Date : March 29 2020, 07:55 AM
Hope this helps I have searched the web far and wide for a solution to this problem. I already know the Yii2 dropdown way is this: , You can also use Html::dropDownList() <?= Html::dropDownList('s_id', null,
ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
|
Yii2 dropdownList select default option
Date : March 29 2020, 07:55 AM
may help you . Finally solved with an unbelievable change. Just changed the first letter of selected to capital ('selected' should be 'Selected'). Here is the code: <?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['Selected'=>'selected']]
, 'prompt' => ' -- Select Category --']) ?>
|
how can I get a value, not key from dropDownList Yii2
Date : March 29 2020, 07:55 AM
I hope this helps you . Use the same value for key and value in this sample country have code and name and the mao give you key value $listData=ArrayHelper::map($countries,'code','name');
$listData=ArrayHelper::map($countries,'name','name');
echo $form->field($model, 'name')->dropDownList($listData, ['prompt'=>'Choose...']);
|
Get same values in DropDownList yii2
Date : March 29 2020, 07:55 AM
To fix the issue you can do The problem lies in your code. In your actionCreate() you have created three model objects of the same type DevisReduction. $modelDevisReduction1 = new DevisReduction();
$modelDevisReduction2 = new DevisReduction();
$modelDevisReduction3 = new DevisReduction();
<?= $form->field($modelDevisReduction1, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-1'] )->label(false); ?>
<?= $form->field($modelDevisReduction2, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-2'] )->label(false); ?>
<?= $form->field($modelDevisReduction3, 'idReduction')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-3'] )->label(false); ?>
<?php
class DevisReductionForm extends yii\base\Model {
public $idReduction1;
public $idReduction2;
public $idReduction3;
public $taux1;
public $taux2;
public $taux3;
/**
* @inheritdoc
*/
public function rules() {
$rules = [
[['idReduction1', 'idReduction1', 'idReduction1', 'taux1', 'taux2', 'taux3'], 'required'],
];
return array_merge($rules, parent::rules());
}
}
public function actionCreate()
{
//create object of custom form model DevisReductionForm given above
$modelDeviceReduction = new DevisReductionForm ();
if ($modelDeviceReduction->load(Yii::$app->request->post())
{
$modelDevisReduction1 = new DevisReduction();
$modelDevisReduction2 = new DevisReduction();
$modelDevisReduction3 = new DevisReduction();
$modelDevisReduction1->idReduction = $modelDeviceReduction->idReduction1
$modelDevisReduction2->idReduction = $modelDeviceReduction->idReduction2
$modelDevisReduction3->idReduction = $modelDeviceReduction->idReduction3
$modelDevisReduction1->taux = $modelDeviceReduction->taux1
$modelDevisReduction2->taux = $modelDeviceReduction->taux2
$modelDevisReduction3->taux = $modelDeviceReduction->taux3
//write other codes
}
else
{
return $this->render('create', [
'modelDeviceReduction ' => $modelDeviceReduction ,
]);
}
}
/* Reduction 1 */
<?= $form->field($modelDeviceReduction, 'idReduction1')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-1'] )->label(false); ?>
<?= $form->field($modelDeviceReduction,'taux1')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-1'])->label(false); ?>
/* Reduction 2 */
<?= $form->field($modelDeviceReduction, 'idReduction2')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-2'] )->label(false); ?>
<?= $form->field($modelDeviceReduction,'taux2')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-2'])->label(false); ?>
/* Reduction 3 */
<?= $form->field($modelDeviceReduction, 'idReduction3')->dropDownList( ArrayHelper::map(Reduction::find()->all(),'idReduction','libelle'), ['prompt'=>'Sélectionner la Reduction','id'=>'DevisReductionType-3'] )->label(false); ?>
<?= $form->field($modelDeviceReduction,'taux3')->textInput(['maxlength' => true,'id'=>'DevisReductionTaux-3'])->label(false); ?>
|
Capture a value from a dropdownlist where the display dropdownlist is not default
Tag : chash , By : Topher Cyll
Date : March 29 2020, 07:55 AM
hope this fix your issue You're repopulating the DropDownList for every postback and reloading the page, hence the SelectedValue property value may be different from the posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback: protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ListItem> items = new List<ListItem>();
for (int i = 0; i < 5; i++)
{
items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));
}
ddldate.DataSource = items;
ddldate.DataBind();
ddldate.Items[0].Selected = true;
}
}
|