首页 文章

CakePHP Ajax:更新列表,相关列表中有更改

提问于
浏览
0

我正在开发CakePHP 2.7.8 . 我想使用Ajax更新列表中的相关列表 .

我在数据库中有一个 customers 表和 customer_addresses 表,项目中有 customerscustomerAddress 模型 .

还有另一个控制器 serviceRequests ,我必须从CakePHP从数据库生成的下拉列表中选择 customer 和所选客户的地址 .

我做了什么 - 我在 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; ?>

serviceRequests 控制器 add 函数的 add.ctp 视图中,我在最后添加了以下脚本 .

<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调用无效,所有客户's name and all customer'的地址都显示在列表中 .

1 回答

相关问题