CakePHP 中的 jQuery 自动完成 JSON

发布于 2024-11-30 05:44:39 字数 1925 浏览 1 评论 0原文

我正在尝试使用 CakePHP 制作 jQuery AutoComplete JSON 数据。我的代码确实有效。它按预期显示自动完成功能,但失败:

  1. 一旦我们选择它(空白),所选项目就不会显示在搜索框中。
  2. 如果数据不存在,则不会显示“无结果”。

代码如下:

//我的 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:

  1. the selected item doesn't show up in the searchbox once we select it (blank).
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-12-07 05:44:39

首先,您正在使用的插件已被弃用,取而代之的是 jQueryUI,我强烈建议建议改用它(来自社区、更新等的更好支持)。但是,如果由于某种原因您必须使用该插件,那么您的问题如下:

  1. 这方面的文档很少(如果有的话),但看起来 parse 函数必须返回具有插入到 input 元素中的 result 属性的对象。所以你应该只需要修改你的解析函数来添加该属性:

    解析:函数(数据){ 
        var 解析 = [];
        for (var i=0; i < data.length; i++) {
            解析[i] = {
                数据:数据[i],
                value: data[i].name, //搜索项
                结果:数据[i].结果
            };
        }
        返回已解析;
    },
    
  2. 我不确定你想要“无结果”显示在哪里,但是你可以稍微修改你的 PHP 代码并在解析函数:

    PHP

    $data = array();
    
    foreach($客户作为$客户){
        $data[] = $customer['客户'];
    }
    
    回显 json_encode($data);
    

    JavaScript

    解析:函数(数据){ 
        var 解析 = [];
        for (var i=0; i < data.length; i++) {
            解析[i] = {
                数据:数据[i],
                value: data[i].name, //搜索项
                结果:数据[i].名称
            };
        }
        if (!parsed.length) { // <-- 对“No Results”消息执行某些操作。
            $("body").append("没有结果");
        }
        返回已解析;
    },
    

抱歉,我无法提供有效的示例。如果您想要一个使用 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:

  1. This is poorly documented (if it is documented at all), but it looks like the parse function must return an object with a result property that gets inserted into the input element. So you should just have to modify you're parse function to add that property:

    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
                result: data[i].result
            };
        }
        return parsed;
    },
    
  2. 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

    $data = array();
    
    foreach ($customers as $customer){
        $data[] = $customer['Customer'];
    }
    
    echo json_encode($data);
    

    JavaScript

    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
                result: data[i].name
            };
        }
        if (!parsed.length) { // <-- Do something with the "No Results" message.
            $("body").append("no results");
        }
        return parsed;
    },
    

Sorry I can't provide a working example. If you want an example using the jQueryUI widget, I'd be happy to provide one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文