2016-08-12 201 views
0

遵守以下$字段阵列,其被用作在一个刀片的输入形式:预先选择字段数组中选择字段的选项?

$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}} 

    ], 

但这并不奏效。我怎么能这样做呢?

回答

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}} 

    ],