简体   繁体   English

Cakephp自动完成填充多个字段

[英]Cakephp autocomplete populate multiple fields

I have followed this tutorial for creating autocomplete for my form: 我已按照本教程为表单创建自动完成功能:

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

There was some errors, but I got it working perfectly. 出现了一些错误,但是我可以正常运行。

Now I want to add fetched data to multiple form fields (type=text) based on the selected company. 现在,我想根据所选公司将获取的数据添加到多个表单字段(类型=文本)中。 How can this be achieved? 如何做到这一点? I added more fields to the 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.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'));
  }

}

So nothing special, only added fields to the search clause. 因此没什么特别的,只向搜索子句添加了字段。 Next step is that I have to populate input fields in autocomplete triggering JavaScript. 下一步是我必须在自动完成触发JavaScript中填充输入字段。 How to do that? 怎么做? Here is my very simple JS: 这是我非常简单的JS:

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

How to get "name_2", "address", "postalCode" and "postalAddress" -fields to the Javascript to populate other fields in the form? 如何获取“ name_2”,“ address”,“ postalCode”和“ postalAddress” -JavaScript字段以填充表单中的其他字段?

Thanks in advance :) 提前致谢 :)

I did not got any answers, but I figured it out myself, and little guidance from original author Richard Willis-Owen. 我没有任何答案,但我自己弄明白了,原始作者理查德·威利斯·欧文也没有提供任何指导。

Here is 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";
        }
    } 

}

...and here is Javascript from VIEW: ...这是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>

Hope this helps somebody struggling with this "challenge" :) 希望这可以帮助有人在这种“挑战”中挣扎:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM