自动完成搜索表单 cakephp

发布于 2024-11-08 12:01:18 字数 977 浏览 0 评论 0原文

嘿,我是个新手,我正在寻找一种像谷歌一样拥有自动完成功能的搜索框的方法。

我已经搜索过,我发现的最佳前景似乎是 http://www .pengoworks.com/workshop/jquery/autocomplete.htm 我在论坛上找到的。建议它的人说他将它与 http://code.google 一起使用。 com/p/searchable-behaviour-for-cakephp/ 这是完全正确的,因为我在上次尝试弄清楚 cakephp 时成功安装了 searchable。

问题是,我以前没有使用过太多 JavaScript,而且我对我到底要做什么有点困惑。带有自动完成代码的文档没有详细介绍我能理解的任何细节。

如果我们假设我设法正确安装可搜索行为,任何好心人都可以向我解释我将如何使自动完成工作正常进行吗?

它说只使用:

$("selector").autocomplete(url [, options]);

例如:

$("#input_box").autocomplete("autocomplete_ajax.cfm");

自动完成期望存在 id 为“input_box”的输入元素。当用户开始在输入框中键入内容时,自动完成程序将使用名为 q 的 GET 参数请求 autocomplete_ajax.cfm

这是我没有得到的位。我应该把它放在哪里?一旦我把它放在某个地方,我是否只需要将我的输入之一命名为“input_box”?

提前致谢。

Hey there, I'm a total newbie and I'm looking for a way to have a search box with autocomplete just like google does.

I've searched and the best prospect that I've found seems to be http://www.pengoworks.com/workshop/jquery/autocomplete.htm which I found on a forum. The guy who suggested it says he uses it with http://code.google.com/p/searchable-behaviour-for-cakephp/ which is dead on because I've managed to install searchable on my last attempt at figuring out cakephp.

The thing is, I've not used much javascript before and I'm a bit confused as to what exactly I'm meant to be doing. The documentation with the autocomplete codes doesn't go into any detail that I can understand.

If we assume that I manage to get searchable behaviour installed correctly, could any kind person explain to me how I would go about making the autocomplete work?

It says to just use:

$("selector").autocomplete(url [, options]);

eg:

$("#input_box").autocomplete("autocomplete_ajax.cfm");

Autocomplete expects an input element with the id "input_box" to exist. When a user starts typing in the input box, the autocompleter will request autocomplete_ajax.cfm with a GET parameter named q

thats the bit I don't get. Where am I meant to put that? And once I've put it somewhere then do I just need to name one of my inputs "input_box"?

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

残月升风 2024-11-15 12:01:18

共有三个步骤:

1)使用视图中的 Html 帮助器创建一个带有输入字段的完全正常的表单:

// app/views/foo_bars/search.ctp
<?php
echo $this->Form->create('FooBar');
echo $this->Form->input('field');
echo $this->Form->end('Submit');
?>

2)触发 jquery 自动完成:

// app/views/foo_bars/search.ctp
<?php
echo $this->Html->scriptBlock(
    .'$("#FooBarField").autocomplete({'
        .'source:"/foo_bars/find",'
        .'delay: 100,'
        .'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
        .'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
    .'});'
    array('inline' => false));
?>

3)通过控制器查询数据库以获取可能的值:

// app/controllers/foo_bars_controller.php
<?php
public function find() {
    if ($this->RequestHandler->isAjax()) {
        $this->autoLayout = false;
        $this->autoRender = false;
        $this->FooBar->recursive = -1;
        $results = $this->FooBar->find('all', array('fields' => array('id', 'name'), 'conditions' => array('name LIKE "%'.$_GET['term'].'%"')));
        $response = array();
        $i = 0;
        foreach($results as $result){
            $response[$i]['value'] = $result['FooBar']['name'];
            $response[$i]['id'] = $result['FooBar']['id'];
            $i++;
        }
        echo json_encode($response);
    }
}
?>

There are three steps:

1) create a totally normal form with an input field, using the Html helper in your view:

// app/views/foo_bars/search.ctp
<?php
echo $this->Form->create('FooBar');
echo $this->Form->input('field');
echo $this->Form->end('Submit');
?>

2) Have a jquery autocomplete fired:

// app/views/foo_bars/search.ctp
<?php
echo $this->Html->scriptBlock(
    .'$("#FooBarField").autocomplete({'
        .'source:"/foo_bars/find",'
        .'delay: 100,'
        .'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
        .'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
    .'});'
    array('inline' => false));
?>

3) Query a database through a controller to get possible values:

// app/controllers/foo_bars_controller.php
<?php
public function find() {
    if ($this->RequestHandler->isAjax()) {
        $this->autoLayout = false;
        $this->autoRender = false;
        $this->FooBar->recursive = -1;
        $results = $this->FooBar->find('all', array('fields' => array('id', 'name'), 'conditions' => array('name LIKE "%'.$_GET['term'].'%"')));
        $response = array();
        $i = 0;
        foreach($results as $result){
            $response[$i]['value'] = $result['FooBar']['name'];
            $response[$i]['id'] = $result['FooBar']['id'];
            $i++;
        }
        echo json_encode($response);
    }
}
?>
南薇 2024-11-15 12:01:18

echo $this->Html->scriptBlock(

'$("#FooBarField").autocomplete({'

    .'source:"/Search/find",'
    .'delay: 100,'
    .'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
    .'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'

.'});'.

array('inline' => false));

echo $this->Html->scriptBlock(

'$("#FooBarField").autocomplete({'

    .'source:"/Search/find",'
    .'delay: 100,'
    .'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
    .'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'

.'});'.

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