Cakephp 2.1 - AjaxHelper

时间:2012-04-09 06:47:28

标签: cakephp cakephp-2.1 cakephp-ajaxhelper

我正在使用cakePhp的最新版本(2.1.1)。 我试图实现ajaxHlper(http://www.cakephp.4uk.pl/)。但卡在两者之间。

 <head>
<script type="text/javascript">
     $(document).ready(function(){
            $("#EmployeeAddForm").validate();
     });            

</script>   
</head>

<div class="employees form">
<?php echo $this->Form->create('Employee');?>
<fieldset>
    <legend><?php echo __('Add Employee'); ?></legend>
<?php
    echo $this->Form->input('first_name');
    echo $this->Form->input('last_name');
    //echo $this->Form->input('age');
    echo $this->Form->input('age', array('class' => 'required number'));
    echo $this->Form->input('sex');
    echo $this->Form->input('Adress.first_line');
    echo $this->Form->input('Adress.second_line');
    echo $this->Form->input('Adress.city');
    echo $this->Form->input('Adress.state');
    echo $ajax->autoComplete('Department.name', '/ajax/autoComplete')
?>
</fieldset>
   <?php echo $this->Form->end(__('Submit'));?>
 </div>
   <div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>

    <li><?php echo $this->Html->link(__('List Employees'), array('action' => 'index'));?></li>
    <li><?php echo $this->Html->link(__('List Adresses'), array('controller' => 'adresses', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Adress'), array('controller' => 'adresses', 'action' => 'add')); ?> </li>
</ul>

  

这是我的add.ctp文件,其中我试图使用ajaxHelper自动完成Department.name。

在我的EmployeesController.php中,我有自动完成功能,就像

 function autoComplete() {
    echo $this->params['url']['q'] . "---";
    $this->loadModel("Department");
    $this->set('departments', $this->Department->find('all', array(
        'conditions' => array(
            'Department.name LIKE ' => '%'.$this->params['url']['q'].'%'
        ),
        'limit' => $this->params['url']['limit'],
        'fields' => array('name')
    )));
    $this->layout = 'ajax';
} 

它不起作用。我在做什么可能是个错误? 它给了我以下错误:

注意(8):未定义的变量:ajax [APP \ View \ Employees \ add.ctp,第84行] 致命错误:在第84行的C:\ xampplite \ htdocs \ cakephp \ app \ View \ Employees \ add.ctp中的非对象上调用成员函数autoComplete()

1 个答案:

答案 0 :(得分:1)

假设您已将帮助程序添加到控制器的$helpers数组中,则必须使用$this->Ajax->autoComplete()而不是$ajax->autoComplete()来访问帮助程序。