【问题标题】:Cakephp autocomplete populate multiple fieldsCakephp 自动完成填充多个字段
【发布时间】:2014-03-11 10:22:47
【问题描述】:

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

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

有一些错误,但我得到了它的完美运行。

现在我想根据所选公司将获取的数据添加到多个表单字段(类型=文本)。如何做到这一点?我向控制器添加了更多字段:

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'));
  }

}

所以没什么特别的,只是在搜索子句中添加了字段。下一步是我必须在自动完成触发 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 以填充表单中的其他字段?

提前致谢:)

【问题讨论】:

  • 有人对此有想法吗?

标签: javascript jquery cakephp autocomplete cakephp-2.3


【解决方案1】:

我没有得到任何答案,但我自己想通了,原作者 Richard Willis-Owen 的指导很少。

这是控制器:

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>

希望这可以帮助那些在这个“挑战”中挣扎的人:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2016-07-02
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多