从字段数组中的选择字段中预选选项?

时间:2016-08-12 20:41:13

标签: php forms select laravel-5

观察以下$ fields数组,该数组用作刀片中的输入表单:

$fields = [
    'user_id'           => [
        'label' => 'Opportunity Owner' . $req,
        'type'  => 'select',
        'class' => 'select2',
        'opts'  => User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()
    ],
    'name'              => [
        'label' => 'Opportunity Name' . $req,
    ],
    'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
    ],
    'description'       => [
        'label' => 'Description',
        'type'  => 'textarea'
    ],
    [
        'type'  => 'submit',
        'label' => 'Save',
        'class' => 'btn btn-primary !important'
    ]

];

在'agent_id'部分中,如果用户预先分配了值,我想预先选择一个值。我知道如何从用户那里获取信息,但我很遗憾如何在'agent_id'字段中“选择”数组中的选项。我需要在select中显示所有选项,但我希望能够根据链接到用户的agent_id号码选择一个“已选择”。我尝试了以下方法:

'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
        'selected' => {{appropriate number here}}

    ],

但那没用。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

添加没有任何索引的选定值 试试这个:

'agent_id'          => [
        'label'       => 'Agent',
        'type'        => 'select',
        'class'       => 'select2',
        'textAsValue' => false,
        'opts'        => array_replace([0 => '-- Select Sales Rep --'],
                         User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
        {{appropriate number here}}

    ],
相关问题