Cakephp自动完成填充多个字段

时间:2014-03-11 10:22:47

标签: javascript jquery cakephp autocomplete cakephp-2.3

我已按照本教程为我的表单创建自动填充功能:

http://www.willis-owen.co.uk/2012/12/autocomplete-widget-cakephp/#comment-12074

出现了一些错误,但我完美地完成了它。

现在我想根据所选公司将提取的数据添加到多个表单字段(type = text)。怎么能实现这一目标?我向控制器添加了更多字段:

public function find_company_by_name() {

  $this->Customer->recursive = -1;

  if ($this->request->is('ajax')) {
    $this->autoRender = false;
    $results = $this->Customer->find('all', array(
      'fields' => array('Customer.name', 'Customer.name_2', 'Customer.address', 'Customer.postalCode', 'Customer.postalAddress',),
      //remove the leading '%' if you want to restrict the matches more
      'conditions' => array('Customer.name LIKE ' => '%' . $this->request->query['q'] . '%')
    ));
    foreach($results as $result) {
      echo $result['Customer']['address'] . "\n";
    }


  } else {
    //if the form wasn't submitted with JavaScript
    //set a session variable with the search term in and redirect to index page
    // $this->Session->write('customerName',$this->request->data['Customer']['name']);
    //  $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
  }

}

所以没什么特别的,只在search子句中添加了字段。下一步是我必须在自动完成触发JavaScript中填充输入字段。怎么做?这是我非常简单的JS:

<script>
  $(document).ready(function(){  
    $("#invoiceName").autocomplete("http://www.domain.com/application/customers/find_company_by_name.json", {
    minChars: 1
    });
  });
</script>

如何将“name_2”,“address”,“postalCode”和“postalAddress”字段添加到Javascript中以填充表单中的其他字段?

提前致谢:)

1 个答案:

答案 0 :(得分:0)

我没有得到任何答案,但我自己想出来了,原作者理查德威利斯 - 欧文的指导很少。

这是CONTROLLER:

public function find_company_by_name() {

    $this->Customer->recursive = -1;

    if ($this->request->is('ajax')) {
    $this->autoRender = false;
    $results = $this->Customer->find('all', array('fields' => array('Customer.id', 'Customer.name', 'Customer.name_2', 'Customer.address', 'Customer.postalCode', 'Customer.postalAddress'), 'conditions' => array('Customer.name LIKE ' => '%' . $this->request->query['q'] . '%')));
        foreach($results as $result) {
          echo $result['Customer']['name'].'|'.$result['Customer']['id'].'|'.$result['Customer']['name_2'].'|'.$result['Customer']['address'].'|'.$result['Customer']['postalCode'].'|'.$result['Customer']['postalAddress']."\n";
        }
    } 

}

...这里是来自VIEW的Javascript:

<script>
$(document).ready(function(){  
    $("#invoiceName").autocomplete("http://www.domain.fi/application/customers/find_company_by_name.json", 
        {
            minChars:       1,
            delay:          0,
            maxCacheLength: 100,
            onItemSelect: 
            function (item) {
                        $("#invoiceCustomerNo").val(item.data[0]);
                        $("#invoiceName2").val(item.data[1]);
                        $("#invoiceAddress").val(item.data[2]);
                        $("#invoicePostalCode").val(item.data[3]);
                        $("#invoicePostalAddress").val(item.data[4]);
                    }
        }
    );
});
</script>

希望这有助于有人挣扎于此挑战&#34; :)