2015-11-19 86 views
1

我有一個包含服務列表的下拉字段的表單。這些服務基於已輸入到管理模型「服務」中的內容顯示在drodpown中。SilverStripe - 基於表單字段選擇從管理模型中檢索選中的值

下拉:

<select id="ServiceReq"> 
    <option value="">Service Requested*</option> 
    <% if getServiceList %> 
     <% loop getServiceList %> 
      <option value="$Name">$Name</option> 
     <% end_loop %> 
    <% end_if %> 
</select> 

的服務管理模型的代碼:

<?php 
class Service extends DataObject { 

    private static $db = array(
     'Name' => 'varchar', 
    ); 

    private static $belongs_many_many = array(
     'Locations' => 'Location' 
    ); 

    public static $summary_fields = array(
     'Name' => 'Title', 
    ); 

    private static $field_labels = array(
     'Name' 
    ); 

    public function getCMSFields() { 

     $fields = parent::getCMSFields(); 

     if ($this->ID) { 
      $fields->addFieldToTab('Root.Locations', CheckboxSetField::create(
       'Locations', 
       'Locations', 
       Location::get()->filter(array(
        'AcceptingAppointments' => '1' 
       ))->map() 
      )); 
     } 

     return $fields; 
    } 
} 

我想要做的是,當服務從ServiceReq下拉列表中選擇,我想要檢索位於服務管理模型的位置選項卡中的複選框字段集中的所有選定位置。那麼這些位置將被用來填充位置的形式下拉列表:

<select id="Location"> 
    <option value="">Location/Hospital</option> 
</select> 

我知道我需要使用當前選擇的服務的ID,但我不知道我怎麼能功能的設置要做到這一點,但是我失去了如何在服務器端設置傳遞給表單的功能。

回答

1

如果您嘗試獲取與當前服務相關的位置,它將是$this->Locations(),您仍然可以應用您的過濾器,即$this->Locations()->filter(array('AcceptingAppointments' => '1'))->map()

如果它更多是一種實時更新類型的東西,您可能希望查看纏繞。有一個good blog post for getting started with entwine here

+0

我怎麼能扎到窗體的功能這一點,雖然,這樣,當我拿起從服務下拉列表中的服務,那麼位置下拉相應地填充? –

+0

SilverStripe中還有一些表單字段模塊,它們使用閉包獲取基於另一個字段的值。這些也可能是一個很好的參考。依賴字段的另一個很好的例子是[依賴下拉模塊](https://github.com/sheadawson/silverstripe-dependentdropdownfield) – muskie9

+0

有很多服務... 25是確切的。在這種情況下,我不確定依賴下拉模塊的設置是否可行。每個服務可以有自己的一組位置,並且如果服務列表增長,那麼看起來依賴下拉模塊功能可能需要更多的代碼行。我覺得它可能會變得多餘。 –

1

Dependent dropdown field module有助於做到這一點很好,很容易。

以下是使用從屬下拉字段的示例表單,該字段包含一個Service下拉字段和一個Location下拉字段,其中填充了與該服務相關的位置。

public function ServiceForm() { 

    $locationSource = function($serviceID) { 
     $service = Service::get()->byID($serviceID); 
     return $service->Locations() 
        ->filter('AcceptingAppointments', true) 
        ->map('ID', 'Name')->toArray(); 
    }; 

    $servicesField = DropdownField::create(
     'Service', 
     'Service', 
     Service::get()->map('ID', 'Name')->toArray() 
    )->setEmptyString(''); 

    $locationsField = DependentDropdownField::create(
     'Location', 
     'Location', 
     $locationSource 
    )->setDepends($servicesField); 

    $form = Form::create($this, 'ServiceForm', 
     FieldList::create(
      $servicesField, 
      $locationsField 
     ), 
     FieldList::create(
      FormAction::create('processServiceForm', 'Submit') 
     ), 
     RequiredFields::create(
      'Service', 
      'Location' 
     ) 
    ); 

    return $form; 
} 

首先,我們有一個正常的下拉字段來選擇服務:

$servicesField = DropdownField::create(
    'Service', 
    'Service', 
    Service::get()->map('ID', 'Name')->toArray() 
)->setEmptyString(''); 

接下來,我們添加從屬下拉字段來選擇服務:

$locationsField = DependentDropdownField::create(
    'Location', 
    'Location', 
    $locationSource 
)->setDepends($servicesField); 

setDepends功能定義了字段來鏈接這個字段,在這種情況下是服務字段。 $locationSource是一個函數,它將檢索位置並返回一個數組供字段使用。

$locationSource = function($serviceID) { 
    $service = Service::get()->byID($serviceID); 
    return $service->Locations() 
       ->filter('AcceptingAppointments', true) 
       ->map('ID', 'Name')->toArray(); 
}; 
相關問題