1

私は多くの調査を行い、いくつかの異なる例を適用しようとしましたが、実際には何も機能していないようです.

したがって、顧客、プロジェクト、イベントの 3 つのモデルがあります。顧客には多くのプロジェクトがあり、プロジェクトには多くのイベントがあります。

イベントの作成中に、ユーザーにドロップダウン リストから顧客を選択してもらい、選択した顧客に属するプロジェクトのリストをユーザーに提供する必要があります。私が得た最も近いものは次のとおりです。私は AJAX の経験がないので、ブレーキをかけるのは本当に難しいです。

Porject のコントローラーでのアクション:

    public function getbycustomer(){

    $customer_id = $this->request->data['Event']['customer_id'];

    $projects = $this->Project->find('list', array('conditions'=>array('Project.customer_id' => $customer_id), 'recursive' => -1));

    $this->set('projects', $projects);
    $this->layout = 'ajax'; 
}

このアクションのビューは次のとおりです。

    <?php foreach ($projects as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>

イベントを追加するためのビューのスニペットは次のとおりです。

    echo $this->Form->input('customer_id');
        echo $this->Form->input('project_id'); 

//form continues and at the end of a page there is the AJAX call

    $this->Js->get('#EventCustomerId')->event('change', 
    $this->Js->request(array(
        'controller'=>'projects',
        'action'=>'getbycustomer'
        ), array(
        'update'=>'#EventProjectId',
        'async' => true,
        'method' => 'post',
        'dataExpression'=>true,
        'data'=> $this->Js->serializeForm(array(
            'isForm' => true,
            'inline' => true
            ))
        ))
    );

デバッグの適切な方法さえ知らないので、どんな助けも大歓迎です。より価値のある情報を提供できます。

4

2 に答える 2

0

autoRender を false に設定する必要があると思います。そうしないと、app/View/Project/getbycustomer.ctp でテンプレートをレンダリングしようとします。また、おそらく JSON を返したり出力したりしたいと思うでしょう。おそらくこれを行うにはいくつかの方法がありますが、私は似たようなものを持っており、コントローラーのアクションは基本的に次のとおりです。

public function getbycustomer() {
  $this->autoRender = $this->layout = false;
  $customer_id = $this->request->data['Event']['customer_id'];
  $projects = $this->Project->find('list', array('conditions'=>array('Project.customer_id' => $customer_id), 'recursive' => -1));
  $this->set('projects', $projects);
  echo json_encode(array('html' => $this->render('your_partial_template')->body())); // This template would be in app/View/Project/json/
}

次に、Ajax 呼び出しで、返された JSON を処理する「成功」コールバックが必要です。

success: function(data) {
  $('#EventProjectId').html(data.html); // assuming this an empty container
}

また、プロジェクト テーブルが 2 列しかない場合を除き、検索の結果はおそらく期待どおりのものではありません。に変更します

$projects = $this->Project->find('list', array('conditions'=>array('Project.customer_id' => $customer_id), 'fields' => array('id', 'name_or_whatever', 'recursive' => -1));

次に、部分テンプレートでフォーム ヘルパーを使用できます。

<?php echo $this->Form->input('Projects.id', array('options' => $projects)); ?>
于 2013-12-11T18:40:24.740 に答える