SilverStripe - 通过jQuery.ajax将数据从控制器传递到html表单

时间:2015-11-20 14:28:59

标签: php jquery silverstripe

我正在处理一个有2个下拉列表的表单:服务请求和位置。每当用户选择服务时,应该使用为该服务选择的那些位置填充位置下拉列表。我有一个服务和位置的管理模型,这里是Service的一个模型,用于显示我如何将Locations模型数据映射到它:

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

这是与服务请求的下拉列表相关的jQuery,后跟将id传递给服务器的ajax函数:

  SchService.change(function() {
  if (SchService.val() != "" && SchService.val() != null) {
    SchLocation.prop('disabled', false);
    sendServiceId();
  } else {
    SchLocation.prop('disabled', true);
  }
});

  function sendServiceId(){
   var service_data = {
     serviceid: SchService.find('option:selected').attr('id')
   };
    $.ajax({
        type: "POST",
        url: "/home/getLocationsByService",
        data: service_data
    }).done(function (response) {
       console.log(response);
    });
}

最后,getLocationsByService的函数,它使用ajax调用中的Service id来检索该服务的位置:

 public function getLocationsByService(){
    $serviceid = $this->getRequest()->getVar('serviceid');
    $service = Service::get()->byId($serviceid);
    $locations = Service::Locations();
    foreach ($locations as $location){
        return json_encode($locations);//not sure if this will be needed
    }
}

我现在很困惑如何解析以可以返回到表单的方式检索的位置数据,以便在Locations下拉字段中使用。我猜json可能是需要的,但就我而言。

1 个答案:

答案 0 :(得分:2)

这可能不是您追求的答案,但我建议您使用一个很棒的模块...... https://github.com/sheadawson/silverstripe-dependentdropdownfield

这是提供的例子......

// 1. Create a callable function that returns an array of options for the DependentDropdownField. 
// When the value of the field it depends on changes, this function is called passing the 
// updated value as the first parameter ($val)
$datesSource = function($val) { 
    if ($val == 'one') {
        // return appropriate options array if the value is one.
    }
    if ($val == 'two') {
        // return appropriate options array if the value is two.
    }
}; 

$fields = FieldList::create(
    // 2. Add your first field to your field list, 
    $fieldOne = DropdownField::create('FieldOne', 'Field One', array('one' => 'One', 'two' => 'Two')),
    // 3. Add your DependentDropdownField, setting the source as the callable function 
    // you created and setting the field it depends on to the appropriate field
    DependentDropdownField::create('FieldTwo', 'Field Two', $datesSource)->setDepends($fieldOne)
);