【发布时间】: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