CakePHP 中的 jQuery 自动完成 JSON
我正在尝试使用 CakePHP 制作 jQuery AutoComplete JSON 数据。我的代码确实有效。它按预期显示自动完成功能,但失败:
- 一旦我们选择它(空白),所选项目就不会显示在搜索框中。
- 如果数据不存在,则不会显示“无结果”。
代码如下:
//我的 Customer 控制器的搜索操作
function search(){
$this->Customer->recursive = -1;
$customers = $this->Customer->find('all', array(
'conditions'=>array('Customer.nama LIKE'=>$this->params['url']['q'].'%'),
'fields'=>array('id', 'name', 'telp', 'address'))
);
$this->set('customers', $customers);
Configure::write('debug', 0);
$this->layout = 'ajax';
}
//我的 search.ctp
<?php
if(!empty($customers)) {
$data = array();
foreach ($customers as $customer){
$data[] = $customer['Customer'];
}
echo json_encode($data);
}
else echo 'No result';
?>
//我的 js 文件
$().ready(function () {
$("#search-txtbox").autocomplete("/customers/search",
{
parse: function(data){
var parsed = [];
for (var i=0; i < data.length; i++) {
parsed[i] = {
data: data[i],
value: data[i].name //the search item
};
}
return parsed;
},
formatItem: function (row, i, max) {
var str = row.name + ' (Telp: '+ row.telp +')' + '<br />';
str += row.address;
return str;
},
formatResult: function (row) {
return row.name;
},
minChars: 2,
max: 0,
width: 224,
scrollHeight: 420,
dataType: 'json'
}
);
});
请帮我修改代码。谢谢你!
I'm trying to make jQuery AutoComplete JSON data with CakePHP. My code actually WORKs. It shows the autocompletion as expected, but it fails on:
- the selected item doesn't show up in the searchbox once we select it (blank).
- the 'No result' doesn't show up if the data does not exist.
Here's the code:
//search action of my Customer controller
function search(){
$this->Customer->recursive = -1;
$customers = $this->Customer->find('all', array(
'conditions'=>array('Customer.nama LIKE'=>$this->params['url']['q'].'%'),
'fields'=>array('id', 'name', 'telp', 'address'))
);
$this->set('customers', $customers);
Configure::write('debug', 0);
$this->layout = 'ajax';
}
//my search.ctp
<?php
if(!empty($customers)) {
$data = array();
foreach ($customers as $customer){
$data[] = $customer['Customer'];
}
echo json_encode($data);
}
else echo 'No result';
?>
//my js file
$().ready(function () {
$("#search-txtbox").autocomplete("/customers/search",
{
parse: function(data){
var parsed = [];
for (var i=0; i < data.length; i++) {
parsed[i] = {
data: data[i],
value: data[i].name //the search item
};
}
return parsed;
},
formatItem: function (row, i, max) {
var str = row.name + ' (Telp: '+ row.telp +')' + '<br />';
str += row.address;
return str;
},
formatResult: function (row) {
return row.name;
},
minChars: 2,
max: 0,
width: 224,
scrollHeight: 420,
dataType: 'json'
}
);
});
Please help me modify the code. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您正在使用的插件已被弃用,取而代之的是 jQueryUI,我强烈建议建议改用它(来自社区、更新等的更好支持)。但是,如果由于某种原因您必须使用该插件,那么您的问题如下:
这方面的文档很少(如果有的话),但看起来
parse
函数必须返回具有插入到input
元素中的result
属性的对象。所以你应该只需要修改你的解析函数来添加该属性:我不确定你想要“无结果”显示在哪里,但是你可以稍微修改你的 PHP 代码并在
解析
函数:PHP
JavaScript
抱歉,我无法提供有效的示例。如果您想要一个使用 jQueryUI 小部件的示例,我很乐意提供一个。
First of all, the plugin you're using has been deprecated in favor of jQueryUI, and I'd highly recommend using it instead (better support from the community, updates, etc.). However, if for some reason you have to use that plugin, here are your issues:
This is poorly documented (if it is documented at all), but it looks like the
parse
function must return an object with aresult
property that gets inserted into theinput
element. So you should just have to modify you're parse function to add that property:I'm not sure where you want "No result" to show up, but you could slightly modify your PHP code and show a message inside the
parse
function:PHP
JavaScript
Sorry I can't provide a working example. If you want an example using the jQueryUI widget, I'd be happy to provide one.