Yii2下拉选择值

Yii2下拉选择值,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,我想在Yii2下拉列表中显示所选值 $\u获取值: $id = $_GET["cid"]; 下拉代码 $form->field($model, 'userid') ->dropDownList( [User::getUser()], //[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')], ['prompt'=>'

我想在Yii2下拉列表中显示所选值

$\u获取值:

  $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('');           
但是这种方法不起作用

试试这个

$model->userid=$id;
$form->field($model, 'userid')
->dropDownList(...)
->label('');

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

$id = $_GET["cid"];
\yii\helpers\Html::dropDownList('userid', $id, [ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name'), [......])

我希望这对你有帮助

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

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

因此,在本例中,我有一个状态数组,值属性有状态缩写,例如
value=“FL”
。我从存储缩写的地址表中获取所选状态,因此我所要做的就是将其用作选项数组中的数组键:

echo $form->field($model, 'state')->dropDownList($listData, ['prompt'=>'Select...', 'options'=>[$address->state=>["Selected"=>true]]]);
文档中详细说明了这一点:


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

最终输出:
NONE
使用以下代码:

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

这是我的S.O.L.I.D方法

控制器

$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);
视图(视图按原样显示)


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

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,只需在我放置$AssociationArrayValueToText的地方提供一个多维数组。这只是意味着您可以通过在下拉列表中引入组标题来拆分选项。

我遇到了一个错误:在非objectArah上调用成员函数formName()。。我只是忘了在PHP标记中编写echo。我还缺少这行代码:$model->userid=$id;我缺少这行代码:$model->userid=$id$型号->国家=‘美国’;这对我不起作用。这里有什么错误?@NileshKumar,可能您的
$model->country
想要的是国家Id,而不是名称。所以:
$model->country=$countryId
(1,30,其他一些)。对于多选也很有用。
# 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('');
echo $form->field($model, 'model_attribute_name')
    ->dropDownList($associativeArrayValueToText, 
    [
        'value'=> $valueIWantSelected, 
        'prompt' => 'What I want as a placeholder for first option', 
        'class' => 'classname'
    ]);