【问题标题】:Preselecting an option from a select field in a fields array?从字段数组中的选择字段中预选一个选项?
【发布时间】:2016-12-19 22:49:58
【问题描述】:

观察以下 $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”字段中的数组中“选择”一个选项。我需要在选择中显示所有选项,但我希望能够根据与用户关联的 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}}

    ],

但这没有用。我该怎么做呢?

【问题讨论】:

    标签: php forms select laravel-5


    【解决方案1】:

    添加不带任何索引的选定值。
    试试这个:

    '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}}
    
        ],
    

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多