CakePHP Ajax : 更新列表并更改相关列表
CakePHP Ajax : update list with change in related list
我正在使用 CakePHP 2.7.8。我想使用 Ajax.
更新相关列表并更改列表
我在数据库中有 customers
table 和 customer_addresses
table,在项目中有 customers
和 customerAddress
模型。
还有另一个控制器 serviceRequests
,我必须从 CakePHP 从数据库生成的下拉列表中 select customer
和 selected 客户的地址。
我做了什么-
我在 serviceRequests
控制器
中添加了一个函数 getCustomerAddress
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
控制器 add
功能的 serviceRequests
视图中,我在最后添加了以下脚本。
<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 调用不起作用,所有客户的姓名和所有客户的地址都显示在列表中。
这是一个如何用蛋糕实现链式选择的例子http://sandbox.dereuromark.de/sandbox/ajax_examples/chained_dropdowns
- 该示例的相关文章在这里 http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/
我正在使用 CakePHP 2.7.8。我想使用 Ajax.
更新相关列表并更改列表我在数据库中有 customers
table 和 customer_addresses
table,在项目中有 customers
和 customerAddress
模型。
还有另一个控制器 serviceRequests
,我必须从 CakePHP 从数据库生成的下拉列表中 select customer
和 selected 客户的地址。
我做了什么-
我在 serviceRequests
控制器
getCustomerAddress
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
控制器 add
功能的 serviceRequests
视图中,我在最后添加了以下脚本。
<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 调用不起作用,所有客户的姓名和所有客户的地址都显示在列表中。
这是一个如何用蛋糕实现链式选择的例子http://sandbox.dereuromark.de/sandbox/ajax_examples/chained_dropdowns - 该示例的相关文章在这里 http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/