Я работаю над CakePHP 2.7.8. Я хочу обновить связанный список с изменением списка с помощью Ajax.CakePHP Ajax: список обновлений с изменениями в перечне
У меня есть customers
стол и customer_addresses
таблицу в базе данных и customers
и customerAddress
модели в проекте.
Существует еще один контроллер serviceRequests
, где я должен выбрать customer
и адрес выбранного клиента из выпадающего списка, сгенерированного CakePHP из базы данных.
То, что я done- Я добавил функцию getCustomerAddress
в serviceRequests
контроллере
public function getCustomerAddress(){
$customer_id = $this->request->data['Post']['customer_id'];
$customer_address = $this->CustomerAddress->find('list',array(
'condition' => array('CustomerAddress.customer_id' => $customer_id),
'recursive' => -1
));
$this->set('customerAddresses', $customer_address);
$this->layout = 'ajax';
}
, чтобы отобразить полученные данные, у меня есть вид get_customer_address.ctp
<?php
foreach ($customerAddresses as $key => $value): ?>
<option value="<?php echo $key;?>"><?php echo $value; ?></option>
<?php endforeach; ?>
В add.ctp
зрения serviceRequests
контроллер для add
функция, я добавил следующий сценарий в последний раз.
<div class="serviceRequests form">
<?php echo $this->Form->create('ServiceRequest'); ?>
<fieldset>
<legend><?php echo __('Add Service Request'); ?></legend>
<?php
echo $this->Form->input('customer_id');
echo $this->Form->input('customer_address_id');
echo $this->Form->input('status');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<?php
$this->Js->get('#ServiceRequestCustomerId')->event('change',
$this->Js->request(array(
'controller' => 'serviceRequests',
'action' => 'getCustomerAddress'
), array(
'update' => '#ServiceRequestCustomerAddressId',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data' => $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
и оказывать Js
, я добавил следующий код к последнему из default.ctp
<!-- script for layout -->
<?php echo $scripts_for_layout; ?>
<!-- Js writeBuffer -->
<?php
if(class_exists('JsHelper') && method_exists($this->Js, 'writeBuffer')) echo $this->Js->writeBuffer();
// writes cached scripts
?>
Но доступ localhost/serviceRequests/add
вызова Ajax не работаю и имя всех клиентов и адрес все клиента показываются в список.