17

我想在 Yii2 下拉列表中显示选定的值,

$_GET 值:

  $id = $_GET["cid"];

下拉代码

  $form->field($model, 'userid')
    ->dropDownList(
          [User::getUser()],
          //[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')],
          ['prompt'=>'Select a user','id'=>'user_dropdown'],    
          ['options' =>
                    [                        
                      $id => ['selected' => true]
                    ]
          ]

        )->label('');           

但这种方法行不通!

4

9 回答 9

20

尝试这个。

$model->userid=$id;
$form->field($model, 'userid')
->dropDownList(...)
->label('');
于 2014-12-20T14:42:01.550 回答
8

基本上,您<option>通过使用value属性的实际值作为 dropDownList 选项数组中的数组键来影响选项(您的元素)。

所以在这种情况下,我有一个状态数组,值属性有状态缩写,例如value="FL"。我从存储缩写的地址表中获取我选择的状态,所以我所要做的就是将它用作选项数组中的数组键:

echo $form->field($model, 'state')->dropDownList($listData, ['prompt'=>'Select...', 'options'=>[$address->state=>["Selected"=>true]]]);

文档详细说明:http ://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#dropDownList()-detail

于 2016-08-31T17:19:31.290 回答
5

我希望这能帮到您

$form->field($model, 'userid')
    ->dropDownList(
          [User::getUser()],
          //[ArrayHelper::map(User::find()->where('id' => $id)->all(), 'id', 'name')],
          ['prompt'=>'Select a user','id'=>'user_dropdown'],    
          ['options' =>
                    [                        
                      $id => ['selected' => true]
                    ]
          ]

        )->label('');
于 2015-05-29T05:44:46.713 回答
3
$model->userid = $_GET['cid'];
$form->field($model, 'userid')
->dropDownList( 
      $items,                   //Flat array('id'=>'val')
['prompt'=>'']                  //options
)->label('');
于 2016-11-28T10:24:45.637 回答
2
<?php 
$selectValue = $_GET['tid']
echo $form->field($model, 'tag_id')
            ->dropdownList(
                ArrayHelper::map(Tag::find()->where(['visibility'=>'1'])->orderBy('value ASC')->all(), 'tag_id', 'value'),
                ['options' => [$selectValue => ['Selected'=>'selected']]], 
                ['prompt' => '-- Select Tag --'])
            ->label(false);
?>

此代码将自动选择作为输入接收的选定值。其中 $selectValue 将是从 GET 方法接收到的数值。

最终输出:<option value="14" selected="selected">NONE</option>

于 2017-02-13T10:13:02.567 回答
1

好的,如果您使用的是 ActiveForm,那么您的模型字段的值将用作选定值。使用 Html 助手 dropDownList 函数接受另一个参数选择文档。例子:

$id = $_GET["cid"];
\yii\helpers\Html::dropDownList('userid', $id, [ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name'), [......])
于 2014-12-20T19:27:34.233 回答
1

这是我的 SOLID 方法。

控制器

$model = new User();
$model->userid = $id; #this line does the magick. Make sure the $id has a value, so do the if else here.
return $this->return('view', compact('model'))

但是,如果您更喜欢 setter 方法。做这个...

# Model
class User extends ActiveRecord
{
    public function setUserId(int $userId): void
    {
        $this->userid = $userId;            
    }
}

# Controller
$model = new User();
$model->setUserId($userId);

查看(按原样查看)

$form->field($model, 'userid')
->dropDownList(...)
->label('');
于 2017-11-20T00:19:44.200 回答
0

我添加的所有选项都是不需要的。“值”索引中写入的内容是默认选择的下拉项。Prompt 仅显示没有关联值的第一个选项。

echo $form->field($model, 'model_attribute_name')
    ->dropDownList($associativeArrayValueToText, 
    [
        'value'=> $valueIWantSelected, 
        'prompt' => 'What I want as a placeholder for first option', 
        'class' => 'classname'
    ]);

您将在以下文件中找到分配此功能的函数:

供应商/yiisoft/yii2/helpers/BaseHtml.php

公共静态函数 renderSelectOptions($selection, $items, &$tagOptions = [])

同样从函数中您可以看到您可以将一个 optgroup 添加到您的下拉列表中,您只需要在我放置 $associativeArrayValueToText 的位置提供一个多维数组。这只是意味着您可以通过在下拉列表中引入组标题来拆分您的选项。

于 2020-01-30T08:50:12.980 回答
0

使用下面的代码:

$category = \backend\models\ProductCategory::find()->WHERE(['deleted'=>'N'])->all();

$listData = ArrayHelper::map($category,'product_category_id','category_name');

echo $form->field($model, 'product_category_id')->dropDownList($listData,['prompt'=>'Select']);
于 2017-03-16T11:47:12.903 回答