Yii:自定义CAutoComplete的结果 [英] Yii: Customize the results of CAutoComplete

查看:104
本文介绍了Yii:自定义CAutoComplete的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用CAutoComplete制作一个下拉列表.一切都设置好并且可以正常工作,这是我的操作代码:

I need to make a dropdown list using CAutoComplete. Everything is set and works fine, here is my code of the action:

<?php
    public function actionSuggestCharacter() {
        if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
            $name = $_GET['q']; 
            $criteria = new CDbCriteria;
            $criteria->condition='`Character` LIKE :keyword';
            $criteria->params=array(':keyword'=>"$name%");
            $criteria->limit = 5;
            $suggestions = zCharacter::model()->findAll($criteria);
            $returnVal = '';
            foreach($suggestions as $suggestion) {
                $returnVal .= $suggestion->Character."\n";
            }
            if (isset($suggestion)) {
                echo $returnVal;
            }
            $criteria->condition='`Character` LIKE :keyword';
            $criteria->params=array(':keyword'=>"%$name%");
            $criteria->limit = 5;
            $suggestions = zCharacter::model()->findAll($criteria);
            $returnVal = '';
            foreach($suggestions as $suggestion) {
                $returnVal .= $suggestion->Character."\n";
            }
            if (isset($suggestion)) {
                echo $returnVal;
            }
        }
    }
?>

此代码的作用是在开头显示前5个匹配项,关键字在任何位置.接下来5个匹配项在任何位置.

What this code does is that it shows the first 5 matches with the keyword at the beginning and the next 5 matches are with the keyword in any place.

示例.假设用户在输入字段"pdd"中输入(实际上并不重要,可以是任何文本),因此自动完成功能返回的结果如下所示:

Example. Let's say a user types in the input field "pdd" (doesn't really matter, could be any text), so the results returned by autocomplete will look like:

1. pddtext...
2. pddtext...
3. pdd_some_other_text
4. pdd_text
5. pdd_text
1. text_text_pdd
2. text_pdd_text
3. etc...

问题是我需要用某种线(带有边框的<hr><div>)分隔这两个块.我该怎么办?

The problem is I need to separate these two blocks by some kind of line (<hr> or <div> with the border). How can I do this?

谢谢.

推荐答案

您不能这样做吗?

<?php
    public function actionSuggestCharacter() {
        if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
            ...
            if (isset($suggestion)) {
                echo $returnVal;
            }
            echo "Hey this is the delimiter\n";
            $criteria->condition='`Character` LIKE :keyword';
            ....
        }
    }
?>

然后在客户端检查此字符串,并在遇到嘿,这是分隔符"时,将其替换为分隔符.

And then on the client side check for this string and when you encounter ""Hey this is the delimiter" replace it with your separator.

这篇关于Yii:自定义CAutoComplete的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆