2016-08-12 131 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}} 

    ],