0

私は CakePHP 2.7.8 に取り組んでいます。Ajaxを使ってリストの変更で関連リストを更新したい。

データベースにテーブルとテーブルがあり、customersプロジェクトにモデルがあります。customer_addressescustomerscustomerAddress

データベースからCakePHPによって生成されたドロップダウンリストから、選択した顧客serviceRequestsを選択してアドレス指定する必要がある別のコントローラーがあります。customer

私がやったこと-コントローラーに関数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ビューで、最後に次のスクリプトを追加しました。serviceRequestsadd

<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 呼び出しにアクセスすると機能せず、すべての顧客の名前とすべての顧客の住所がリストに表示されます。

4

1 に答える 1